<?php
/**
 * Class CreateRessource
 * User: ThurData
 * Date: 2019-10-07
 * Time: 10:41
 * @package ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create
 */

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


use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
use ThurData\Servers\KerioEmail\App\Enums\Size;
use ThurData\Servers\KerioEmail\App\Enums\Kerio;
use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProductManagerHandler;
use ThurData\Servers\KerioEmail\Core\Helper\ConfigOptionsHelper;

class CreateRessource extends ApiService
{
    use ProductManagerHandler;
    use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;

    /**
     * added more conditions
     * @return bool
     */
    protected function isValid()
    {
        /**
         * check if product manager is set
         */
        if(!$this->productManager)
        {
            $this->setError('Product Manager Not Found');
            return false;
        }

        /**
         * domain name
         */
        if(!$this->formData['domain'])
        {
            $this->setError('Domain name can not be found.');
            return false;
        }

        /**
         *
         */
        return parent::isValid();
    }

    /**
     * @return mixed
     */
    protected function getRessources()
    {
        $ressources = $this->api->repository()->ressources->getRessources($this->formData['domain']);
        return $ressources;
    }

    /**
     * @return Ressource
     */
    protected function getModel()
    {
        /**
         * create new ressource in kerio
         */
        $ressource = new Ressource();

        $ressource->setName($this->formData['username'].'@'.$this->formData['domain']);
        $ressource->setPassword(html_entity_decode($this->formData['password']), ENT_QUOTES);
        /**
         *  set ressource attributes
         */
        $ressource->setAttr(Ressource::ATTR_STATUS, $this->formData['status']);
        $ressource->setAttr(Ressource::ATTR_TYPE, $this->formData['type']);
        $ressource->setAttr(Ressource::ATTR_DISPLAY_NAME, $this->formData['display_name']);
        $ressource->setAttr(Ressource::ATTR_DESC, $this->formData['description']);
        $ressource->setAttr(Ressource::ATTR_NOTE, $this->formData['notes']);
        $ressource->setAttr(Ressource::ATTR_CONT, $this->formData['contact']);
        $ressource->setAttr(Ressource::ATTR_CONT_EMAIL, $this->formData['contact_mail']);
        $ressource->setAttr(Ressource::ATTR_CONT_PHONE, $this->formData['contact_phone']);
        $ressource->setAttr(Ressource::ATTR_SITE, $this->formData['site']);
        $ressource->setAttr(Ressource::ATTR_BUILDING, $this->formData['building']);
        $ressource->setAttr(Ressource::ATTR_FLOOR, $this->formData['floor']);
        $ressource->setAttr(Ressource::ATTR_ROOM, $this->formData['room']);
        $ressource->setAttr(Ressource::ATTR_CAPACITY, $this->formData['capacity']);
        $ressource->setAttr(Ressource::ATTR_STREET, $this->formData['street']);
        $ressource->setAttr(Ressource::ATTR_TOWN, $this->formData['town']);
        $ressource->setAttr(Ressource::ATTR_POSTAL_CODE, $this->formData['post_code']);
        $ressource->setAttr(Ressource::ATTR_COUNTY, $this->formData['county']);
        $ressource->setAttr(Ressource::ATTR_STATE, $this->formData['state']);
        $ressource->setAttr(Ressource::ATTR_AUTO, $this->formData['auto_accept'] == 'on' ? 'TRUE' : 'FALSE' );
        $ressource->setAttr(Ressource::ATTR_BUSY, $this->formData['auto_busy'] == 'on' ? 'TRUE' : 'FALSE' );

        foreach($this->productManager->getKerioConfiguration() as $key => $value)
        {
            $value = $value === ProductParams::SWITCHER_ENABLED ? Kerio::ATTR_ENABLED : Kerio::ATTR_DISABLED;
            $ressource->setAttr($key, $value);
        }

        return $ressource;
    }

    /**
     * @return bool|mixed|Ressource|void
     */
    protected function process()
    {
        /**
         *
         */
        $model = $this->getModel();

        /**
         * create ressource in KERIO
         */
        $result = $this->api->ressource->create($model);

        /**
         * problem with create ressource
         */
        if(!$result)
        {
            $this->setError($this->api->ressource->getLastResult()->getLastErrorCode());
            return false;
        }

        return $result;
    }
}