<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Create;

use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\ApiService;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Domain;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Traits\ProductManagerHandler;
use ModulesGarden\Servers\ZimbraEmail\Core\Helper\ConfigOptionsHelper;
use ModulesGarden\Servers\ZimbraEmail\Core\Helper\UpdateLimit;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 03.10.19
 * Time: 11:04
 * Class CreateDomain
 */
class CreateDomain extends ApiService
{
    use ProductManagerHandler;
    use \ModulesGarden\Servers\ZimbraEmail\App\Traits\HostingService;

    /**
     * @return mixed|void
     */
    public function process()
    {
        /**
         * prepare model
         */
        $hosting = $this->productManager->getHosting();
        $serviceID = $hosting->id;
        $configOption = new ConfigOptionsHelper;
        $acc_base = $this->productManager->get('acc_base') ? $this->productManager->get('acc_base') : 0;
        if(isset($this->formData['configoptions']['acc_add']) && isset($this->formData['configoptions']['acc_new'])) {
            $acc_add = $this->formData['configoptions']['acc_add'];
            $acc_new = $this->formData['configoptions']['acc_new'];
            $acc_add = $acc_add + $acc_new;
            $acc_new = 0;
            $accountLimit = $acc_base + $acc_add;
            if (($this->productManager->get('acc_limit') > 0) && ($accountLimit > $this->productManager->get('acc_limit'))) {
                $acc_add = $this->productManager->get('acc_limit') - $acc_base;
                $accountLimit = $this->productManager->get('acc_limit');
            }
            $acc_addObj = $configOption->getConfigurableOption($serviceID, 'acc_add');
            if(isset($acc_addObj->id)) {
                $configOption->updateConfigurableOption($acc_addObj->id, ['qty' => $acc_add]);
            }
            $acc_newObj = $configOption->getConfigurableOption($serviceID, 'acc_new');
            if(isset($acc_newObj->id)) {
                $configOption->updateConfigurableOption($acc_newObj->id, ['qty' => $acc_new]);
            }
        } else {
            $accountLimit = $acc_base;
        }
        $domain = new Domain();
        $domain->setName($this->formData['domain']);
        $domainBaseSize = $this->productManager->get('domainBaseSize') ? $this->productManager->get('domainBaseSize') : 0;
        if(isset($this->formData['configoptions']['domainAddSize']) && isset($this->formData['configoptions']['domainNewSize'])) {
            $domainAddSize = $this->formData['configoptions']['domainAddSize'];
            $domainNewSize = $this->formData['configoptions']['domainNewSize'];
            $domainAddSize = $domainAddSize + $domainNewSize;
            $domainNewSize = 0;
            $domainSize = $domainBaseSize + $domainAddSize;
            if (($this->productManager->get('domainMaxSize') > 0) && ($domainSize > $this->productManager->get('domainMaxSize'))) {
                $domainMaxSize = $this->productManager->get('domainMaxSize');
                $domainAddSize = $domainMaxSize - $domainBaseSize;
                $domainSize = $domainBaseSize + $domainAddSize;
            }
            $domainAddSizeObj = $configOption->getConfigurableOption($serviceID, 'domainAddSize');
            if(isset($domainAddSizeObj->id)) {
                $configOption->updateConfigurableOption($domainAddSizeObj->id, ['qty' => $domainAddSize]);
            }
            $domainNewSizeObj = $configOption->getConfigurableOption($serviceID, 'domainNewSize');
            if(isset($domainNewSizeObj->id)) {
                $configOption->updateConfigurableOption($domainNewSizeObj->id, ['qty' => $domainNewSize]);
            }
        } else {
            $domainSize = $domainBaseSize;
        }
        $updateLimit = new UpdateLimit;
        $updateLimit->updateDiskLimit($serviceID, $domainSize * 1024);
        $updateLimit->updateBWLimit($serviceID, $accountLimit);
        $domain->setAttrs([
            Domain::ATTR_MAIL_DOMAIN_QUOTA  => $domainSize * Size::B_TO_GB,
        ]);

        /**
         * API request
         */
        $result = $this->api->domain->create($domain);

        /**
         * check result
         */
        if(!$result)
        {
            $this->setError($this->api->domain->getError());
            return false;
        }

        return $result;
    }
}
