<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\Ressource\Providers;
use ModulesGarden\Servers\ZimbraEmail\App\Enums\ProductParams;
use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Http\Admin\ProductConfiguration;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\ServiceFactory;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Ressource;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository\ClassOfServices;
use ModulesGarden\Servers\ZimbraEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\di;
use ModulesGarden\Servers\ZimbraEmail\Core\Http\JsonResponse;
use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * 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'] = [
            Zimbra::ACC_STATUS_ACTIVE        => $lang->absoluteT('zimbra','account','status','active'),
            Zimbra::ACC_STATUS_LOCKED        => $lang->absoluteT('zimbra','account','status','locked'),
            Zimbra::ACC_STATUS_MAINTENANCE   => $lang->absoluteT('zimbra','account','status','maintenance'),
            Zimbra::ACC_STATUS_CLOSED        => $lang->absoluteT('zimbra','account','status','closed'),
            Zimbra::ACC_STATUS_LOCKOUT       => $lang->absoluteT('zimbra','account','status','lockout'),
            Zimbra::ACC_STATUS_PENDING       => $lang->absoluteT('zimbra','account','status','pending')
        ];

        $this->availableValues['type'] = [
            Zimbra::RES_TYPE_LOCATION        => $lang->absoluteT('zimbra','ressource','type','location'),
            Zimbra::RES_TYPE_EQUIPMENT       => $lang->absoluteT('zimbra','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 ZimbraManager())
            ->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 ZimbraManager())
            ->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();
    }
}