<?php

namespace MGModule\DNSManager2\mgLibs\custom\manager;

use \MGModule\DNSManager2 as main;
use \MGModule\DNSManager2\mgLibs\custom\helpers\DomainHelper;
use \MGModule\DNSManager2\models\custom\package;
use \MGModule\DNSManager2\models\custom\zone\ZoneTypeEnum;
use MGModule\DNSManager2\mgLibs\custom\manager\additionalMethods\DefaultIPHelper;

class ZoneAutomationHelper {
    const ADDON_ACTIVATION = 'addon_activation';
    const PRODUCT_CREATE = 'product_create';
    const DOMAIN_TRANSFER = 'domain_transfer';
    const DOMAIN_REGISTRATION = 'domain_registration';
    const DOMAIN_WITH_DNS_MANAGEMENT = 'domain_with_dns_management';
    
    private $hook_type;
    /** @var ZoneCreator */
    private $creator;
    
    public function __construct($type, $params) {

        $this->hook_type = $type;
        $defaultIPMethods = new DefaultIPHelper();

        switch ($type) {
            case self::ADDON_ACTIVATION:
                $hosting    =   new main\models\whmcs\service\service($params['serviceid']);
                $domain     =   new DomainHelper($hosting->domain);

                $ip = $defaultIPMethods->getDefaultIp($hosting->dedicatedIP, $hosting->IPList, $hosting->server()->ip, null);
                
                $this->creator = new ZoneCreator($domain->getDomainWithTLD(), ZoneTypeEnum::ADDON, $params['id'], $ip, $hosting->clientID());
                break;

            case self::PRODUCT_CREATE:
                $hosting    =   new main\models\whmcs\service\service($params['serviceid']);
                $domain     =   new DomainHelper($hosting->domain);

                try
                {
                    $serverIp = $hosting->server()->ip;
                }
                catch( \Exception $e )
                {
                    $serverIp = null;
                }

                $ip =  $defaultIPMethods->getDefaultIp($hosting->dedicatedIP, $hosting->IPList,$serverIp , null);

                $this->creator = new ZoneCreator($domain->getDomainWithTLD(), ZoneTypeEnum::HOSTING, $params['serviceid'], $ip, $hosting->clientID());
                break;
            
            case self::DOMAIN_REGISTRATION: case self::DOMAIN_TRANSFER: case self::DOMAIN_WITH_DNS_MANAGEMENT:
                $domain = new main\models\whmcs\domains\domain($params['domainid']);

                $ip = $defaultIPMethods->getDefaultIpBySpecificDomain($domain, ZoneTypeEnum::DOMAIN);

                $this->creator = new ZoneCreator($domain->domain, ZoneTypeEnum::DOMAIN, $params['domainid'], $ip, $domain->clientID());
                break;
        }
        
    }

    public function run() {
        if(!$this->isAutomationEnabled())
        {
            return;
        }
        $this->creator->create( false, $this->isExistedZoneUpdateAllowed());
    }
    
    private function isAutomationEnabled() {
        if($this->creator->getPackage() === false) {

            return false;
        }

        switch ($this->hook_type) {
            case self::ADDON_ACTIVATION: case self::PRODUCT_CREATE:
                return (string)$this->creator->getPackage()->getSettings(package\setting\PackageSettingEnum::ZONE_AUTOMATION_NEW_ITEM) === 'on';
                break;
            case self::DOMAIN_REGISTRATION:
                return (string)$this->creator->getPackage()->getSettings(package\setting\PackageSettingEnum::ZONE_AUTOMATION_PENDING_REGISTRATION_DOMAIN) === 'on';
                break;
            case self::DOMAIN_TRANSFER:
                return (string)$this->creator->getPackage()->getSettings(package\setting\PackageSettingEnum::ZONE_AUTOMATION_PENDING_TRANSFER_DOMAIN) === 'on';
                break;
            case self::DOMAIN_WITH_DNS_MANAGEMENT:
                return (string)$this->creator->getPackage()->getSettings(package\setting\PackageSettingEnum::ZONE_AUTOMATION_DOMAIN_WITH_DNS_MANAGEMENT) === 'on';
                break;
        }
    }
    
    
    private function isExistedZoneUpdateAllowed()
    {
        return $this->creator->getPackage()->getSettings(package\setting\PackageSettingEnum::UPDATE_EXISTED_ZONE) == 'on';
    }
    
    public function getCreator()
    {
        return $this->creator;
    }
    
}
