<?php

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

use Exception;
use ModulesGarden\Servers\OpenXchange\App\Libs\RestApi;
use ModulesGarden\Servers\OpenXchange\App\Helpers\Helper;
use ModulesGarden\Servers\OpenXchange\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

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

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

    public function update()
    {
    }

    public function delete()
    {
        try
        {
            $params         = Helper::moduleParams($this->request->get('id'));
            $context        = new RestApi($params);
            $userIdPunycode = Helper::convertEmailToPunnycode($this->formData['id']);
            $result         = $context->deleteUser($userIdPunycode);

            if($result == '')
            {
                return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenDeleted')->setStatusSuccess();
            }
            else
            {
                return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountsDeleteError')->setStatusError();
            }
        }
        catch(Exception $e)
        {
            return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountsDeleteError')->setStatusError();
        }
    }
}
