/**
 * (c) by aheadware.com
 */

var app = app || {};
app.state = app.state || {};

app.state.ConfigurationAccountEditPassword = function()
{
    var state = app.core.StateManager.createState( 'configuration-account-edit-password' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent();

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Account',
            'getAccountRelatedData',
            null,
            function( res )
            {
                app.core.View.setContent(
                    app.core.View.getTemplate( 'configuration-account-edit-password', res )
                );

                $content.find( '[data-id="btn-update-password"]' ).first().click( function() {
                    let $form = $content.find('[data-id="form-account-edit-password"]').first(),
                        $oldpassword = $form.find( '[data-id="input-current-password"]' ).first(),
                        $password = $form.find( '[data-id="input-new-password"]' ).first(),
                        $password2 = $form.find( '[data-id="input-new-password-check"]' ).first(),
                        isValid = false;

                    $oldpassword.get(0).setCustomValidity( '' );

                    // Special validation
                    if ( $password.val() !== $password2.val() )
                    {
                        $password2.get(0).setCustomValidity( 'wrong' );
                    }
                    else
                    {
                        $password2.get(0).setCustomValidity( '' );
                    }

                    isValid = app.util.Form.bootstrapValidate($form);

                    if ( isValid )
                    {
                        app.gui.PageLoader.show();
                        app.core.Rpc.call(
                          'Account',
                          'changePassword',
                            {
                                'currentPassword' : $oldpassword.val(),
                                'newPassword' : $password.val()
                            },
                            function( res )
                            {
                                if ( res && res.hasOwnProperty( 'oldpasswordnok' ) )
                                {
                                    $oldpassword.get(0).setCustomValidity( 'wrong');
                                    app.util.Form.bootstrapValidate($form);
                                    app.gui.PageLoader.hide();
                                }
                                else
                                {
                                    app.core.View.toastSuccess( _lc( 'PASSWORD_SUCCESSFULLY_UPDATED' ) );
                                    app.core.Controller.redirect( '#/configuration/account' );
                                }
                            }

                        );
                    }
                });

                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};