<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\EmailAlias\Providers;


use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias;
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: 18.09.19
 * Time: 12:59
 * Class DeleteEmailAliasDataProvider
 */
class DeleteEmailAliasDataProvider extends BaseDataProvider
{

    public function read()
    {
        $params = json_decode(base64_decode($this->actionElementId), true);

        $this->data['id'] = $params['accId'];
        $this->data['alias'] = $params['alias'];
    }

    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 alias service
         * set service configuration
         */
        $service =(new ZimbraManager())
            ->getApiByHosting($hid)
            ->soap
            ->service()
            ->deleteAccountAlias()
            ->setFormData($this->formData);

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

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

        return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAliasHasBeenDeleted')->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 alias service
         * set service configuration
         */
        $service = (new ZimbraManager())
            ->getApiByHosting($hid)
            ->soap
            ->service()
            ->deleteAccountAlias();

        foreach ($this->request->get('massActions') as $endodedData)
        {
            $data = json_decode(base64_decode($endodedData), true);
            $data['id'] = $data['accId'];
            $service->setFormData($data);
            $result = $service->run();
        }

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

    }
}