<?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\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\ClassOfService;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 07.11.19
 * Time: 14:00
 * Class ChangePackageDedicatedCos
 */
class ChangePackageDedicatedCos extends ChangePackage
{

    protected $cosModels = [];

    /**
     *
     */
    public function config()
    {
        $this->cosModels = $this->api->repository()->cos->all();

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

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

        /**
         * if cos doesn't exists
         */
        if(!$this->productManager->getSettingCos())
        {
            $this->setError('Can not found class of service Id');
            return false;
        }

        /**
         * invalid id format
         */
        if(!is_string($this->productManager->getSettingCos()))
        {
            $this->setError('Invalid class of service ID format');
            return false;
        }

        return parent::isValid();
    }

    /**
     * @return bool|mixed
     */
    public function process()
    {
        $accounts = $this->api->repository()->accounts->getByDomainName($this->formData['domain']);

        foreach($accounts as $account)
        {
            /* @var $account Account*/
            /* @var $cos ClassOfService*/
            $cos = $this->cosModels[$this->productManager->getSettingCos()];

            /**
             * set quota by class of service
             */
            $account->setAttr(Account::ATTR_MAIL_QUOTA, $cos->getDataResourceA(Account::ATTR_MAIL_QUOTA));

            /**
             * define class of services attribute for account
             */
            $cosAttrs = $cos->getAllDataResourcesAAttributes();

            foreach(Kerio::BASE_ACCOUNT_CONFIG as $key)
            {
                $value = $cosAttrs[$key] ? $cosAttrs[$key] : Kerio::ATTR_DISABLED;
                $account->setAttr($key, $value);
            }

            /**
             *
             * set class of service id as account attribute
             */
            $account->setAttr(Account::ATTR_CLASS_OF_SERVICE_ID, $cos->getId());

            /**
             * update account
             */
            $result = $this->api->account->update($account);

            if(!$result)
            {
                //todo
            }
        }

        return true;

    }
}