<?php

namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update;


use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
use ThurData\Servers\KerioEmail\App\Enums\Size;
use ThurData\Servers\KerioEmail\App\Enums\Kerio;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProductManagerHandler;
use ThurData\Servers\KerioEmail\Core\Helper\ConfigOptionsHelper;
use ThurData\Servers\KerioEmail\Core\Helper\UpdateLimit;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 07.11.19
 * Time: 13:03
 * Class ChangePackage
 */
class ChangePackage extends ApiService
{
    use ProductManagerHandler;
    use \ThurData\Servers\KerioEmail\App\Traits\HostingService;

    public function isValid()
    {
        if(!$this->formData['domain'])
        {
            $this->setError('Domain name can not be found.');
            return false;
        }

        return parent::isValid(); // TODO: Change the autogenerated stub
    }

    public function process()
    {
        $serviceID = $this->formData['serviceid'];
        $configOption = new ConfigOptionsHelper;
        $accounts = $this->api->repository()->accounts->getByDomainName($this->formData['domain']);
        $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;
            }
            $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 = $this->productManager->get('acc_limit');
        }
        foreach($accounts as $account)
        {
            /* @var $account Account*/
            foreach($this->productManager->getKerioConfiguration() as $key => $value)
            {
                $value = $value === ProductParams::SWITCHER_ENABLED ? Kerio::ATTR_ENABLED : Kerio::ATTR_DISABLED;
                $account->setAttr($key, $value);
                $result = $this->api->account->update($account);

                if(!$result)
                {
                    //todo some logs
                }
            }
        }
        $domain = $this->api->repository()->domains->getByName($this->formData['domain']);
        if(!$domain) {
            throw new \Exception($this->api->repository()->domains->getError());
            return false;
        }
        $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 = $this->productManager->get('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,
        ]);
        $response = $this->api->domain->update($domain);
        if(!$response)
        {
            throw new \Exception($this->api->domain->getError());
            return false;
        }

        return true;
    }
}
