<?php
/**
 * Class ConfigurableOptionManager
 * User: Nessandro
 * Date: 2019-09-29
 * Time: 15:33
 * @package ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\ProductConfiguration\Providers
 */

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


use ModulesGarden\Servers\ZimbraEmail\App\Enums\ProductParams;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\ZimbraEmail\App\Services\ConfigurableOptions\ConfigurableOptions;
use ModulesGarden\Servers\ZimbraEmail\App\Services\ConfigurableOptions\Strategy\ConfigOptionsType;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\ProductConfiguration\Helper\ConfigurableOptionsBuilder;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

class ConfigurableOptionManager extends BaseDataProvider implements AdminArea
{
    public function read()
    {
        $productId = $this->getRequestValue('id');

        $productManager = new ProductManager();
        $productManager
            ->loadById($productId);

        $options = new ConfigOptionsType();
        $options->setType($productManager->get(ProductParams::CLASS_OF_SERVICE_NAME));
        $options->setProductId($productId);
        $options->load();

        $configurableOption = new ConfigurableOptions($productId);
        ConfigurableOptionsBuilder::buildAll($configurableOption, $options->getConfigurableOptions());

        $this->data         = [
            'fields' => $configurableOption->show()
        ];
    }

    /**
     * @return HtmlDataJsonResponse|void
     */
    public function create()
    {
        $productId = $this->getRequestValue('id');

        try
        {
            $productManager = new ProductManager();
            $productManager
                ->loadById($productId);

            $options = new ConfigOptionsType();
            $options->setType($productManager->get(ProductParams::CLASS_OF_SERVICE_NAME));
            $options->setProductId($productId);
            $options->load();

            $configurableOption = new ConfigurableOptions($productId);

            ConfigurableOptionsBuilder::build($configurableOption, $this->formData, $options->getConfigurableOptions());
            $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()
    {

    }
}