<?php

namespace MGModule\DNSManager2\mgLibs\custom\manager;

use \MGModule\DNSManager2\mgLibs\custom\mailer\Mailer;
use \MGModule\DNSManager2\mgLibs\MySQL\query;
use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
use \MGModule\DNSManager2\models\custom\notification\NotificationNameEnum;
use \MGModule\DNSManager2\models\custom\zone\Zone;
use \MGModule\DNSManager2\models\whmcs\admin\admin;
use MGModule\DNSManager2\models\whmcs\client\client;
use MGModule\DNSManager2\models\whmcs\clients\repository;

class EmailNotificationHelper {
    private static $messages_map = array(
        DefaultNotifications::GENERAL_ZONE_CREATED_NOTIFICATION => array(
            'type' => 'client',
            'option' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_CREATED,
            'exclude' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_CREATED_EXCLUDE,
            'notification' => NotificationNameEnum::ZONE_CREATED_EMAIL,
        ),
        DefaultNotifications::GENERAL_ZONE_ALTERED_NOTIFICATION => array(
            'type' => 'client',
            'option' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_ALTERED,
            'exclude' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_ALTERED_EXCLUDE,
            'notification' => NotificationNameEnum::ZONE_EDITED_EMAIL,
        ),
        DefaultNotifications::GENERAL_ZONE_REMOVED_NOTIFICATION => array(
            'type' => 'client',
            'option' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_REMOVED,
            'exclude' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_REMOVED_EXCLUDE,
            'notification' => NotificationNameEnum::ZONED_REMOVED_EMAIL,
        ),
        DefaultNotifications::ADMIN_CRON_CLEANER_NOTIFICATION => array(
            'type' => 'admin',
            'option' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_CLEANER,
            'include' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_CLEANER_INCLUDE,w
        ),
        DefaultNotifications::ADMIN_CRON_MIGRATOR_NOTIFICATION => array(
            'type' => 'admin',
            'option' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_MIGRATOR,
            'include' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_MIGRATOR_INCLUDE,
        ),
        DefaultNotifications::ADMIN_CRON_SYNCHRONIZATOR_NOTIFIACTION => array(
            'type' => 'admin',
            'option' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_SYNCHRONIZATOR,
            'include' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_SYNCHRONIZATOR_INCLUDE,
        ),
    );

    public static function sendClientNotificationUsingZoneID($message_name, $zoneid, $data = array()) {
        $zone = Zone::factory($zoneid);
        self::sendClientNotificationUsingZone($message_name, $zone, $data);
    }

    public static function sendClientNotificationUsingZone($message_name, $zone, $data = array()) {
        $item = $zone->getPackageItem();
        if($item == false) {
            return ;
        }
        self::sendClientNotification($message_name, $zone->clientid, $item->type, $item->relid, $data);
    }

    public static function sendClientNotification($message_name, $clientid, $type, $relid, $data = array()) {
        $message = self::$messages_map[$message_name];
        if(GlobalSettingHelper::getSetting($message['option']) != 'on')
            return ;

        $exclude = GlobalSettingHelper::getSetting($message['exclude']);
        $exclude = explode(';', $exclude);
        foreach($exclude as &$arr)
            $arr = explode(',', $arr);

        if(in_array($relid, $exclude[$type-1]))
            return ;

        $data['customvars'] = $data;
        $data['id'] = $clientid;

        $client = new \MGModule\DNSManager2\models\whmcs\clients\client($clientid);
        $clientNames = "{$client->firstname} {$client->lastname}";
        $data['messagename'] = $message_name;
        $clientURL = '<a href="clientssummary.php?userid=%s"> #%s (%s) </a>';
        $client = sprintf($clientURL, $clientid, $clientid, $clientNames);
        $value = "Email $message_name sent to a client with ID $client";

        NotificationHelper::addInfoNotification($value, $message['notification']);

        return self::localApi('sendemail', $data);
    }

    public static function sendAdminNotification($message_name, $data = array()) {
        $message = self::$messages_map[$message_name];
        if(GlobalSettingHelper::getSetting($message['option']) != 'on')
            return ;

        $include = GlobalSettingHelper::getSetting($message['include']);
        $include = explode(',', $include);

        $mailer = new Mailer();

        foreach($include as $adminid) {
            try {
                $admin = new admin($adminid);
                $mailer->addAddress($admin->email);
            } catch (\Exception $e) {

            }
        }
        $mailer->addVariables($data);
        $mailer->sendWHMCSTemplate($message_name);
    }

    private static function localApi($command, $data = array()) {
        return localAPI($command, $data, self::getAdmin());
    }

    private static function getAdmin() {
        return query::query('SELECT tbladmins.username FROM tbladmins WHERE tbladmins.roleid = \'1\'')->fetchColumn();
    }
}