<?php

namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Providers;


use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Delete\DeleteRessource;
use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 18.09.19
 * Time: 11:28
 * Class DeleteRessourceDataProvider
 */
class DeleteRessourceDataProvider extends BaseDataProvider
{

    public function read()
    {
        $this->data['id'] = $this->actionElementId;
    }

    public function update()
    {
        // TODO: Implement update() method.
    }

    public function delete()
    {

        /**
         * 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()
            ->deleteRessource();
        /**
         *
         * set product manager & form data to service
         */
        $service
            ->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('ressourceHasBeenDeleted')->setStatusSuccess();

    }

    public function massDelete()
    {
        /**
         * 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()
            ->deleteRessource();

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

        return (new HtmlDataJsonResponse())->setMessageAndTranslate('massRessourceHasBeenDeleted')->setStatusSuccess();
    }
}