<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\Notifications;

use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Converter;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Unit;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Groups\Constants as GroupConstants;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Arrays;

/**
 * Description of StepsConfiguration
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class StepsConfiguration
{

    public static function calculateConfigurationByUnitsForMin($configuration)
    {
        $out = [];
        foreach ($configuration as $key => $conf)
        {
            $unit = $conf['unit'] == GroupConstants::PERCENTAGE ? GroupConstants::PERCENTAGE : GroupConstants::MB;
            $out[$unit][$conf['id']] = Unit::calculateByUnit($conf['unit'], $conf['value']);
        }
        return $out;
    }

    public static function calculateConfigurationByUnits($configuration, $usage, $kind)
    {
        foreach ($configuration as $key => &$conf)
        {
            $conf['value'] = Unit::calculateByUnit($conf['unit'], $conf['value'], $usage[$kind.'limit']);
            $conf['unit']  = $conf['unit'] == GroupConstants::PERCENTAGE ? GroupConstants::PERCENTAGE : GroupConstants::MB;
        }
        return $configuration;
    }

    public static function calculateByUnitsAndGetMin($configuration)
    {
        $out = [];
        foreach ($configuration as $key => $conf)
        {
            $out[$key] = Arrays::getMinInArray(self::calculateConfigurationByUnitsForMin($conf));
        }
        return $out;
    }

    public static function getMaxFromCalculatedByUnitsIfHostingUsageIsHigherThanDeclared($configuration, $usage, $kind)
    {
        $configuration = self::calculateConfigurationByUnits($configuration, $usage, $kind);
        return self::findBestStepForTiered($configuration, $usage[$kind], $usage[$kind.'limit']);
    }

    public static function findBestStepForTiered($configuration, $usage, $limit)
    {
        if(empty($configuration))
        {
            return [];
        }

        $best = $configuration[0];
        $best['value'] = -1;

        foreach ($configuration as $conf)
        {
            if($best['value'] < $conf['value'] && $conf['value'] < $usage )
            {
                $best = $conf;
            }
        }

        $best['usage'] = $usage;
        $best['usageUnit'] = GroupConstants::MB;

        if($best['unit'] == GroupConstants::PERCENTAGE && $limit != 0)
        {
            $best['usageUnit'] = GroupConstants::PERCENTAGE;
            $best['value'] = (int) Converter::valueToPercent($best['value'], $limit);
            $best['usage'] = (int) Converter::valueToPercent($best['usage'], $limit);
        }
        return $best;
    }

    public static function findBestStep($configuration, $value)
    {
        if(empty($configuration))
        {
            return [];
        }
        $usage = Converter::toTypeAndValue($value);
        $choiced = $configuration[0];
        $choiced['value'] = 0;
        foreach($configuration as $conf)
        {
            if($conf['value'] < $usage['usage'] && $conf['value'] > $choiced['value'])
            {
                $choiced = $conf;
            }
        }
        return array_merge($choiced, $usage);
    }


}
