/**
 * Created by Benny on 12.10.2014.
 */
"use strict";

var TB = TB || {};

TB.Premium = {

    PACKAGE_STANDARD : 'STANDARD',
    PACKAGE_PREMIUM25 : 'PREMIUM_25',
    PACKAGE_PREMIUM50 : 'PREMIUM_50',
    PACKAGE_PREMIUM100 : 'PREMIUM_100',
    PACKAGE_PREMIUM250 : 'PREMIUM_250',
    PACKAGE_PREMIUM100_FREE : 'PREMIUM_100_FREE',

    isPremium : function( t )
    {
        var team = t || TB.Client.getCurrentTeam();

        if ( team.active_premium_package !== TB.Premium.PACKAGE_STANDARD )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    ,

    isValidPremiumPackage : function( p )
    {
        if ( p === TB.Premium.PACKAGE_STANDARD ||
             p === TB.Premium.PACKAGE_PREMIUM25 ||
             p === TB.Premium.PACKAGE_PREMIUM50 ||
             p === TB.Premium.PACKAGE_PREMIUM100 ||
             p === TB.Premium.PACKAGE_PREMIUM250 ||
             p === TB.Premium.PACKAGE_PREMIUM100_FREE )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    ,

    getName : function( premiumPackage )
    {
        var name = 'Standard';

        switch( premiumPackage )
        {
            case TB.Premium.PACKAGE_STANDARD:
                name = 'Standard';
            break;
            case TB.Premium.PACKAGE_PREMIUM25:
                name = 'Premium25';
            break;
            case TB.Premium.PACKAGE_PREMIUM50:
                name = 'Premium50';
            break;
            case TB.Premium.PACKAGE_PREMIUM100:
                name = 'Premium100';
            break;
            case TB.Premium.PACKAGE_PREMIUM250:
                name = 'Premium250';
            break;
            case TB.Premium.PACKAGE_PREMIUM100_FREE:
                name = 'Premium100 Free';
            break;
        }

        return name;
    }

    ,

    getPrice : function( premiumPackage )
    {
        var price;

        switch( premiumPackage )
        {
            case TB.Premium.PACKAGE_STANDARD:
                price = '0€';
            break;
            case TB.Premium.PACKAGE_PREMIUM25:
                price = '3,99€';
            break;
            case TB.Premium.PACKAGE_PREMIUM50:
                price = '4,99€';
            break;
            case TB.Premium.PACKAGE_PREMIUM100:
                price = '5,99€';
            break;
            case TB.Premium.PACKAGE_PREMIUM250:
                price = '9,99€';
            break;
            case TB.Premium.PACKAGE_PREMIUM100_FREE:
                price = 'kostenlos';
            break;
        }

        return price;

    }
    ,

    getTax : function( premiumPackage )
    {
        var tax;

        switch( premiumPackage )
        {
            case TB.Premium.PACKAGE_STANDARD:
                tax = '0€';
                break;
            case TB.Premium.PACKAGE_PREMIUM25:
                tax = '0,64€';
                break;
            case TB.Premium.PACKAGE_PREMIUM50:
                tax = '0,80€';
                break;
            case TB.Premium.PACKAGE_PREMIUM100:
                tax = '0,96€';
            break;
            case TB.Premium.PACKAGE_PREMIUM250:
                tax = '1,60€';
            break;
            case TB.Premium.PACKAGE_PREMIUM100_FREE:
                tax = '-';
            break;
        }

        return tax;

    }
    ,

    getStatus : function( status )
    {
        var txt = "Unbekannt";

        switch( status.toUpperCase() )
        {
            case 'PENDING':
                txt = 'anstehend';
            break;

            case 'ACTIVE':
                txt = 'aktiv';
            break;

            case 'CANCELLED':
                txt = 'storniert';
            break;

            case 'CLOSED':
                txt = 'beendet';
            break;

            case 'DELETED':
                txt = 'gelöscht';
            break;
        }

        return txt;
    },

    getStatusHtml : function( status )
    {
        var txt = '<span class="label label-default">unbekannt</span>';

        switch( status.toUpperCase() )
        {
            case 'PENDING':
                txt = '<span class="label label-primary">anstehend</span>';
                break;

            case 'ACTIVE':
                txt = '<span class="label label-success">aktiv</span>';
                break;

            case 'CANCELLED':
                txt = '<span class="label label-danger">storniert</span>';
                break;

            case 'CLOSED':
                txt = '<span class="label label-default">beendet</span>';
            break;

            case 'DELETED':
                txt = txt = '<span class="label label-danger">gelöscht</span>';
                break;
        }

        return txt;
    },

    getCurrentTeamPackage : function( t ) {
        var team = t || TB.Client.getCurrentTeam();

        if ( team && team.active_premium_package )
        {
            return team.active_premium_package;
        }
        else
        {
            return 'STANDARD';
        }
    },

    callPremium: function( control, action, params )
    {
        if ( TB.Premium.isPremium() )
        {
            TB.Controller.do( control, action, params );
        }
        else
        {
            TB.Premium.handleNoPremium();
        }
    }

    ,

    handleNoPremium : function()
    {
        if ( !TB.Env.isNativeMobile() )
        {
            TB.Ext.Alert.noPremiumBuy();
        }
        else
        {
            TB.Ext.Alert.noPremiumInfo();
        }
    }
    ,

    isPremiumPackageAvailableForTeam : function( profiles, premiumPackage )
    {
        var lenp = profiles.length,
            maxAllowed = TB.Premium.getMaxAllowedBuddies( premiumPackage );

        return ( lenp <= maxAllowed );
    }

    ,

    getPossiblePremiumPackagesForTeam: function( team, profiles )
    {
        var packages = [ TB.Premium.PACKAGE_PREMIUM100 ],
            pMax,
            pLength = profiles.length;

        pMax = TB.Premium.getMaxAllowedBuddies( TB.Premium.PACKAGE_PREMIUM50 );
        if ( pLength <= pMax )
        {
            packages.push( TB.Premium.PACKAGE_PREMIUM50 );
        }

        pMax = TB.Premium.getMaxAllowedBuddies( TB.Premium.PACKAGE_PREMIUM25 );
        if ( pLength <= pMax )
        {
            packages.push( TB.Premium.PACKAGE_PREMIUM25 );
        }

        return packages;
    }
    ,

    getRecommendedPremiumPackageForTeam : function( team, profiles )
    {
        var recPackage = TB.Premium.PACKAGE_PREMIUM100,
            pMax,
            pLength = profiles.length;

        // Recommend PREMIUM50
        pMax = TB.Premium.getMaxAllowedBuddies( TB.Premium.PACKAGE_PREMIUM50 );
        if ( (pLength + 5) <= pMax )
        {
            recPackage = TB.Premium.PACKAGE_PREMIUM50;
        }

        // Recommend PREMIUM25
        pMax = TB.Premium.getMaxAllowedBuddies( TB.Premium.PACKAGE_PREMIUM25 );
        if ( (pLength + 5) <= pMax )
        {
            recPackage = TB.Premium.PACKAGE_PREMIUM25;
        }

        return recPackage;
    }

    ,

    /**
     *
     * @param team
     * @returns {number}
     */
    getMaxAllowedBuddies : function( premiumPackage )
    {
        var maxNumBuddies = 0,
            premiumPackage = premiumPackage || null;

        if ( !premiumPackage ) {
            premiumPackage = TB.Premium.getCurrentTeamPackage();
        }

        switch( premiumPackage )
        {
            case TB.Premium.PACKAGE_STANDARD:
                maxNumBuddies = 10;
            break;

            case TB.Premium.PACKAGE_PREMIUM25:
                maxNumBuddies = 25;
            break;

            case TB.Premium.PACKAGE_PREMIUM50:
                maxNumBuddies = 50;
            break;

            case TB.Premium.PACKAGE_PREMIUM100:
                maxNumBuddies = 100;
            break;

            case TB.Premium.PACKAGE_PREMIUM250:
                maxNumBuddies = 350;
            break;

            case TB.Premium.PACKAGE_PREMIUM100_FREE:
                maxNumBuddies = 100;
            break;
        }

        return maxNumBuddies;
    }
    ,
    getMaxHighscoreLength : function() {
        if ( TB.Premium.isPremium() )
        {
            return 1000;
        }
        else
        {
            return 3;
        }
    }
    ,
    isFreemiumPackage : function( packageId )
    {
        if ( packageId === TB.Premium.PACKAGE_PREMIUM100_FREE )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};