<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\Migration\Notification;

use ModulesGarden\HostingQuotaNotifications\App\Helper\Basics\Arrays;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Notifications\Manage;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Migration\DBManager;
use ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration\Constants as GroupConstants;


/**
 * Class Notifications
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Notifications
{
    public static function getFieldsToReplace()
    {
        return [
            'hid' => 'hostingid',
            'gid' => 'groupid',
        ];
    }

    public static function convertData($data)
    {
        $data        = Arrays::replaceKeys($data, self::getFieldsToReplace());
        $data['kind'] = self::getKind($data);
        $data['ticketid'] = 0;
        Manage::saveByFill($data);
    }

    public static function getKind($data)
    {
        $groupidConf = unserialize(self::getGroupConf($data));
        foreach ($groupidConf as $kind => $conf)
        {
            if(self::findValue($conf, $data['over']))
            {
                return GroupConstants::getFullKindFromShort($kind);
            }
        }
        return '';
    }

    public static function findValue($conf, $value)
    {
        foreach ($conf as $values)
        {
            if($values['value'] == $value)
            {
                return true;
            }
        }
        return false;
    }

    public static function getGroupConf($data)
    {
        return DBManager::fetchFromTableWhereRaw('mg_hq_groups', ['conf'], 'id = ?', [$data['groupid']], 'conf');
    }


}