<?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\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\ExportSettingsFormProcessAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\View\FormView;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\FormFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Forms\Form\FormWithFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\Config\CeneoConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareAttributeConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareCategoryConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Menu\SettingsMenu;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\UcpSettingsForm;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\UcpSettingsDataProvider;
class ExportSettingsAction implements Displayable, Registrable
{
    private Config $config;
    private Renderer $renderer;
    private Request $request;
    private DataProviderFactory $data_provider_factory;
    private FormFactory $form_factory;
    private PriceCompareFactory $price_compare_factory;
    private SettingsMenu $settings_menu;
    private PriceCompareConfig $price_compare_config;
    public function __construct(Config $config, Request $request, Renderer $renderer, FormFactory $form, DataProviderFactory $data_provider_factory, PriceCompareFactory $price_compare_factory, SettingsMenu $settings_menu)
    {
        $this->config = $config;
        $this->renderer = $renderer;
        $this->request = $request;
        $this->data_provider_factory = $data_provider_factory;
        $this->form_factory = $form;
        $this->price_compare_factory = $price_compare_factory;
        $this->settings_menu = $settings_menu;
    }
    public function register(): array
    {
        return [ExportSettingsFormProcessAction::class];
    }
    public function show()
    {
        $template = null;
        $tab = $this->get_tab_value();
        if ($tab === SettingsMenu::MENU_UCP_TAB) {
            $template = 'Settings/Ucp';
        } else {
            $config = $this->get_price_compare_config();
            if ($tab === SettingsMenu::MENU_ATTRIBUTES_TAB && $config instanceof PriceCompareAttributeConfig) {
                $template = $config->get_attribute_template_name();
            } elseif ($config instanceof PriceCompareCategoryConfig) {
                $template = $config->get_category_template_name();
            }
        }
        if (\null === $template) {
            return;
        }
        $form = $this->get_form();
        $data = ['title' => __('Settings', 'dropshipping-export-free'), 'edit' => \true, 'mode' => $this->request->get_param('get.mode')->getAsString(), 'mapper_img_assets' => $this->config->get_param('assets.img.core_dir_url')->get() . 'mapper/', 'renderer' => $this->renderer, 'form' => new FormView($form, $this->renderer), 'menu' => $this->settings_menu];
        $this->renderer->output_render($template, $data);
    }
    private function get_form(): FormWithFields
    {
        $tab = $this->get_tab_value();
        if ($tab === SettingsMenu::MENU_UCP_TAB) {
            $class_name_data_provider = UcpSettingsDataProvider::class;
            $class_name = UcpSettingsForm::class;
        } else {
            $config = $this->get_price_compare_config();
            $class_name = '';
            $class_name_data_provider = '';
            if ($tab === SettingsMenu::MENU_ATTRIBUTES_TAB && $config instanceof PriceCompareAttributeConfig) {
                $class_name_data_provider = $config->get_attribute_data_provider_class();
                $class_name = $config->get_attribute_form_class();
            } elseif ($config instanceof PriceCompareCategoryConfig) {
                $class_name_data_provider = $config->get_category_data_provider_class();
                $class_name = $config->get_category_form_class();
            }
        }
        $data_provider = $this->data_provider_factory->create_by_class_name($class_name_data_provider);
        $form = $this->form_factory->create_by_class_name($class_name);
        $form->set_data($data_provider);
        return $form;
    }
    private function get_tab_value(): string
    {
        $tab = $this->request->get_param('get.tab')->getAsString();
        $tab = !empty($tab) ? $tab : SettingsMenu::MENU_CATEGORIES_TAB;
        return $tab;
    }
    private function get_price_compare_config(): PriceCompareConfig
    {
        if (isset($this->price_compare_config)) {
            return $this->price_compare_config;
        }
        $type = $this->request->get_param('get.type')->getAsString();
        $type = !empty($type) ? $type : CeneoConfig::ID;
        $this->price_compare_config = $this->price_compare_factory->get_config_by_id($type);
        return $this->price_compare_config;
    }
}
