<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Process\Form;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Conditional\Conditional;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Initable\Initable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\FormFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Form\Abstraction\FormIdentity;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\Config\CeneoConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareCategoryConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Menu\SettingsMenu;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareAttributeConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\UcpSettingsForm;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\UcpSettingsDataProvider;
class ExportSettingsFormProcessAction implements Conditional, Initable
{
    private Request $request;
    private PluginHelper $plugin_helper;
    private DataProviderFactory $data_provider_factory;
    private PriceCompareFactory $price_compare_factory;
    private FormFactory $form_factory;
    public function __construct(Request $request, DataProviderFactory $data_provider_factory, PluginHelper $helper, PriceCompareFactory $price_compare_factory, FormFactory $form_factory)
    {
        $this->request = $request;
        $this->plugin_helper = $helper;
        $this->data_provider_factory = $data_provider_factory;
        $this->price_compare_factory = $price_compare_factory;
        $this->form_factory = $form_factory;
    }
    public function isActive(): bool
    {
        $settings = $this->request->get_param('post');
        return $settings->isArray() && !$settings->isEmpty() && $this->plugin_helper->is_plugin_page($this->request->get_param('get.page')->getAsString());
    }
    private function get_price_compare_config(): PriceCompareConfig
    {
        $type = $this->request->get_param('get.type')->getAsString();
        $type = !empty($type) ? $type : CeneoConfig::ID;
        return $this->price_compare_factory->get_config_by_id($type);
    }
    public function init()
    {
        $this->save_form_data();
    }
    private function save_form_data()
    {
        $class_name_data_provider = null;
        $class_name = null;
        $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();
            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();
            }
        }
        if (!empty($class_name_data_provider) && !empty($class_name)) {
            $form = $this->form_factory->create_by_class_name($class_name);
            if ($form instanceof FormIdentity) {
                $file_form_id = $form::get_id();
                $form->handle_request($this->request->get_param('post.' . $file_form_id)->get());
                if ($form->is_valid() && current_user_can('manage_options')) {
                    $data_provider = $this->data_provider_factory->create_by_class_name($class_name_data_provider);
                    $data_provider->update($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;
    }
}
