<?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\Action\View\ExportManagerViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportOptionsViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportFileTypeDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\FormFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
class ExportMapperFormProcessAction implements Conditional, Initable
{
    private Request $request;
    private FormFactory $form_factory;
    private PluginHelper $plugin_helper;
    private DataProviderFactory $data_provider_factory;
    private PriceCompareFactory $price_compare_factory;
    private PriceCompareConfig $price_compare_config;
    public function __construct(Request $request, FormFactory $form_factory, DataProviderFactory $data_provider_factory, PluginHelper $helper, PriceCompareFactory $price_compare_factory)
    {
        $this->request = $request;
        $this->form_factory = $form_factory;
        $this->plugin_helper = $helper;
        $this->data_provider_factory = $data_provider_factory;
        $this->price_compare_factory = $price_compare_factory;
    }
    public function isActive(): bool
    {
        $config = $this->get_price_compare_config();
        $form_classname = $config->get_mapper_form_class();
        $settings = $this->request->get_param('post.' . $form_classname::get_id());
        return $settings->isArray() && !$settings->isEmpty();
    }
    public function init()
    {
        $this->save_form_data();
    }
    private function save_form_data()
    {
        $args = [];
        $config = $this->get_price_compare_config();
        $uid = $this->request->get_param('get.uid')->getAsString();
        $form_classname = $config->get_mapper_form_class();
        $data_provider_classname = $config->get_mapper_data_provider_class();
        $form = $this->form_factory->create_by_class_name($form_classname);
        $form->handle_request($this->request->get_param('post.' . $form->get_form_id())->get());
        if ($form->is_valid() && current_user_can('manage_options')) {
            $data_provider = $this->data_provider_factory->create_by_class_name($data_provider_classname, ['postfix' => $uid]);
            $data_provider->update($form);
            $edit = $this->request->get_param('get.mode')->get() === 'edit';
            $mode = $this->request->get_param('get.mode')->getAsString();
            if (!empty($mode)) {
                $args = ['uid' => $uid, 'mode' => $mode];
            } else {
                $args = ['uid' => $uid];
            }
            $url = $edit ? $this->plugin_helper->generate_url_by_view(ExportManagerViewAction::class) : $this->plugin_helper->generate_url_by_view(ExportOptionsViewAction::class, $args);
            $this->redirect($url);
        }
    }
    private function get_price_compare_config(): PriceCompareConfig
    {
        if (isset($this->price_compare_config)) {
            return $this->price_compare_config;
        }
        $type_provider = $this->data_provider_factory->create_by_class_name(ExportFileTypeDataProvider::class, ['postfix' => $this->request->get_param('get.uid')->getAsString()]);
        $type = $type_provider->get(ExportFileTypeFormFields::EXPORT_TYPE);
        $this->price_compare_config = $this->price_compare_factory->get_config_by_id($type);
        return $this->price_compare_config;
    }
    private function redirect(string $url)
    {
        wp_safe_redirect($url);
        exit;
    }
}
