<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper;

use ModulesGarden\HostingQuotaNotifications\App\Models\Templates\MessageGroups;

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

    //  protected static $needSelect = ['New Order Notification', 'Support Ticket Created', 'Support Ticket Flagged'];

    public static function getGroupsForMessage($message)
    {
        $data = array(
            'Automatic Setup Failed'               => array('service', 'client', 'domain'),
            'Automatic Setup Successful'           => array('service', 'client', 'domain'),
            'Domain Renewal Failed'                => array('client', 'domain'),
            'Domain Renewal Successful'            => array('client', 'domain'),
            'New Cancellation Request'             => array('client', 'service'),
            'New Order Notification'               => array('order', 'client'),
            'Service Unsuspension Failed'          => array('service', 'client'),
            'Service Unsuspension Successful'      => array('service', 'client'),
            'Support Ticket Created'               => array('client', 'ticket'),
            'Support Ticket Department Reassigned' => array('client', 'ticket'),
            'Support Ticket Flagged'               => array('client', 'ticket'),
            'Support Ticket Response'              => array('client', 'ticket'),
            'Client Login Notification'            => array('client'),
        );
        return $data[$message];
    }

    public static function getAdminMessages()
    {
        return [
            'Automatic Setup Failed',
            'Automatic Setup Successful',
            'Domain Renewal Failed',
            'Domain Renewal Successful',
            'New Cancellation Request',
            'New Order Notification',
            'Service Unsuspension Failed',
            'Service Unsuspension Successful',
            'Support Ticket Created',
            'Support Ticket Department Reassigned',
            'Support Ticket Flagged',
            'Support Ticket Response'
            ];
    }

    public static function filterMessages($messages)
    {
        $templates = self::getAdminMessages();
        foreach ($messages as $key => $msg)
        {
            if (!in_array($msg['msg_name'], $templates) && !in_array($msg['message'], $templates))
            {
                unset($messages[$key]);
            }
        }
        return $messages;
    }

    public static function getAdminSwitchForm()
    {
        $model  = new MessageGroups();
        $fields = $model->where('group', 'admin')->get()->toArray();
        return $fields;
    }

    public static function markSelectedTemplates($templates, $selected)
    {
        foreach ($templates as $key => $item)
        {
            if (in_array($item['id'], $selected))
            {
                $templates[$key]['checked'] = 1;
            }
        }
        return $templates;
    }
}
