const UserProfile = {
    create : function( p )
    {
        this.contextGroupId = null;
        Object.assign( this, p.getAll() );
        this.roleName = 'Unbekannt';
        this.teamData = p.get( 'teams_js' );
    },
    getName : function( lastNameFirst = false )
    {
        let name = '';

        if ( true  === lastNameFirst )
        {
            name = this.last_name;
            if ( this.first_name )
            {
                name += ', ' + this.first_name;
            }
        }
        else
        {
            name = this.first_name ? ( this.first_name + ' ' ) : '';
            if ( this.last_name )
            {
                name += this.last_name;
            }
        }
        return name;
    },
    get : function( prop, defValue = null )
    {
        if ( this.hasOwnProperty( prop ) )
        {
            return this[ prop ];
        }
        else
        {
            return defValue;
        }
    },
    getAddress : function()
    {
        let add = '';
        if ( this.street )
        {
            add += this.street;
        }
        if ( this.zip_code )
        {
            if ( add.length > 0 )
            {
                add += ', ';
            }
            add += this.zip_code;
        }
        if ( this.city )
        {
            if ( add.length > 0 )
            {
                add += ', ';
            }
            add += this.city;
        }
        return add;
    },
    getPhoneMobile : function()
    {
        let pm = '';
        if ( this.phone )
        {
            pm += this.phone;
        }
        if ( this.mobile )
        {
            if ( pm.length > 0 )
            {
                pm += ', ';
            }
            pm += this.mobile;
        }
        return pm;
    },
    setContextGroupId : function( groupId )
    {
        this.contextGroupId = groupId;
        this.roleName = this.getRoleName();
    },
    getGroupData : function( groupId )
    {
        var gd = null;
        if ( this.teamData && this.teamData.length )
        {
            for ( var gi = 0; gi < this.teamData.length; gi++ )
            {
                if ( this.teamData[ gi ].team_id == groupId )
                {
                    gd = this.teamData[ gi ];
                    break;
                }
            }
        }
        return gd;
    },
    getRoleName : function()
    {
        var gd = this.getGroupData( this.contextGroupId ),
            roleName = _lc( 'UNKNOWN');

        if ( gd )
        {
            switch( gd[ 'role'] )
            {
                case 'trainer':
                    roleName = 'Gruppeninhaber';
                    break;

                case 'cotrainer':
                    roleName = 'Admin';
                    break;

                case 'player':
                    roleName = 'Mitglied';
                    break;
            }
        }
        return roleName;
    },
    isAdminOfGroup : function( groupId )
    {
        let isAdmin = false;

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

        return isAdmin;
    },
    isGroupOwner : function( groupId )
    {
        let gId = groupId || this.contextGroupId,
            isGroupOwner = false;

        for ( var ti = 0; ti < this.teamData.length; ti++ )
        {
            if ( this.teamData[ ti ].team_id == gId )
            {
                isGroupOwner =  ( "trainer" === this.teamData[ ti ].role ) ? true : false;
                break;
            }
        }

        return isGroupOwner;
    },
    hasManagerAccess : function( groupId )
    {
        let gId = +groupId || +this.contextGroupId,
            hasManagerAccess = false;

        if ( true === this.isGroupOwner( groupId ) )
        {
            return true;
        }

        if ( this.manager_access_ids_js && this.manager_access_ids_js.length > 0)
        {
            hasManagerAccess = -1 < this.manager_access_ids_js.indexOf( gId );
        }

        return hasManagerAccess;
    },
    destroy : function()
    {

    }
};

export { UserProfile };