<?php

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


use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractAction;
use SoapFault;
use SoapParam;
use SoapVar;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 28.08.19
 * Time: 13:46
 * Class Domain
 */
class Domain extends AbstractAction
{
    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain|\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function create(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {
        $params   = array(
            new SoapParam($domain->getName(), "name"),
        );

        foreach ($domain->getAttrs() as $key => $value)
        {
            $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
        }

        $result = $this->connection->request("CreateDomainRequest", $params);
        $this->setLastResult($result);
        /**
         * check data has been returned or return false
         */
        if($data = $result->getResponseBody()['CREATEDOMAINRESPONSE']['DOMAIN'])
        {
            $domain->fill($data);
            return $domain;
        }

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

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function update(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {
        $params   = array(
            new SoapParam($domain->getId(), "id"),
        );

        foreach ($domain->getAttrs() as $key => $value)
        {
            $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
        }

        $result = $this->connection->request("ModifyDomainRequest", $params);
        $this->setLastResult($result);
        if($data = $result->getResponseBody()['MODIFYDOMAINRESPONSE']['DOMAIN'])
        {
            $domain->fill($data);
            return $domain;
        }
        $this->setError($result->getLastError());
        return false;
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return bool
     */
    public function delete(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {
        $params   = array(
            new SoapParam($domain->getId(), "id"),
        );

        $result = $this->connection->cleanResponse()->request("DeleteDomainRequest", $params);
        $this->setLastResult($result);
        if($result->getResponseBody()['DELETEDOMAINRESPONSE'])
        {
            return true;
        }
        $this->setError($result->getLastError());
        return false;

    }

    /**
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function getAll()
    {
        $result = $this->connection->request("GetAllDomainsRequest", []);
        return $result;
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    function getDomainId(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {

        $params = array(
            new SoapVar('<ns1:domain by="name">' . $domain->getName() . '</ns1:domain>', XSD_ANYXML),
        );

        $result = $this->connection->cleanResponse()->request("GetDomainInfoRequest", $params);
        $this->setLastResult($result);
        return $result;
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    function getDomain(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {

        $params = array(
            new SoapVar('<ns1:domain by="name">' . $domain->getName() . '</ns1:domain>', XSD_ANYXML),
        );

        $result = $this->connection->cleanResponse()->request("GetDomainRequest", $params);
        $this->setLastResult($result);
        return $result;
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function getDomainUsages(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {

        $params = array(
            new SoapVar('<ns1:GetQuotaUsageRequest />', XSD_ANYXML),
            new SoapParam($domain->getName(), 'domain'),
            new SoapParam("1", 'refresh')
        );

        $result = $this->connection->request("GetQuotaUsageRequest", $params);
        $this->setLastResult($result);
        return $result;
    }

}