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

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

app.state.AuthLogin = function()
{
    var state = app.core.StateManager.createState( 'auth-login' );

    state.onEnter = function( p )
    {
        var $authContent = app.core.View.getAuthContent(),
            redirect = app.core.Controller.getGetParam( "redirect" ),
            theme = p.hasOwnProperty( 'theme' ) ? p.theme : null,
            bgImg = null;

        switch( theme )
        {
            case 'yoga':
                bgImg = 'img-system/themes/yoga-head-bg.jpg';
            break;

            case 'horse':
                bgImg = 'img-system/themes/daniil-vnoutchkov-FwK07kj-mTc-unsplash.jpg';
            break;

            case 'dog':
                bgImg = 'img-system/themes/DogHeader3.jpg';
            break;
        }

        if ( bgImg )
        {
            $( '[data-id="section-auth-bg-container"]' ).first().css( 'backgroundImage', 'url(' + bgImg + ')' );
        }

        app.core.View.setAuthContent(
            app.core.View.getTemplate( 'auth-login' )
        );

        $authContent.find( '[data-id="btn-back-to-login"]' ).first().click( function()
        {
            $authContent.find( '[data-id="section-not-validated"]' ).first().hide();
            $authContent.find( '[data-id="section-login"]' ).first().show();
        });

        $authContent.find( '[data-id="btn-login"]' ).first().click( function()
        {
            let $form = $authContent.find( '[data-id="form-login"]' ).first(),
                isValid = app.util.Form.bootstrapValidate( $form );

            if ( isValid )
            {
                $form.removeClass( 'was-validated' );
                app.core.Rpc.call(
                    'Auth',
                    'login',
                    {
                        email : $form.find( '[data-id="input-login-email"]' ).first().val(),
                        password : $form.find( '[data-id="input-login-password"]' ).first().val()
                    },
                    function( res )
                    {
                        if ( res )
                        {
                            if ( res.hasOwnProperty( 'error' ) &&
                                 res.error === 'not_validated' )
                            {
                                $authContent.find( '[data-id="section-login"]' ).first().hide();
                                $authContent.find( '[data-id="section-not-validated"]' ).first().show();
                            }
                            else
                            {
                                app.core.Session.init( res );
                                if ( app.core.Session.isLoggedIn() )
                                {
                                    app.core.Controller.disable();
                                    if ( redirect )
                                    {
                                        app.core.Controller.redirect( '#' + redirect );
                                    }
                                    else
                                    {
                                        app.core.Controller.redirect( '#/home' );
                                    }
                                    app.core.Controller.reload( true );
                                }
                                else
                                {
                                    app.core.View.alert( 'Bad response during login - user is not logged in.' );
                                }
                            }
                        }
                        else
                        {
                            app.core.View.alert( 'Bad response during login.' );
                        }
                    }
                    ,
                    function( err, code )
                    {
                        if( 404 == code)
                        {
                            app.core.View.toastError( _lc( "INVALID_LOGIN_CREDENTIALS" ) );
                        }
                        else
                        {
                            app.core.View.toastError( _lc( "GENERAL_SERVER_ERROR" ) );
                        }
                    }
                );
            }
            return false;
        });

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

    return state;
};