<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View;

use DropshippingExportFreeVendor\WPDesk\View\Renderer\Renderer;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Registrable\Registrable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\View\Abstraction\Displayable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Process\Form\ExportMapperFormProcessAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\FormFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\View\FormView;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportFileTypeDataProvider;
class ExportMapperViewAction implements Displayable, Registrable
{
    private Config $config;
    private Renderer $renderer;
    private FormFactory $form_factory;
    private PluginHelper $plugin_helper;
    private Request $request;
    private DataProviderFactory $data_provider_factory;
    private PriceCompareConfig $price_compare_config;
    private PriceCompareFactory $price_compare_factory;
    public function __construct(Config $config, Request $request, Renderer $renderer, FormFactory $form_factory, DataProviderFactory $data_provider_factory, PriceCompareFactory $price_compare_factory, PluginHelper $helper)
    {
        $this->config = $config;
        $this->renderer = $renderer;
        $this->plugin_helper = $helper;
        $this->request = $request;
        $this->form_factory = $form_factory;
        $this->data_provider_factory = $data_provider_factory;
        $this->price_compare_factory = $price_compare_factory;
    }
    public function register(): array
    {
        return [ExportMapperFormProcessAction::class];
    }
    public function show()
    {
        $uid = $this->request->get_param('get.uid')->getAsString();
        $config = $this->get_price_compare_config();
        $data_provider = $this->data_provider_factory->create_by_class_name($config->get_mapper_data_provider_class(), ['postfix' => $uid]);
        $form = $this->form_factory->create_by_class_name($config->get_mapper_form_class());
        $form->set_data($data_provider);
        $data = ['title' => __('Product mapper', 'dropshipping-export-free'), 'edit' => \true, 'mode' => $this->request->get_param('get.mode')->getAsString(), 'form' => new FormView($form, $this->renderer), 'previous_step' => $this->plugin_helper->generate_url_by_view(ExportLogicViewAction::class, ['uid' => $this->request->get_param('get.uid')->get()]), 'mapper_img_assets' => $this->config->get_param('assets.img.core_dir_url')->get() . 'mapper/', 'renderer' => $this->renderer];
        $this->renderer->output_render($config->get_mapper_template_name(), $data);
    }
    private function get_price_compare_config(): PriceCompareConfig
    {
        if (isset($this->price_compare_config)) {
            return $this->price_compare_config;
        }
        $uid = $this->request->get_param('get.uid')->getAsString();
        $file_type_data_provider = $this->data_provider_factory->create_by_class_name(ExportFileTypeDataProvider::class, ['postfix' => $uid]);
        $type = $file_type_data_provider->get(ExportFileTypeFormFields::EXPORT_TYPE);
        $this->price_compare_config = $this->price_compare_factory->get_config_by_id($type);
        return $this->price_compare_config;
    }
}
