<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportOptionsDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\ExportOptionsForm;
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\DAO\ExportDAO;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Scheduler\ExportSchedulerService;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportManagerViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportStatusViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportOptionsFormFields;
class ExportOptionsFormProcessAction implements Conditional, Initable
{
    private Request $request;
    private ExportOptionsForm $form;
    private PluginHelper $plugin_helper;
    private ExportDAO $export_dao;
    private ExportSchedulerService $scheduler;
    private DataProviderFactory $data_provider_factory;
    public function __construct(Request $request, ExportOptionsForm $form, DataProviderFactory $data_provider_factory, ExportDAO $import_dao, ExportSchedulerService $scheduler, PluginHelper $helper)
    {
        $this->request = $request;
        $this->form = $form;
        $this->plugin_helper = $helper;
        $this->export_dao = $import_dao;
        $this->scheduler = $scheduler;
        $this->data_provider_factory = $data_provider_factory;
    }
    public function isActive(): bool
    {
        $settings = $this->request->get_param('post.' . ExportOptionsForm::get_id());
        return $settings->isArray() && !$settings->isEmpty();
    }
    public function init()
    {
        $this->save_form_data();
    }
    private function change_schedule()
    {
        $uid = $this->request->get_param('get.uid')->get();
        if ($this->export_dao->is_uid_exists($uid)) {
            $file_export = $this->export_dao->find_by_uid($uid);
            $file_export->set_next_export($this->scheduler->estimate_time($uid));
            $file_export->set_cron_schedule($this->scheduler->get_formated_schedule($uid));
            $this->export_dao->update($file_export);
        }
    }
    private function change_export_name(string $new_name)
    {
        $uid = $this->request->get_param('get.uid')->get();
        if ($this->export_dao->is_uid_exists($uid)) {
            $file_export = $this->export_dao->find_by_uid($uid);
            if ($file_export->get_export_name() !== $new_name) {
                $file_export->set_export_name($new_name);
                $this->export_dao->update($file_export);
            }
        }
    }
    private function save_form_data()
    {
        $uid = $this->request->get_param('get.uid')->getAsString();
        $this->form->handle_request($this->request->get_param('post.' . ExportOptionsForm::get_id())->get());
        if ($this->form->is_valid() && current_user_can('manage_options')) {
            $data_provider = $this->data_provider_factory->create_by_class_name(ExportOptionsDataProvider::class, ['postfix' => $uid]);
            $data_provider->update($this->form);
            $this->change_schedule();
            $this->change_export_name($data_provider->get(ExportOptionsFormFields::EXPORT_NAME));
            $edit = $this->request->get_param('get.mode')->getAsString() === '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(ExportStatusViewAction::class, $args);
            $this->redirect($url);
        }
    }
    private function redirect(string $url)
    {
        wp_safe_redirect($url);
        exit;
    }
}
