<?php

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


use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\ApiService;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Create\CreateAccountCosQuota;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 09.10.19
 * Time: 08:25
 * Class UpdateAccountCosQuota
 */
class UpdateAccountCosQuota extends CreateAccountCosQuota
{
    /**
     * @return bool|mixed|Account|void
     */
    protected function process()
    {
        /**
         *
         */
        $model = $this->getModel();

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

        /**
         * problem with create account
         */
        if(!$result)
        {
            $this->setError($this->api->account->getError());
            return false;
        }

        return $result;
    }

    /**
     * @return Account
     */
    public function getModel()
    {
        $account = new Account();
        /**
         *  set account attributes
         */
        $account->setId($this->formData['id']);

        $account->setAttr(Account::ATTR_FIRSTNAME, $this->formData['firstname']);
        $account->setAttr(Account::ATTR_LASTNAME, $this->formData['lastname']);
        $account->setAttr(Account::ATTR_PHONE, $this->formData['phone']);
        $account->setAttr(Account::ATTR_MOBILE_PHONE, $this->formData['mobile_phone']);
        $account->setAttr(Account::ATTR_FAX, $this->formData['fax']);
        $account->setAttr(Account::ATTR_PAGER, $this->formData['pager']);
        $account->setAttr(Account::ATTR_HOME_PHONE, $this->formData['home_phone']);
        $account->setAttr(Account::ATTR_COUNTRY, $this->formData['country']);
        $account->setAttr(Account::ATTR_STATE, $this->formData['state']);
        $account->setAttr(Account::ATTR_PROF_TITLE, $this->formData['title']);
        $account->setAttr(Account::ATTR_POSTAL_CODE, $this->formData['post_code']);
        $account->setAttr(Account::ATTR_CITY, $this->formData['city']);
        $account->setAttr(Account::ATTR_STREET, $this->formData['street']);
        $account->setAttr(Account::ATTR_COMPANY, $this->formData['company']);
        $account->setAttr(Account::ATTR_ACCOUNT_STATUS, $this->formData['status']);
        $account->setAttr(Account::ATTR_DISPLAY_NAME, $this->formData['display_name']);

        /* @var $cos ClassOfService*/
        $cos = $this->cosModels[$this->formData['cosId']];

        /**
         * 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(Zimbra::BASE_ACCOUNT_CONFIG as $key)
        {
            $value = $cosAttrs[$key] ? $cosAttrs[$key] : Zimbra::ATTR_DISABLED;
            $account->setAttr($key, $value);
        }

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


        return $account;
    }

}