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

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

/**
 * Profile Model
 */
app.model.Profile = function( data, groupsData )
{
    var id = data.id,
        publicId = data.public_id || null,
        accountId = data.account_id || null,
        gender = data.gender,
        picUrl = data.pic_url,
        status = data.status || null,
        firstName = ( data.first_name && data.first_name.length ) ? data.first_name : null,
        lastName = ( data.last_name && data.last_name.length ) ? data.last_name : null,
        birthday = data.birthday_dt || null,
        street = data.street,
        zipCode = data.zip_code,
        city = data.city,
        country = ( data && data.hasOwnProperty( 'country' ) && data.country && data.country.length ) ? data.country : null,
        phone = data.phone,
        mobile = data.mobile,
        misc = data.misc,
        teamData = data.teams_js || [],
        groups = [],
        teamProps = data.team_prop_js || {},
        teamCategoryIds = data.team_category_ids_js || [],
        createdDt = data.created_dt,
        // Note: Emails are optional
        email = null;

    if ( "object" === typeof( teamCategoryIds ) )
    {
        teamCategoryIds = Object.values( teamCategoryIds );
    }

    if ( groupsData && groupsData.length > 0 )
    {
        for ( var ptdi = 0; ptdi < groupsData.length; ptdi++ )
        {
            groups.push( new app.model.Group( groupsData[ ptdi ] ) );
        }
    }

    this.setEmail = function( e)
    {
        email = e;
    };

    this.getEmail = function()
    {
        return email;
    };

    this.isAdminOfGroup = function( groupId )
    {
        let isAdmin = false;

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

        return isAdmin;
    };

    this.isAdminInAGroupOfGiveProfile = function( p )
    {
        var isAdmin = false,
            teamIds = p.getTeamIds();

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

        return isAdmin;
    };

    this.getNumberOfGroupsWithAdminRole = function()
    {
        var num = 0;

        for ( var ti = 0; ti < teamData.length; ti++ )
        {
            if ( true === this.isAdminOfGroup( teamData[ ti ].team_id ) )
            {
                num++;
            }
        }

        return num;
    };

    this.getStatus = function()
    {
        var statusTxt= _lc( 'NO_PROFILE_STATUS' );
        if ( status && status.length > 0 )
        {
            statusTxt = status;
        }
        return statusTxt;
    };

    this.isOwnerOfGroup = function( groupId )
    {
        var gd = this.getGroupData( groupId );
        return ( gd && "trainer" === gd.role );
    };

    this.isAnonymousInGroup = function( groupId )
    {
        let isAnonymous = false;

        for ( var ti = 0; ti < teamData.length; ti++ )
        {
            if ( teamData[ ti ].team_id == groupId )
            {
                isAnonymous = ( 1 == teamData[ ti ].is_anonymous );
                break;
            }
        }

        return isAnonymous;
    };

    this.isInGroup = function( groupId )
    {
        var is = false;

        for ( var ti = 0; ti < teamData.length; ti++ )
        {
            if ( groupId == teamData[ ti ].team_id )
            {
                is = true;
                break;
            }
        }

        return is;
    };

    /**
     *
     * @returns {Array}
     */
    this.getTeamIds = function()
    {
        var teamIds = [];

        for ( var ti = 0; ti < teamData.length; ti++ )
        {
            teamIds.push( teamData[ ti ].team_id );
        }

        return teamIds;
    };

    this.getGroupsByRole = function( role )
    {
        var groupData = [];

        for ( var ti = 0; ti < teamData.length; ti++ )
        {
            if ( teamData[ ti ].role === role )
            {
                groupData.push( teamData[ ti ] );
            }
        }

        return groupData;
    };

    this.getGroupData = function( groupId )
    {
        if ( !groupId )
        {
            return teamData;
        }

        var gd = null;

        for ( var gi = 0; gi < teamData.length; gi++ )
        {
            if ( teamData[ gi ].team_id == groupId )
            {
                gd = teamData[ gi ];
                break;
            }
        }

        return gd;
    };

    this.setGroupData = function( gd )
    {
        teamData = gd;
    };

    this.isOwn = function()
    {
        return ( this.getId() == app.model.SessionUser.getUserProfile().getId() );
    };

    /**
     * @returns {*}
     */
    this.isAccessible = function()
    {
        if ( true === this.isOwn() ) return true;

        return app.model.SessionUser.canAccessProfile( this );
    };

    this.getRoleInGroup = function( groupId )
    {
        var role = null,
            gd = this.getGroupData( groupId );

        role = gd ? gd.role : null;

        return role;
    };

    this.getAccountId = function()
    {
        return accountId;
    };

    this.getContractInGroup = function( groupId )
    {
        var contract = null,
            gd = this.getGroupData( groupId );

        contract = ( gd && gd.hasOwnProperty( 'contract' ) ) ? gd.contract : null;

        return contract;
    };

    this.getContractMomentInGroup = function( groupId )
    {
        var contractMoment = null,
            gd = this.getGroupData( groupId );

        contractMoment = ( gd && gd.hasOwnProperty( 'contract_date' ) ) ? app.util.Helper.getMomentFromUTCTime( gd.contract_date ) : null;

        return contractMoment;
    };

    this.isOlderThanTwoWeeks = function()
    {
        let mCreated = app.util.Helper.getMomentFromUTCTime( createdDt ),
            m14 = moment().subtract( 14, 'days' );
        return mCreated.isBefore( m14 );
    };

    this.getRolenameInGroup = function( groupId )
    {
        var gd = this.getGroupData( groupId ),
            roleName = _lc( 'UNKNOWN');

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

                case 'cotrainer':
                    roleName = _lc( 'GROUP_ADMIN' );
                break;

                case 'player':
                    roleName = _lc( 'GROUP_MEMBER' );
                break;
            }
        }
        return roleName;
    };

    this.getMomentJoinInGroup = function( groupId )
    {
        var gd = this.getGroupData( groupId ),
            momentJoin = null;

        if ( gd && gd[ 'join_dt'] )
        {
            momentJoin = app.util.Helper.getMomentFromUTCTime( gd[ 'join_dt' ] );
        }

        return momentJoin;
    };

    this.getId = function()
    {
        return id;
    };

    this.getMomentBirthday = function()
    {
        var mBirthday = null;
        if ( birthday )
        {
            mBirthday = app.util.Helper.getMomentFromUTCTime( birthday );
        }
        return mBirthday;
    };

    this.getStreet = function()
    {
        return street;
    };

    this.getCountry = function()
    {
        return country;
    };

    this.getPublicId = function()
    {
        return publicId;
    };

    this.getZipCode = function()
    {
        return zipCode;
    };

    this.getCity = function()
    {
        return city;
    };

    this.getPhone = function()
    {
        return phone;
    };

    this.getMobile = function()
    {
        return mobile;
    };

    this.getMisc = function()
    {
        return misc;
    };

    this.getProfileImg = function( groupId)
    {
        if ( groupId && false === this.isInGroup( groupId ) )
        {
            return './img-system/user-icon-default.png';
        }

        if ( picUrl )
        {
            return picUrl;
        }
        else
        {
            return './img-system/user-icon-default.png';
        }
    };

    this.getProfileBackgroundImg = function()
    {
        return '1900x1200_img_5.jpg';
    };

    this.getFirstName = function()
    {
        return firstName;
    };

    this.getLastName = function()
    {
        return lastName;
    };

    this.getName = function( groupId )
    {
        var name = '';

        if ( groupId && true === this.isAnonymousInGroup( groupId ) && false === this.isAccessible() )
        {
            name = _lc( 'ANONYMOUS' );
            return '<i>' + name + '</i>';
        }

        if  ( groupId &&
             false === this.isInGroup( groupId ) )
        {
            if ( false === app.model.SessionUser.getUserProfile().isAdminOfGroup( groupId ) )
            {
                name = '[ausgetreten]';
                return '<i>' + name + '</i>';
            }
            else
            {
                name = '(gelöscht) ';
            }
        }

        if ( firstName )
        {
            name += firstName;
        }

        if ( lastName )
        {
            if ( name.length > 0  )
            {
                name = name + " " + lastName;
            }
            else
            {
                name = lastName;
            }
        }

        if ( name.length === 0 )
        {
            return _lc( 'UNKNOWN_NAME' );
        }
        else
        {
            return name;
        }
    };

    this.getLastFirstName = function()
    {
        var lastFirstName = '';

        if ( false === this.isAccessible() )
        {
            lastFirstName = _lc( 'ANONYMOUS' );
            return '<i>' + lastFirstName + '</i>';
        }

        if ( lastName )
        {
            lastFirstName = lastName;
            if ( firstName )
            {
                lastFirstName += ", " + firstName;
            }
        }
        else if ( firstName )
        {
            lastFirstName = firstName;
        }
        else
        {
            lastFirstName = _lc( 'UNKNOWN_NAME' );
        }
        return lastFirstName;
    };

    this.getTeamProp = function( category )
    {
        if ( category )
        {
            if ( teamProps.hasOwnProperty( category ) )
            {
                return teamProps[ category ];
            }
            else
            {
                return null;
            }
        }

        return teamProps;
    };

    this.getGroupCourseCategoryIds = function()
    {
        return teamCategoryIds;
    };

    this.isInGroupCourseCategoryId = function( cid )
    {
        return ( -1 < teamCategoryIds.indexOf( cid ) );
    };

    /***
     *
     * @param c
     * @returns {boolean}
     */
    this.isInGroupCategory = function( c )
    {
        var ret = false;

        if ( groups && groups.length > 0 )
        {
            for ( var gi = 0; gi < groups.length; gi++ )
            {
                if ( groups[ gi ].getCategory() === c )
                {
                    ret = true;
                    break;
                }
            }
        }

        return ret;
    };

    /**
     *
     * @param category
     * @param property
     * @returns {*}
     */
    this.getCustomGroupProperty = function( category, property )
    {
        if ( teamProps.hasOwnProperty( category ) && teamProps[ category ].hasOwnProperty( property ) )
        {
            return teamProps[ category ][  property ];
        }
        else
        {
            return null;
        }
    };

    this.hasValidAddressData = function()
    {
        if ( !street || 0 === street.length ||
             !zipCode || 0 === zipCode.length ||
             !city || 0 === city.length ||
             !country || 0 === country.length )
        {
            return false;
        }
        else
        {
            return true;
        }
    };

    this.getCountryName = function()
    {
        let cn = null;
        if ( country )
        {
            let countries = app.core.App.getCountries();
            for( let ci = 0; ci < countries.length; ci++ )
            {
                if ( countries[ ci ].key == country )
                {
                    cn = countries[ ci ].name;
                    break;
                }
            }
        }
        return cn;
    }
};