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

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

/**
 * Group Model
 */
app.model.Group = function( data, memberData )
{
    var id = data.id,
        publicId = data.public_id,
        name = data.display_name,
        category = data.category,
        description = data.description || null,
        contactInformation = data.contact_information || null,
        emblemUrl = data.emblem_url || null,
        emblem_cloud_id = data.emblem_cloud_id || null,
        memberData = memberData || null,
        members = [],
        courseCategories = data.course_categories_js || null,
        mCreated = app.util.Helper.getMomentFromUTCTime( data.created_dt ),
        mLastUpdated = app.util.Helper.getMomentFromUTCTime( data.last_updated_dt );

    if ( courseCategories && courseCategories.length)
    {
        courseCategories.sort( app.util.Helper.dynamicSort( 'name' ) );
    }

    if ( memberData && memberData.length>  0 )
    {
        for ( var mi = 0; mi < memberData.length; mi++ )
        {
            members.push( new app.model.Profile( memberData[ mi ] ) );
        }
    }

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

    this.getName = function()
    {
        return name;
    };

    this.getCategory = function()
    {
        return category;
    };

    this.getCategoryImageUrl = function()
    {

    };

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

    this.getEmblemUrl = function()
    {
        return emblemUrl ? emblemUrl : './img-system/group-emblem-default.png';
    };

    this.getBackgroundImgUrl = function()
    {
        var imgUrl = '';

        switch ( this.getCategory() )
        {
            case 'YOGA':
                imgUrl = 'rawpixel-771284-unsplash.jpg';
            break;

            case 'DOGSCHOOL':
                imgUrl = 'bryan-hanson-1302169-unsplash.jpg';//'kevin-laminto-621654-unsplash.jpg';
            break;

            case 'FITNESS':
                imgUrl = 'bruce-mars-556415-unsplash.jpg';
            break;

            case 'CHARITY':
            break;

            default:
                imgUrl = 'alvaro-reyes-507651-unsplash.jpg';
        }

        return imgUrl;
    };

    this.getDescription = function()
    {
        return description;
    };

    this.getContactInformation = function()
    {
        return contactInformation;
    };

    /**
     *
     * @returns {*}
     */
    this.getCourseCategories = function()
    {
        return ( courseCategories && courseCategories.length ) ? courseCategories : [];
    };

    this.getAdminCourseCategoriesForProfile = function( ap )
    {
        var cgs = this.getCourseCategories(),
            apcgs;

        if ( ap.isOwnerOfGroup( this.getId() ) )
        {
            return cgs;
        }
        else if ( ap.isAdminOfGroup( this.getId() ) )
        {
            apcgs = [];
            for ( var cgsi = 0; cgsi < cgs.length; cgsi++ )
            {
                if ( ap.isInGroupCourseCategoryId( cgs[ cgsi ].id ) )
                {
                    apcgs.push( cgs[ cgsi ] );
                }
            }
            return apcgs;
        }
        else
        {
            return [];
        }
    };

    this.getCourseCategoriesForProfile = function ( p )
    {
        var pccs = [],
            cgs = this.getCourseCategories(),
            profileCourseCategories = p.getGroupCourseCategoryIds();

        if ( p.isOwnerOfGroup( this.getId() ) )
        {
            pccs = cgs;
        }
        else
        {
            for ( var cgsi = 0; cgsi < cgs.length; cgsi++ )
            {
                if ( -1 < profileCourseCategories.indexOf( cgs[ cgsi ].id ) )
                {
                    pccs.push( cgs[ cgsi ] );
                }
            }
        }

        return pccs;
    };

    /**
     *
     * @param id
     * @returns {*}
     */
    this.getCourseCategoryById = function( id )
    {
        var cgs = this.getCourseCategories(),
            cg = null;

        for ( var cgsi = 0; cgsi < cgs.length; cgsi++ )
        {
            if ( cgs[ cgsi ][ 'id' ] === id )
            {
                cg = cgs[ cgsi ];
                break;
            }
        }
        return cg;
    };

    this.getCourseCategoryNameById = function( id )
    {
        var cg = this.getCourseCategoryById( id );

        if ( cg && cg.hasOwnProperty( 'name' ) )
        {
            return cg.name;
        }
        else
        {
            return null;
        }
    };

    this.getCourseCategoryMemberCountById = function( cid )
    {
        var cnt = 0;

        for ( var mi = 0; mi < members.length; mi++ )
        {
            if ( members[ mi ].isInGroupCourseCategoryId( cid ) )
            {
                cnt++;
            }
        }

        return cnt;
    };


    this.isDefaultCategoryId = function( cid )
    {
        var cg = this.getCourseCategoryById( cid ),
            isDefault = false;

        if ( cg )
        {
            isDefault = cg.id ===  ( id + '-0' );
        }

        return isDefault;
    };

    this.getMemberById = function( mid )
    {
        var m = null;
        for ( var mi = 0; mi < members.length; mi++ )
        {
            if ( members[ mi ].getId() == mid )
            {
                m = members[ mi ];
                break;
            }
        }

        return m;
    };

    this.getMembers = function()
    {
        return members;
    };
};