<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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

use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Converter;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Queries;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Types\Tiered;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Types\Fixed;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\ProductGroups;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Notifications\StepsConfiguration;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Groups\Constants as GroupConstant;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Actions;


/**
 * Description of Types
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Types
{
    CONST TYPE_NAMESPACE = '\ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Types\\';
    private $configuration;
    private $products;
    private $type;
    private $actions;

    public function __construct()
    {
        $this->actions = new Actions();
    }

    public function research($group)
    {

        $this->basicActions($group, $group['type']);
        $className = self::TYPE_NAMESPACE . ucfirst($group['type']);

        $class  = new $className();
        $limits = $class->getMessagesIfDeclaredLimitIsReached($this->getArguments($group['type']), $this->products);

        $this->checkForSend($limits, $group, $this->configuration);
    }

    public function getArguments($type)
    {

        if ($type == GroupConstant::TIERED)
        {
            return StepsConfiguration::calculateByUnitsAndGetMin($this->configuration);
        }
        return $this->configuration;
    }

    public function basicActions($group, $type)
    {
        $this->type = $type;
        $this->setBasicValues($group['id']);
    }

    public function setBasicValues($groupId)
    {
        $this->configuration = [
            'bw'   => GroupConfiguration\Manage::getConfigurationForGroupByType(GroupConfiguration\Constants::BANDWIDTH, $groupId, false),
            'disk' => GroupConfiguration\Manage::getConfigurationForGroupByType(GroupConfiguration\Constants::DISK, $groupId, false),
        ];

        $this->products = ProductGroups\Manage::getProducts($groupId);
    }

    public function checkForSend($limits, $group, $configuration)
    {
        foreach ($limits as $kind => $limiter)
        {
            $this->checkForKind($kind, $limiter, $group, $configuration);
        }
    }

    public function checkForKind($kind, $limiter, $group, $configuration)
    {
        foreach ($limiter as $value)
        {
            $actionData                            = $this->getDataByType($configuration[$kind], $value, $kind);

            $actionData['differenceUsageAndLimit'] = $actionData['usage'] - $actionData['value'];
            $actionData['hostingId']               = $value['hid'];
            $actionData['type']                    = $group['type'];
            $actionData['groupId']                 = $group['id'];
            $actionData['intervalHours']           = $group['hours'];

            $this->actions->run($actionData);
        }
    }

    public function getDataByType($configuration, $value, $kind)
    {
        if ($this->type == GroupConstant::TIERED)
        {
            return StepsConfiguration::getMaxFromCalculatedByUnitsIfHostingUsageIsHigherThanDeclared($configuration, $value, $kind);
        }
        return StepsConfiguration::findBestStep($configuration, $value, $kind);
    }
}
