/**
 * (c) by aheadware.com
 */
"use strict";

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

/**
 * Represents the session user
 */
app.model.SessionUser = (function()
{
    var userProfile = null,
        groups = [];

    return {
        init : function( up, t )
        {
            userProfile = new app.model.Profile( up );
            groups = [];
            if ( t && t.length > 0 )
            {
                for ( var ti = 0; ti < t.length; ti++ )
                {
                    groups.push( new app.model.Group( t[ ti ] ) );
                }
            }
            app.core.NotificationManager.init();
            app.core.Cache.set( 'general.hasLoggedInBefore', true );
            app.util.OneSignal.init();
        }
        ,
        destroy : function()
        {
            userProfile = null;
            groups = [];
            app.core.NotificationManager.destroy();
        }
        ,
        getUserProfile : function()
        {
            return userProfile;
        }
        ,
        updateUserData : function ( ud, t )
        {
            userProfile = new app.model.Profile( ud );
            groups = [];
            if ( t && t.length > 0 )
            {
                for ( var ti = 0; ti < t.length; ti++ )
                {
                    groups.push( new app.model.Group( t[ ti ] ) );
                }
            }
        },
        setUserProfile : function( newProfile )
        {
            userProfile = newProfile;
        }
        ,
        getAccountId : function()
        {
            if ( userProfile )
            {
                return userProfile.getAccountId();
            }
            else
            {
                return null;
            }
        }
        ,
        getProfileId : function()
        {
            if ( userProfile )
            {
                return userProfile.getId();
            }
            else
            {
                return null;
            }
        }
        ,
        isOwnProfile : function( profile )
        {
            return ( this.getProfileId() == profile.getId() );
        },
        getGroups : function()
        {
            return groups;
        }
        ,
        setGroupsByData : function ( gd )
        {
            groups = [];
            if ( gd && gd.length > 0 )
            {
                for ( var gdi = 0; gdi < gd.length; gdi++ )
                {
                    groups.push( new app.model.Group( gd[ gdi ] ) );
                }
            }
        }
        ,
        hasGroups : function()
        {
            return ( 0 < groups.length );
        }
        ,
        getGroupById: function( groupId )
        {
            var g = null;

            for ( var gi = 0; gi < groups.length; gi++ )
            {
                if ( groups[ gi ].getId() == groupId )
                {
                    g = groups[ gi ];
                    break;
                }
            }

            return g;
        }
        ,
        getAdminGroups : function()
        {
            var ag = [];

            for ( var gi = 0; gi < groups.length; gi++ )
            {
                if ( this.canAdministrateTeam( groups[ gi ].getId() ) )
                {
                    ag.push( groups[ gi ] );
                }
            }

            return ag;
        }
        ,
        isAdminOfAnyTeam : function()
        {
            var isAdmin = false,
                teamIds = ( userProfile ) ? this.getUserProfile().getTeamIds() : null;

            if ( !teamIds )
            {
                return false;
            }

            for ( var ti = 0; ti < teamIds.length; ti++ )
            {
                if ( this.canAdministrateTeam( teamIds[ti]))
                {
                    isAdmin = true;
                    break;
                }
            }

            return isAdmin;
        },
        canAccessProfile : function( profile )
        {
            var canAccess = false,
                selfTeamIds = this.getUserProfile().getTeamIds(),
                profileTeamIds = profile.getTeamIds(),
                teamIds = selfTeamIds.filter(value => -1 !== profileTeamIds.indexOf(value));

            for ( var ti = 0; ti < teamIds.length; ti++ )
            {
                if ( this.canAdministrateTeam( teamIds[ ti ] ) )
                {
                    canAccess = true;
                    break;
                }

                if ( false === profile.isAnonymousInGroup( teamIds[ ti ] ) )
                {
                    canAccess = true;
                    break;
                }
            }

            return canAccess;
        }
        ,
        isGroupOwnerOfATeam : function()
        {
            var isGroupOwner = false,
                userTeamData = this.getUserProfile().getGroupData();

            for ( var ti = 0; ti < userTeamData.length; ti++ )
            {
                if ( userTeamData[ ti ].role === "trainer" )
                {
                    isGroupOwner = true;
                    break;
                }
            }

            return isGroupOwner;
        }
        ,
        canAdministrateProfile : function( p )
        {
            var canAdmin = false,
                teamIds = p.getTeamIds();

            for ( var ti = 0; ti < teamIds.length; ti++ )
            {
                if ( this.canAdministrateTeam( teamIds[ ti ] ) )
                {
                    canAdmin = true;
                    break;
                }
            }

            return canAdmin;
        }
        ,
        canAdministrateTeam : function( teamId )
        {
            var canAdministrate = false,
                userTeamData = this.getUserProfile().getGroupData();

            for ( var ti = 0; ti < userTeamData.length; ti++ )
            {
                if ( userTeamData[ ti ].team_id == teamId )
                {
                    canAdministrate = ( userTeamData[ ti ].role === "trainer" || "cotrainer" === userTeamData[ ti ].role ) ? true : false;
                    break;
                }
            }

            return canAdministrate;
        }
    };
})();
