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

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

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

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

        app.core.View.setAuthContent(
            app.core.View.getTemplate( 'auth-register', { theme : theme } )
        );

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

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

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

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

            if ( isValid )
            {
                app.gui.PageLoader.show();

                app.core.Rpc.call(
                    'Auth',
                    'registerTrainer',
                    {
                        teamname : $form.find( '[data-id="input-register-teamname"]' ).first().val(),
                        firstname : $form.find('[data-id="input-register-firstname"]').first().val(),
                        lastname : $form.find('[data-id="input-register-lastname"]').first().val(),
                        email : $form.find( '[data-id="input-register-email"]' ).first().val(),
                        password : $form.find( '[data-id="input-register-password"]' ).first().val(),
                        industry : $form.find( '[data-id="select-register-industry"]' ).first().val(),
                        subscribedNews : $form.find( '[data-id="register-accept-newsletter"]' ).first().prop( 'checked' ) ? 1 : 0
                    },
                    function( res )
                    {
                        app.gui.PageLoader.hide();

                        if ( res.session === null )
                        {
                            $authContent.find( '[data-id="section-registration-form"]' ).first().hide();
                            $authContent.find( '[data-id="section-registration-done"]' ).first().show();
                        }
                        else
                        {
                            app.core.Session.init( res );
                            if ( app.core.Session.isLoggedIn() )
                            {
                                app.core.Controller.redirect( '#/home/new-group-created' );
                            }
                        }
                    },
                    function( err, code )
                    {
                        app.gui.PageLoader.hide();
                        $form.removeClass( 'was-validated' );

                        if ( code == 404  && err.gen_err == "email_already_exists" )
                        {
                            app.core.View.toastError( "EMAIL_AREADY_EXISTS_BUT_PASSWORD_IS_INCORRECT" );
                        }
                        else
                        {
                            app.core.View.toastError( "GENERAL_SERVER_ERROR" );
                        }
                    }
                );
            }
            return false;
        });

        app.gui.PageLoader.hide();

    };

    return state;
};