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

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

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

    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-start-join', { joinTeamData: res.team } )
                );

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

                    if ( isValid )
                    {
                        app.core.Rpc.call(
                            'Auth',
                            'joinTeam',
                            {
                                firstname : $form.find('[data-id="input-startjoin-firstname"]').first().val(),
                                lastname : $form.find('[data-id="input-startjoin-lastname"]').first().val(),
                                email: $form.find('[data-id="input-startjoin-email"]').first().val(),
                                password: $form.find('[data-id="input-startjoin-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;
};