<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Types;

use ModulesGarden\HostingQuotaNotifications\App\Helper\Migration\Groups\Group;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Queries\Tiered as QueriesTiered;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Notifications\StepsConfiguration;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Actions;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Types\Interfaces\TypeMessages;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Groups\Constants as GroupConstants;

/**
 * Description of Tiered
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Tiered implements TypeMessages
{
    public function getMessagesIfDeclaredLimitIsReached($minValues, $products)
    {

        $out = [];
        foreach ($minValues as $key => $value)
        {
            if (!$value)
            {
                continue;
            }
            $result = QueriesTiered::checkForMessages($value, $products, $key);
            $out[$key] = $this->mergeSameServices($result);
        }

        return $out;
    }

    public function mergeSameServices($result)
    {
        $out = $result[GroupConstants::MB];
        foreach ($result[GroupConstants::PERCENTAGE] as $key => $value)
        {
            if(isset($out[$key]))
            {
                $out[$key][GroupConstants::PERCENTAGE] = $value[GroupConstants::PERCENTAGE];
            }
            else
            {
                $out[$key] = $value;
            }
        }
        return $out;
    }

}
