const User = {
    create : function( p = {} )
    {
        this.observables({
            sessionId : this.storage.get( 'sessionId' )
        });

        this.userProfile = null;
        this.activeGroupId = this.storage.get( 'activeGroupId' );
        this.groups = [];
        this.roles = [];
        this.zContracts = [];
    },

    destroy : function()
    {
        this.deinit();
    },

    init : function( sessionId, userProfile, groups, zContracts )
    {
        this.groups = [];
        this.userProfile = this.createInstance( 'UserProfile', userProfile );
        this.storage.set( 'sessionId', sessionId );
        this.zContracts = [];
        if ( zContracts && zContracts.length > 0 )
        {
            for ( let zi = 0; zi < zContracts.length; zi++ )
            {
                this.zContracts.push( this.createInstance( 'ZContract', zContracts[ zi ] ) );
            }
        }

        if ( groups && groups.length )
        {
            let g, doesGroupIdExsits = false;
            for ( let gi = 0; gi < groups.length; gi++ )
            {
                g = this.createInstance( 'Group', groups[ gi ] );
                if ( g.id == this.activeGroupId )
                {
                    doesGroupIdExsits = true;
                }
                this.groups.push( g );
            }
            if ( false === doesGroupIdExsits )
            {
                this.activeGroupId = this.groups[ 0 ].id;
            }
        }

        this.sessionId = sessionId;
    },

    canAccessContractExtension : function()
    {
        let canAccess = false;

        for ( let zi = 0; zi < this.zContracts.length; zi++ )
        {
            if ( this.zContracts[ zi ].isValid() &&
                 this.zContracts[ zi ].fk_TeamID == this.activeGroupId &&
                 this.zContracts[ zi ].product_type === 'Vertrag' )
            {
                canAccess = true;
                break;
            }
        }

        return canAccess;
    },

    deinit : function()
    {
        this.storage.remove( 'sessionId' );
        this.storage.remove( 'activeGroupId' );
        this.groups = [];
        this.userProfile = null;
        this.sessionId = null;
        this.activeGroupId = null;
    },

    getActiveGroup : function()
    {
        var ag = null;
        for ( var gi = 0; gi < this.groups.length; gi++ )
        {
            if ( this.groups[ gi ].getId() == this.activeGroupId )
            {
                ag = this.groups[ gi ];
                break;
            }
        }
        return ag;
    },

    setActiveGroupId : function( activeGroupId )
    {
        this.storage.set( 'activeGroupId', activeGroupId );
        this.activeGroupId = activeGroupId;
    },

    canAdministrateGroup : function( groupId )
    {
        var canAdministrate = false;

        if ( this.userProfile && this.userProfile.hasOwnProperty( 'teams_js' ) )
        {
            for ( var ti = 0; ti < this.userProfile.teams_js.length; ti++ )
            {
                if ( this.userProfile.teams_js[ ti ].team_id == groupId &&
                    ( this.userProfile.teams_js[ ti ].role === 'trainer' || this.userProfile.teams_js[ ti ].role === 'cotrainer' ) )
                {
                    canAdministrate = true;
                    break;
                }
            }
        }

        return canAdministrate;
    },

    hasTestedContractModule : function()
    {
        let hasTested = false;

        for ( let zi = 0; zi < this.zContracts.length; zi++ )
        {
            if ( this.zContracts[ zi ].fk_TeamID == this.activeGroupId &&
                 this.zContracts[ zi ].product_type === 'Vertrag' &&
                 this.zContracts[ zi ].status === 'Test Vollversion' )
            {
                hasTested = true;
                break;
            }
        }

        return hasTested;
    },

    logout : function()
    {
        this.deinit();
        this.app.UI.onUserUpdate();
        this.redirect( 'auth/login' );
    },

    isLoggedIn : function()
    {
        return ( this.sessionId !== null );
    },

    hasRole : function( role )
    {
        return ( -1 < this.roles.indexOf( role ) );
    }

};

export { User };