<?php

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

use ModulesGarden\HostingQuotaNotifications\App\Helper\Models\Hosting;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Models\Product;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Notifications\Manage as NotificationsManage;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Models\EmailTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Lang\Lang;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Api;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\Log;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Date\Date;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Converter;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Groups\Manage as GroupManage;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Unit;

/**
 * Description of SendNotification
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class SendNotification extends Base
{
    CONST SENDEMAIL = 'SendEmail';

    public function run()
    {
        if (!$this->checkLastNotificationDate() || $this->data['value'] == 0)
        {
            return;
        }

        $params = $this->getParams();

        if (!$params)
        {
            return false;
        }
        $result = Api::apiCall(SELF::SENDEMAIL, $params);
        Log::log(SELF::SENDEMAIL, $params, $result, $this->data);
        return isset($result['error']) ? false : true;
    }

    public function checkLastNotificationDate()
    {
        $lastNotificationDate = NotificationsManage::getLastNotificationValue($this->data['hostingId'], $this->data['kind'], $this->data['type'], $this->data['groupId'], 'date');
        return Date::checkIfWaitedEnough($this->data['intervalHours'], $lastNotificationDate);
    }

    public function getParams()
    {
        $templateName = $this->getEmailTemplate();
        if (!$templateName)
        {
            return false;
        }

        $params = [
            'id'          => $this->data['hostingId'],
            'messagename' => $templateName,
            'customvars'  => [
                'limit'                   => Unit::calculateByUnit($this->data['unit'], $this->data['value']),
                'usage'                   => $this->data['usage'],
                'kind'                    => Lang::getAbsoulteLang('kind', $this->data['kind']),
                'unit'                    => Converter::overType($this->data['usageUnit']),
                'differenceUsageAndLimit' => $this->data['differenceUsageAndLimit'],
                'periodDays'              => $this->data['countedDays'],
            ]
        ];

        $params['customvars'] += Hosting::getServiceLimits($this->data['hostingId']);

        return $params;
    }

    public function getEmailTemplate()
    {
        if ($this->data['email_template'] == 'none')
        {
            return false;
        }

        if ($this->data['email_template'] == 'default')
        {
            $templateRule = GroupManage::getValueForGroup($this->data['groupid'], 'defaultEmailTemplate');
            return $templateRule == 0 ? false : EmailTemplates::getValueById($templateRule, 'name');
        }
        return EmailTemplates::getValueById($this->data['email_template'], 'name');
    }

}
