<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
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\DAO\ExportDAO;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportManagerViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportOptionsDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\EntityCreator\ExportEntityCreatorService;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Provider\Abstraction\DataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Locator\FileLocatorService;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportFileTypeDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportLogicDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportMapperDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Entity\Export;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportOptionsFormFields;
class ExportManagerCloneProcessAction implements Conditional, Initable
{
    private Request $request;
    private PluginHelper $plugin_helper;
    private DataProviderFactory $data_provider_factory;
    private ExportEntityCreatorService $export_creator_service;
    public function __construct(Request $request, PluginHelper $plugin_helper, ExportEntityCreatorService $export_creator_service, DataProviderFactory $data_provider_factory)
    {
        $this->request = $request;
        $this->plugin_helper = $plugin_helper;
        $this->export_creator_service = $export_creator_service;
        $this->data_provider_factory = $data_provider_factory;
    }
    public function isActive(): bool
    {
        return $this->request->get_param('get.clone')->isString() && $this->plugin_helper->is_plugin_page($this->request->get_param('get.page')->getAsString());
    }
    public function init()
    {
        $this->validate_form_data();
        $this->clone_export();
    }
    private function clone_export()
    {
        $old_uid = $this->request->get_param('get.clone')->getAsString();
        $uid = $this->generate_uid();
        $this->clone_export_data($old_uid, $uid);
        $this->export_creator_service->create_export($uid, Export::STATUS_STOPPED);
        $url = $this->plugin_helper->generate_url_by_view(ExportManagerViewAction::class);
        wp_safe_redirect($url);
        exit;
    }
    private function clone_export_data(string $from_uid, string $to_uid)
    {
        $args = ['postfix' => $from_uid];
        $data_providers = [$this->data_provider_factory->create_by_class_name(ExportFileTypeDataProvider::class, $args), $this->data_provider_factory->create_by_class_name(ExportLogicDataProvider::class, $args), $this->data_provider_factory->create_by_class_name(ExportMapperDataProvider::class, $args), $this->data_provider_factory->create_by_class_name(ExportOptionsDataProvider::class, $args)];
        foreach ($data_providers as $data_provider) {
            $this->create_data_provider_copy($data_provider, $to_uid);
        }
    }
    private function create_data_provider_copy(DataProvider $data_provider, string $uid)
    {
        try {
            $data_provider_data = $data_provider->get_all();
            $data_provider_class = get_class($data_provider);
            $args = ['postfix' => $uid];
            $new_data_provider = $this->data_provider_factory->create_by_class_name($data_provider_class, $args);
            if (\is_array($data_provider_data)) {
                foreach ($data_provider_data as $key => $value) {
                    $new_data_provider->set($key, $value);
                }
                if ($data_provider_class === ExportFileTypeDataProvider::class) {
                    $new_data_provider->set(ExportFileTypeFormFields::UID, $uid);
                }
                if ($data_provider_class === ExportOptionsDataProvider::class) {
                    $name = $data_provider->get(ExportOptionsFormFields::EXPORT_NAME);
                    $new_data_provider->set(ExportOptionsFormFields::EXPORT_NAME, $name . ' ' . __('copy', 'dropshipping-export-free'));
                }
                $new_data_provider->save();
            }
        } catch (\Exception $e) {
            error_log($e->getMessage());
            // phpcs:ignore
        }
    }
    private function generate_uid(): string
    {
        return uniqid();
    }
    private function validate_form_data()
    {
        if (!wp_verify_nonce($this->request->get_param('get.nonce')->getAsString(), ExportManagerViewAction::MANAGER_NONCE)) {
            wp_die('Error, security code is not valid');
        }
        if (!current_user_can('manage_options')) {
            wp_die('Error, you are not allowed to do this action');
        }
    }
}
