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

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

app.state.AuthValidateEmail = function()
{
    var state = app.core.StateManager.createState( 'auth-validate-email' );

    state.onEnter = function( p )
    {
        let $authContent = app.core.View.getAuthContent(),
            token = p.token || null;

        app.core.Rpc.call(
            'Auth',
            'getToken',
            {
                token: token
            },
            function( tokenData )
            {
                var actionToken  = new app.model.ActionToken( tokenData );

                // Note:
                // We perform this logic - because some members might use the email validation link from the mail
                // as a bookmark to the probuddy login
                if ( false === actionToken.isValid() )
                {
                    app.core.View.toastError( _lc( 'TOKEN_NOT_VALID_ANYMORE_PLEASE_LOGIN' ) );
                    app.core.Controller.redirect( '#/auth/login' );
                    return false;
                }

                app.core.Rpc.call(
                    'Auth',
                    'validateEmail',
                    {
                        token : token
                    },
                    function( res )
                    {
                        if ( res.result === "ok" )
                        {
                            app.core.Session.init( res );
                            app.core.View.setAuthContent(
                                app.core.View.getTemplate( 'auth-validate-email', { result : res.result } )
                            );
                            $authContent.find( '[data-id="btn-enter-probuddy"]' ).first().click( function()
                            {
                                app.core.Controller.disable();
                                /*
                                if ( res && res.hasOwnProperty( 'isNewAccount' ) && true == res.isNewAccount )
                                {
                                    app.core.Controller.redirect('#/home/new-member');
                                }
                                else
                                {
                                    app.core.Controller.redirect('#/home');
                                }
                                */
                                app.core.Controller.redirect('#/home');
                                app.core.Controller.reload( true );

                            });
                        }

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

    return state;
};