<?php
namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Providers;
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\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\App\Http\Admin\ProductConfiguration;
use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\ServiceFactory;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
use function ThurData\Servers\KerioEmail\Core\Helper\di;
use ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 10.09.19
 * Time: 13:06
 * Class RessourceDataProvider
 */
class RessourceDataProvider extends BaseDataProvider
{

    public function read()
    {
        /**
         * hosting id
         */
        $hid = $this->request->get('id');

        $hosting = Hosting::where('id', $hid)->first();
        //todo refactor
        $this->data['domain'] = $hosting->domain;

        $lang = di('lang');
        $this->availableValues['status'] = [
            Kerio::ACC_STATUS_ACTIVE        => $lang->absoluteT('kerio','account','status','active'),
            Kerio::ACC_STATUS_LOCKED        => $lang->absoluteT('kerio','account','status','locked'),
            Kerio::ACC_STATUS_MAINTENANCE   => $lang->absoluteT('kerio','account','status','maintenance'),
            Kerio::ACC_STATUS_CLOSED        => $lang->absoluteT('kerio','account','status','closed'),
            Kerio::ACC_STATUS_LOCKOUT       => $lang->absoluteT('kerio','account','status','lockout'),
            Kerio::ACC_STATUS_PENDING       => $lang->absoluteT('kerio','account','status','pending')
        ];

        $this->availableValues['type'] = [
            Kerio::RES_TYPE_LOCATION        => $lang->absoluteT('kerio','ressource','type','location'),
            Kerio::RES_TYPE_EQUIPMENT       => $lang->absoluteT('kerio','ressource','type','equipment')
        ];

    }

    public function create()
    {
        /**
         * hosting id
         */
        $hid = $this->request->get('id');

        $fieldToProtection = [
            'display_name',
            'status',
            'type',
            'capacity',
            'description',
            'notes',
            'contact',
            'site',
            'contact_mail',
            'contact_phone',
            'street',
            'building',
            'floor',
            'room',
            'post_code',
            'town',
            'state',
            'county',
            'auto_accept',
            'auto_busy'
        ];

        foreach ($this->formData as $field => &$value)
        {
            $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
        }

        /**
         * product manager allow to check product settings
         */
        $productManager = new ProductManager();
        $productManager->loadByHostingId($hid);

        /**
         *
         * get soap create domain  service
         */
        $service =(new KerioManager())
            ->getApiByHosting($hid)
            ->soap
            ->service()
            ->createRessource();
        /**
         *
         * set product manager & form data to service
         */
        $service
            ->setProductManager($productManager)
            ->setFormData($this->formData);

        /**
         *
         * run service
         */
        $result = $service->run();

        /**
         *
         * return success or error response
         */
        if(!$result)
        {
            return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
        }

        return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenAdded')->setStatusSuccess();

    }

    public function updateStatus()
    {


    }

    public function update()
    {
        /**
         * hosting id
         */
        $hid = $this->request->get('id');

        /**
         * product manager allow to check product settings
         */
        $productManager = new ProductManager();
        $productManager->loadByHostingId($hid);

        /**
         *
         * get soap create domain  service
         */
        $service =(new KerioManager())
            ->getApiByHosting($hid)
            ->soap
            ->service()
            ->updateRessourceStatus()
            ->setProductManager($productManager)
        ;
        /**
         *
         * set product manager & form data to service
         */

        /**
         * run service for each id
         */
        foreach($this->request->get('massActions') as $id)
        {
            $service->setFormData(['status' => $this->formData['status'], 'id' => $id]);
            $result = $service->run();
        }

        /**
         * return success
         */
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('massRessourceStatusHasBeenUpdated')->setStatusSuccess();
    }
}