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

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

app.state.AuthJoinWeb = function()
{
    var state = app.core.StateManager.createState( 'auth-join-web' );

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Auth',
            'getTeamForJoin',
            { publicTeamId : publicTeamId },
            function( res )
            {
                app.core.View.setAuthContent(
                    app.core.View.getTemplate('auth-joinweb', { joinTeamData: res.team } )
                );

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

                    if ( isValid )
                    {
                        app.gui.PageLoader.show();
                        app.core.Rpc.call(
                            'Auth',
                            'joinTeam',
                            {
                                firstname : $form.find('[data-id="input-joinweb-firstname"]').first().val(),
                                lastname : $form.find('[data-id="input-joinweb-lastname"]').first().val(),
                                email: $form.find('[data-id="input-joinweb-email"]').first().val(),
                                password: $form.find('[data-id="input-joinweb-password"]').first().val(),
                                publicTeamId: publicTeamId
                            },
                            function (res)
                            {
                                // Similar code AuthStartJoin.js and AuthJoinWeb.js
                                if ( res && res.hasOwnProperty( 'account_validated' ) )
                                {
                                    app.core.Session.init( res );
                                    if ( app.core.Session.isLoggedIn() )
                                    {
                                        app.core.Controller.disable();
                                        app.core.Controller.redirect('#/home');
                                        app.core.Controller.reload( true );
                                    }
                                    else
                                    {
                                        app.gui.PageLoader.hide();
                                        app.core.View.alert( 'Bad response during login - user is not logged in.' );
                                    }
                                }
                                else
                                {
                                    app.gui.PageLoader.hide();
                                    $authContent.find( '[data-id="section-form-join"]' ).first().hide();
                                    $authContent.find( '[data-id="section-validation-info"]' ).first().show();
                                }
                            }
                        );
                    }
                });

                app.gui.PageLoader.hide();

                return false;
            }
            ,
            function( res )
            {
                console.log( res );
            }
        );
    }

    return state;
};