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

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

app.state.Groups = function()
{
    var state = app.core.StateManager.createState( 'groups' );

    state.onEnter = function( p )
    {
        var $content = app.core.View.getContent(),
            groupsToJoin = [],
            groups = [];

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Account',
            'getAccountRelatedData',
            null,
            function( res )
            {
                if ( res && res.own_teams )
                {
                    for ( var rti = 0; rti < res.own_teams.length; rti++ )
                    {
                        groups.push( new app.model.Group( res.own_teams[ rti ] ) );
                    }
                }

                app.core.View.setContent(
                    app.core.View.getTemplate( 'groups', { groups: groups } )
                );

                $content.on( 'click', '[data-type="group-link"]', function()
                {
                    var groupId = $(this).attr( 'data-group-id' );
                    app.core.Controller.redirect( '#/group/' + groupId );
                });

                app.core.Rpc.call(
                    'Team',
                    'getAll',
                    null,
                    function( resFirst )
                    {
                        if ( resFirst && resFirst.groups && resFirst.groups.length )
                        {
                            for ( var gi = 0; gi < resFirst.groups.length; gi++ )
                            {
                                groupsToJoin.push( new app.model.Group( resFirst.groups[ gi ] ) );
                            }
                        }

                        $content.on( 'click', '[data-id="btn-search-group"]', function()
                        {
                            var $form = $content.find('[data-id="form-join-group"]').first(),
                                isValid = app.util.Form.bootstrapValidate( $form );

                            if ( isValid )
                            {
                                var groupCode = $form.find( '[data-id="input-public-group-id"]' ).first().val().toUpperCase(),
                                    isAlreadyInGroup = false,
                                    groupToJoin = null,
                                    gi;

                                // Check if user is already in group

                                for ( gi = 0; gi < groups.length; gi++ )
                                {
                                    if ( groups[ gi ].getPublicId() === groupCode )
                                    {
                                        isAlreadyInGroup = true;
                                        break;
                                    }
                                }

                                if ( true === isAlreadyInGroup )
                                {
                                    app.core.View.alert( 'Du bist bereits Mitglied in dieser Gruppe.' );
                                    return false;
                                }

                                for ( gi = 0; gi < groupsToJoin.length; gi++ )
                                {
                                    if ( groupsToJoin[ gi ].getPublicId() === groupCode )
                                    {
                                        groupToJoin = groupsToJoin[ gi ];
                                        break;
                                    }
                                }

                                if ( null === groupToJoin )
                                {
                                    app.core.View.alert( 'Es konnte keine Gruppe mit diesem Code gefunden werden.' );
                                }
                                else
                                {
                                    app.core.View.showModal(
                                    {
                                        title: 'Dieser Gruppe beitreten',
                                        body: app.core.View.getTemplate('groups-join-group-modal', { group : groupToJoin } ),
                                        onShow: function ()
                                        {
                                            var $modalRoot = app.core.View.getModalContent();

                                        },
                                        onConfirm : function()
                                        {
                                            app.core.View.closeModal();
                                            app.core.Rpc.call(
                                                'Team',
                                                'join',
                                                {
                                                    groupId : groupToJoin.getId()
                                                },
                                                function( joinResp )
                                                {
                                                    app.model.SessionUser.updateUserData( joinResp.profile, joinResp.profile_teams );
                                                    app.core.View.toastSuccess( 'Gruppe erfolgreich beigetreten.' );
                                                    app.core.Controller.reload();
                                                }
                                            );
                                        },
                                        okButtonText : 'Ja, Gruppe beitreten'
                                    });
                                }
                            }
                        });

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

    return state;
};