<?php

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


use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractApiClient;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractRepository;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 10.09.19
 * Time: 07:59
 * Class Domains
 */
class Domains extends AbstractRepository
{
    use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;


    /**
     * @param $name
     * @return Domain| null
     */
    public function getByName($name)
    {
        $domain = new Domain();
        $domain->setName($name);

        $result = $this->getClient()->domain->getDomainId($domain);

        if(!$result->getLastError())
        {
            $domain = new Domain($result->getResponseBody()['GETDOMAININFORESPONSE']['DOMAIN']);
            return $domain;
        }

        $this->setError($result->getLastError());
        return false;
    }

    public function getAliases($name)
    {
        $mainDomain = $this->getByName($name);

        if(!$mainDomain)
        {
            return false;
        }

        /**
         *
         * parse all domain & return aliasese
         */
        foreach($aliases = $this->getAll() as $key => $alias)
        {
            /**
             *
             * check if domain is alias
             */
            /* @var $alias Domain */
            if(!$alias->isAlias())
            {
                unset($aliases[$key]);
                continue;
            }

            /**
             *
             * check if alias belong to main domain
             */
            if($alias->getAttr(Domain::ATTR_ALIAS_TARGET_ID) !== $mainDomain->getId())
            {
                unset($aliases[$key]);
                continue;
            }
        }

        return $aliases;
    }

    public function getAll()
    {
        $result = $this->getClient()->domain->getAll();

        $domains = $result->getResponseBody()['GETALLDOMAINSRESPONSE']['DOMAIN'];

        /**
         * API return one or araay with accounts
         */
        if(isset($domains['NAME']))
        {
            $tmpAccount =  new Domain($domains);
            $tmp[$tmpAccount->getId()] = $tmpAccount;
        }else{
            foreach($domains as $account)
            {
                $tmpAccount =  new Domain($account);
                $tmp[$tmpAccount->getId()] = $tmpAccount;
            }
        }

        return $tmp;
    }

    public function getDomain()
    {
        $result = $this->getClient()->domain->getAll();
        $domains = $result->getResponseBody()['GETALLDOMAINSRESPONSE']['DOMAIN'];

        $domain = new Domain();

        $result = $this->getClient()->domain->getDomain($domain);


    }

}