<?php

namespace ModulesGarden\Servers\OpenXchange\App\UI\Admin\ProductConfiguration\Providers;

use Exception;
use ModulesGarden\Servers\OpenXchange\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\OpenXchange\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\OpenXchange\App\Service\ConfigurableOptions\ConfigurableOptions;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
use ModulesGarden\Servers\OpenXchange\App\UI\Admin\ProductConfiguration\Helper\ConfigurableOptionsBuilder;

class ConfigurableOptionManage extends BaseDataProvider implements AdminArea
{

    public function read()
    {
        $configurableOption = new ConfigurableOptions($this->getRequestValue('id', 0));
        ConfigurableOptionsBuilder::buildAll($configurableOption);
        $this->data         = [
            'fields' => $configurableOption->show()
        ];
    }

    public function create()
    {
        try
        {
            $configurableOption = new ConfigurableOptions($this->getRequestValue('id', 0));
            ConfigurableOptionsBuilder::build($configurableOption, $this->formData);
            $status             = $configurableOption->createOrUpdate();
            $msg                = ($status) ? 'configurableOptionsCreate' : 'configurableOptionsUpdate';
            return (new HtmlDataJsonResponse())
                            ->setStatusSuccess()
                            ->setCallBackFunction('redirectToConfigurableOptionsTab')
                            ->setMessageAndTranslate($msg);
        }
        catch (Exception $ex)
        {
            return (new HtmlDataJsonResponse())
                            ->setStatusError()
                            ->setMessage($ex->getMessage());
        }
    }

    public function delete()
    {
        
    }

    public function update()
    {
        
    }

}
