<?php

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


use ThurData\Servers\KerioEmail\App\Enums\Kerio;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioApiException;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioServiceException;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Enums\ApiErrorCodes;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 10.10.19
 * Time: 14:41
 * Class DeleteDomain
 */
class DeleteDomain extends ApiService
{

    protected $domain;

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

        return true;
    }

    /**
     * @return mixed|void
     */
    public function process()
    {
        $this->setDomainAsActiveIfRequired();
        $this->removeLists();
        $this->removeAccounts();
        $this->removeDomain();
        if ($this->getError())
        {
            return false;
        }

        return true;
    }

    protected function setDomainAsActiveIfRequired()
    {
        $domain = $this->getOrLoadDomain();
        $domain->setAttrs([
            Domain::ATTR_DOMAIN_STATUS => Kerio::ACC_STATUS_ACTIVE,
            Domain::ATTR_MAIL_STATUS    => Kerio::ENABLED,
        ]);

        $response = $this->api->domain->update($domain);

        return $this;
    }

    /**
     * remove distribution list
     *
     * @return bool
     */
    protected function removeLists()
    {
        /**
         * get lists
         */
        $lists = $this->api->repository()->lists->getAllDistributionListsByDomain($this->formData['domain']);

        /**
         * remove each list
         */
        foreach($lists as $list)
        {
            /* @var $list DistributionList */
            $result = $this->api->distributionList->delete($list);
            if($this->api->distributionList->getError())
            {
                $this->throwError($this->api->distributionList->getError());
            }
        }

        return true;
    }

    /**
     *
     * @return bool
     *
     */
    protected function removeAccounts()
    {
        /**
         * get accounts from Kerio
         */
        $accounts = $this->api->repository()->accounts->getByDomainName($this->formData['domain']);
        /**
         * remove each account
         */
        foreach($accounts as $account)
        {
            $result = $this->api->account->delete($account);
            if($this->api->account->getError())
            {
                $this->throwError($this->api->account->getError());
            }
        }

        return true;
    }

    /**
     * remove domain from API
     *
     * @return bool
     */
    protected function removeDomain()
    {
        /**
         *
         * get domain from kerio
         */
        $domain = $this->getOrLoadDomain();


        /**
         * remove domain
         */
        $result = $this->api->domain->delete($domain);

        if(!$result && ApiErrorCodes::DOMAIN_DOES_NOT_EXISTS !== $this->api->domain->getLastResult()->getLastErrorCode())
        {
            $this->setError($this->api->domain->getLastResult()->getLastErrorCode());
            return false;
        }

        return true;
    }

    /**
     *
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain
     */
    protected function getOrLoadDomain()
    {
        if(!$this->domain)
        {
            $this->domain = $this->api->repository()->domains->getByName($this->formData['domain']);
        }

        if ($this->api->repository()->domains->getError())
        {
            $this->throwError($this->api->repository()->domains->getError());
        }

        return $this->domain;
    }


}