<?php

namespace ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailForwarder\Providers;

use Exception;
use ModulesGarden\Servers\OpenXchange\App\Libs\RestApi;
use ModulesGarden\Servers\OpenXchange\App\Helpers\Helper;
use ModulesGarden\Servers\OpenXchange\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\OpenXchange\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\OpenXchange\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
use ModulesGarden\Servers\OpenXchange\App\Libs\Zimbra\Components\Api\Soap\Services\Delete\DeleteForwarder;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 18.09.19
 * Time: 11:28
 * Class DeleteForwarderDataProvider
 */
class DeleteForwarderDataProvider extends BaseDataProvider
{

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

    public function update()
    {
    }

    public function delete()
    {
        try{
            $params = Helper::moduleParams($_REQUEST['id']);
            $api = new RestApi($params);
            $api->deleteForwarder(Helper::convertEmailToPunnycode($this->formData['id']));
            return (new HtmlDataJsonResponse())->setMessageAndTranslate('forwardingAliasHasBeenDeleted')->setStatusSuccess();
        } catch(Exception $e)
        {
            return (new HtmlDataJsonResponse())->setMessageAndTranslate('forwardingAliasDeletedError')->setStatusError();
        }
    }
}