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

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

/**
 * Chat Model
 */
app.model.Contract = function( data )
{
    var id = data.id,
        profileId = data.profile_id,
        profileData = data.profile_data_js,
        contracteeData = data.contractee_data_js,
        teamId = data.team_id,
        name = data.name,
        type = data.type,
        currency = data.currency,
        currentPriceFactor = data.current_price_factor,
        maxPriceFactor = data.max_price_factor,
        price = data.price,
        pricePayed = data.price_payed,
        momentStart = moment( data.start_date, 'YYYY-MM-DD' ),
        momentEnd = moment( data.end_date, 'YYYY-MM-DD' ),
        momentPaymentDue = moment( data.payment_due_date, 'YYYY-MM-DD' ),
        state = data.state,
        momentCreation = app.util.Helper.getMomentFromUTCTime(data.created_dt);

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

    this.getProfileData = function( key )
    {
        if ( key   )
        {
            if ( profileData && profileData.hasOwnProperty( key ) )
            {
                return profileData[ key ];
            }
            else
            {
                return null;
            }
        }
        else
        {
            return profileData;
        }
    };

    this.getProfileCountry = function()
    {
        let cn = app.model.Countries.getNameByKey( this.getProfileData( 'country' ) );
        if ( null === cn )
        {
            cn = 'Deutschland';
        }
        return cn;
    };

    this.getContracteeData = function( key )
    {
        if ( key )
        {
            if ( contracteeData && contracteeData.hasOwnProperty( key ) )
            {
                return contracteeData[ key ];
            }
            else
            {
                return null;
            }
        }
        else
        {
            return contracteeData;
        }
    };

    this.getContracteeCountry = function()
    {
        let cn = app.model.Countries.getNameByKey( this.getContracteeData( 'country' ) );
        if ( null === cn )
        {
            cn = 'Deutschland';
        }
        return cn;
    };

    this.getProfileId = function()
    {
        return profileId;
    };

    this.getTeamId = function()
    {
        return teamId;
    };

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

    this.getType = function()
    {
        return type;
    };

    this.isFlatrate = function()
    {
        return ( type === 'flatrate' );
    };

    this.getCurrency = function()
    {
        return currency;
    };

    this.getCurrentPriceFactor = function()
    {
        return ( currentPriceFactor / 100 ).toFixed( 2 );
    };

    this.getMaxPriceFactor = function()
    {
        return ( maxPriceFactor / 100 ).toFixed( 2 );
    };

    this.getPrice = function()
    {
        return ( price / 100 ).toFixed( 2 ).replace( '.', ',' );
    };

    this.getPricePayed = function()
    {
        return ( pricePayed / 100 ).toFixed( 2 ).replace( '.', ',' );
    };

    this.isPayed = function()
    {
        return ( pricePayed >= price );
    };

    this.getMomentOfStart = function()
    {
        return momentStart;
    }

    this.getMomentOfEnd = function()
    {
        return momentEnd;
    };

    this.getMomentOfPaymentDue = function()
    {
        return momentPaymentDue;
    };

    this.isRunning = function()
    {
        var momentNow = moment();
        return  ( momentNow.isAfter( momentStart ) && momentNow.isBefore( momentEnd ) );
    };

    this.isPaymentDue = function()
    {
        return ( false === this.isPayed() && moment().isAfter( momentPaymentDue ) );
    };

    this.getOpenPaymentAmount = function()
    {
        return ( ( price - pricePayed ) / 100 ).toFixed( 2 ).replace( '.', ',' );
    };

    this.getState = function()
    {
        return state;
    };

    this.getMomentOfCreation = function()
    {
        return momentCreation;
    };
};