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

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

app.state.GroupMembers = function()
{
    var state = app.core.StateManager.createState( 'group-members' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent(),
            groupId = p.groupId;

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Team',
            'getDetails',
            { teamId : groupId },
            function( res )
            {

                var group = new app.model.Group( res.team ),
                    currentProfile = null,
                    member = null,
                    members = [];

                for ( var mi = 0; mi < res.members.length; mi++ )
                {
                    member = new app.model.Profile( res.members[ mi ] );
                    members.push( member );
                    if ( member.getId() == app.model.SessionUser.getProfileId() )
                    {
                        currentProfile = member;
                    }
                }

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'group-members',
                        {
                            members : members,
                            group : group,
                            currentProfile : currentProfile
                        } )
                );

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

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

    return state;
};