<?php
namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update;
use ThurData\Servers\KerioEmail\App\Enums\Size;
use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
use ThurData\Servers\KerioEmail\App\Enums\Kerio;
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\Services\Create\CreateRessource;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 18.09.19
 * Time: 11:09
 * Class UpdateRessource
 */
class UpdateRessource extends CreateRessource
{

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

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

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

        return $result;
    }

    /**
     * @return Ressource
     */
    public function getModel()
    {
        $ressource = new Ressource();
        /**
         *  set ressource attributes
         */
        $ressource->setId($this->formData['id']);

        $ressource->setAttr(Ressource::ATTR_NAME, $this->formData['name']);
        $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' );

        return $ressource;

    }
}