<?php

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


use ThurData\Servers\KerioEmail\App\Enums\Size;
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\Ressource;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\RessourceAlias;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 10.09.19
 * Time: 08:28
 * Class Ressources
 */
class Ressources extends AbstractRepository
{
    /**
     * get all ressource for domain
     * @param $name
     * @return mixed
     */
    public function getByDomainName($name)
    {
        $domain = new Domain();
        $domain->setName($name);

        $result = $this->getClient()->ressource->getAllByDomain($domain);
        $ressources = $result->getResponseBody()['GETALLCALENDARRESOURCESRESPONSE']['CALRESOURCE'];
        /**
         * API return one or araay with ressources
         */
        $tmp = [];
        if(isset($ressources['NAME']))
        {
            $tmpRessource =  new Ressource($ressources);

            /**
             * skip ZiImbra default ressource
             */
            if (strpos($tmpRessource->getName(), 'galsync@') !== false)
            {
                return [];
            }
            $tmp[$tmpRessource->getId()] = $tmpRessource;
        }else{
            foreach($ressources as $ressource)
            {
                $tmpRessource =  new Ressource($ressource);

                /**
                 * skip ZiImbra default ressource
                 */
                if (strpos($tmpRessource->getName(), 'galsync@') !== false)
                {
                    continue;
                }


                $tmp[$tmpRessource->getId()] = $tmpRessource;
            }
        }

       return $tmp;
    }

    /**
     * @param $name
     * @return mixed
     */
    public function getRessources($name)
    {
        $ressources = $this->getByDomainName($name);

        foreach($ressources as $key => $ressource)
        {
            /* @var $ressource Ressource*/
            if (strpos($ressource->getName(), 'galsync@') !== false)
            {
                unset($ressources[$key]);
                continue;
            }

        }

        return $ressources;
    }

    /**
     * @param $id
     * @return Ressource
     */
    public function getRessourceInfoById($id)
    {
        $ressource = new Ressource();
        $ressource->setId($id);

        $result = $this->getClient()->ressource->getRessourceInfo($ressource);

        if(!$result->getLastError())
        {
            $body = $result->getResponseBody();
            $result =  $ressource->fill($body['GETCALENDARRESOURCERESPONSE']);
            $result->setName($body['GETCALENDARRESOURCERESPONSE']['NAME']['DATA']);
            return $result;
        }

        return $result;
    }

    /**
     * @param $id
     * @return Ressource
     */
    public function getRessourceOptionsById($id)
    {
        $ressource = new Ressource();
        $ressource->setId($id);

        $result = $this->getClient()->ressource->getRessourceOptions($ressource);

        if(!$result->getLastError())
        {
            $body = $result->getResponseBody();
            return $ressource->fill($body['GETCALENDARRESOURCERESPONSE']['CALRESOURCE']);
        }

        return $result;
    }

}