<?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;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\XmlParser;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 28.08.19
 * Time: 13:46
 * Class Account
 */
class Ressource extends AbstractAction
{

    public function read()
    {

    }

    /**
     * @description create ressource in Kerio API
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource
     * @return bool|void
     */
    public function create(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource)
    {
        /**
         *
         */
        $params = [
            new SoapParam($ressource->getName(), "name"),
            new SoapParam($ressource->getPassword(), "password"),
        ];

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

        /**
         * run request
         */
        $result = $this->connection->request("CreateCalendarResourceRequest", $params);

        $this->setLastResult($result);

        /**
         * load response to model
         */
        if($ressourceData = $result->getResponseBody()['CREATECALENDARRESOURCERESPONSE']['CALRESOURCE'])
        {
            $ressource->fill($ressourceData);
            return $ressource;
        }

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

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource
     * @return bool|\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource
     */
    public function update(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource)
    {
        $params = [
            new SoapParam($ressource->getId(), "id"),
        ];

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

        $result = $this->connection->request("ModifyCalendarResourceRequest", $params);
        $this->setLastResult($result);

        if($ressourceData = $result->getResponseBody()['MODIFYCALENDARRESOURCERESPONSE']['CALRESOURCE'])
        {
            $ressource->fill($ressourceData);
            return $ressource;
        }

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

    public function setPassword(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource)
    {
        $params = [
            new SoapParam($ressource->getId(), "id"),
            new SoapParam($ressource->getPassword(), "newPassword"),
        ];

        $result = $this->connection->request("SetPasswordRequest", $params);
        $this->setLastResult($result);

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

        return true;

    }

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

        $result = $this->connection->request("DeleteCalendarResourceRequest", $params);
        $this->setLastResult($result);

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

        return true;
    }

    /**
     * @param $name
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function getRessourceId($name)
    {
        $params = [
            new SoapVar('<ns1:calresource by="name">' . $name . '</ns1:calresource>', XSD_ANYXML),
        ];

        return $this->connection->cleanResponse()->request("GetCalendarResourceRequest", $params);

    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function getRessourceInfo(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource)
    {
        if($value = $ressource->getId())
        {
            $type = 'id';
        }elseif($value =$ressource->getName())
        {
            $type = 'name';
        }

        $result = null;

        $params = [
            new SoapVar('<ns1:calresource by="' . $type . '">' . $value . '</ns1:calresource>', XSD_ANYXML),
        ];

        return $this->connection->request("GetCalendarResourceRequest", $params);
    }

    /**
     * @param \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource
     * @return \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response
     */
    public function getRessourceOptions(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource $ressource)
    {
        if($value = $ressource->getId())
        {
            $type = 'id';
        }elseif($value =$ressource->getName())
        {
            $type = 'name';
        }

        $params = [
            new SoapVar('<ns1:calresource by="' . $type . '">' . $value . '</ns1:calresource>', XSD_ANYXML),
        ];

        return  $this->connection
            ->cleanResponse()
            ->request("GetCalendarResourceRequest", $params);
    }

    /**
     * @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 getAllByDomain(\ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain $domain)
    {
        if($value = $domain->getId())
        {
            $type = 'id';
        }elseif($value =$domain->getName())
        {
            $type = 'name';
        }

        $params = [
            new SoapVar('<ns1:domain by="' . $type . '">' . $value . '</ns1:domain>', XSD_ANYXML),
        ];
        $result = $this->connection->request("GetAllCalendarResourcesRequest", $params);
        return $result;
    }

    public function getAllRessources($idOrNameDomain, $type = "auto")
    {
        if ($type == "auto")
        {
            //$realType = getDomainType($idOrNameDomain);
        }
        else
        {
            $realType = $type;
        }

        $result = null;

        $params = [
            new SoapVar('<ns1:domain by="' . $realType . '">' . $idOrNameDomain . '</ns1:domain>', XSD_ANYXML),
        ];

        return  $this->connection->request("GetAllCalendarResourcesRequest", $params);

    }

}