<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields;

use DropshippingExportFreeVendor\WPDesk\Forms\Field\NoOnceField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SelectField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SubmitField;
use DropshippingExportFreeVendor\WPDesk\Forms\FieldProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\CheckboxField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\InputTextField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\RadioField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportFileTypeDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Locator\FileLocatorService;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
class ExportOptionsFormFields implements FieldProvider
{
    public const CRON_WEEK_DAY = 'cron_week_days';
    public const CRON_HOURS = 'cron_hours';
    public const NONCE_ACTION = 'product_options_action';
    public const NONCE_NAME = 'product_options_nonce';
    public const NEXT_STEP = 'next_step';
    public const FIELD_REMOVED_PRODUCTS = 'removed_products';
    public const EXPORT_NAME = 'export_name';
    public const PRODUCT_IN_BATCHES = 'product_in_batches';
    public const PRODUCT_COLUMN_SEPARATOR = 'product_column_separator';
    public const USER_ROLES = 'user_roles';
    public const FILE_LOCATION = 'file_location';
    public const FILE_LOCATION_INTERNAL = 'internal';
    public const FILE_LOCATION_INTERNAL_ID = 'file_location_internal';
    public const FILE_LOCATION_EXTERNAL = 'external';
    public const FILE_LOCATION_EXTERNAL_ID = 'file_location_external';
    public const FILE_FTP_PATH = 'ftp_path';
    public const SAVE_IMAGES_AS_ZIP = 'save_as_zip';
    public const DEFAULT_CSV_SEPARATOR = ',';
    public const DEFAULT_PRODUCT_IN_BATCHES = 100;
    private ExportFileTypeDataProvider $file_type_data_provider;
    private FileLocatorService $file_locator_service;
    private string $uid;
    private PriceCompareConfig $price_compare_config;
    public function __construct(DataProviderFactory $data_provider_factory, FileLocatorService $file_locator_service, PriceCompareFactory $price_compare_factory, string $uid)
    {
        $this->uid = $uid;
        $this->file_locator_service = $file_locator_service;
        $this->file_type_data_provider = $data_provider_factory->create_by_class_name(ExportFileTypeDataProvider::class, ['postfix' => $uid]);
        $type = $this->file_type_data_provider->get(ExportFileTypeFormFields::EXPORT_TYPE);
        $this->price_compare_config = $price_compare_factory->get_config_by_id($type);
    }
    /**
     * @see FieldProvider::get_fields()
     */
    public function get_fields()
    {
        $result = [(new InputTextField())->set_name(self::EXPORT_NAME)->set_label(__('Export Name', 'dropshipping-export-free'))->set_required()->set_default_value($this->get_default_name())->add_class('width-100'), (new InputTextField())->set_name(self::PRODUCT_IN_BATCHES)->set_label(__('Products in one batch', 'dropshipping-export-free'))->set_required()->set_default_value(self::DEFAULT_PRODUCT_IN_BATCHES)->add_class('width-100'), (new InputTextField())->set_name(self::PRODUCT_COLUMN_SEPARATOR)->set_label(__('Separator', 'dropshipping-export-free'))->set_attribute('maxlength', 1)->set_required()->set_default_value(self::DEFAULT_CSV_SEPARATOR)->add_class('width-100'), (new SelectField())->set_label(__('Cron schedule', 'dropshipping-export-free'))->set_name(self::CRON_WEEK_DAY)->add_class('dropshipping-select2 width-100')->set_description(__('Select the days of the week for the export to run automatically.', 'dropshipping-export-free'))->set_multiple()->set_options(self::get_week_days()), (new SelectField())->set_label('')->set_name(self::CRON_HOURS)->add_class('dropshipping-select2 width-100')->set_description(__('Select hours for the export to run automatically.', 'dropshipping-export-free'))->set_multiple()->set_options($this->get_hours_data()), (new RadioField())->set_name(self::FILE_LOCATION)->set_attribute('id', self::FILE_LOCATION_INTERNAL_ID)->set_attribute('type', 'radio')->set_default_value(self::FILE_LOCATION_INTERNAL)->set_label(__('Export to your server\'s FTP', 'dropshipping-export-free')), (new InputTextField())->set_name(self::FILE_FTP_PATH)->set_label(__('FTP path', 'dropshipping-export-free'))->set_description(__('Enter the file location.', 'dropshipping-export-free'))->set_default_value($this->file_locator_service->generate_tmp_file_path($this->uid, $this->price_compare_config->get_supported_extension(), \true))->add_class('width-100')];
        if ($this->price_compare_config->is_custom()) {
            $result[] = (new CheckboxField())->set_name(self::SAVE_IMAGES_AS_ZIP)->set_label(__('Export images', 'dropshipping-export-free'))->set_sublabel(__('Check to save images in a .zip file (next to the exported file).', 'dropshipping-export-free'));
        }
        $result[] = (new SubmitField())->set_name(self::NEXT_STEP)->set_label(__('Start Export &rarr;', 'dropshipping-export-free'))->add_class('button button-primary button-hero')->set_attribute('id', self::NEXT_STEP);
        $result[] = (new NoOnceField(self::NONCE_ACTION))->set_name(self::NONCE_NAME);
        return $result;
    }
    private function get_default_name(): string
    {
        return sprintf(__('File export %s', 'dropshipping-export-free'), current_time('Y-m-d H:i:s'));
    }
    public static function get_user_roles(): array
    {
        global $wp_roles;
        $roles = is_object($wp_roles) ? $wp_roles->roles : [];
        $result = [];
        foreach ($roles as $role_key => $role) {
            if (isset($role['name'])) {
                $result[$role_key] = $role['name'];
            }
        }
        return $result;
    }
    public static function get_week_days(): array
    {
        return ['1' => __('Monday', 'dropshipping-export-free'), '2' => __('Tuesday', 'dropshipping-export-free'), '3' => __('Wednesday', 'dropshipping-export-free'), '4' => __('Thursday', 'dropshipping-export-free'), '5' => __('Friday', 'dropshipping-export-free'), '6' => __('Saturday', 'dropshipping-export-free'), '7' => __('Sunday', 'dropshipping-export-free')];
    }
    private function get_hours_data(): array
    {
        $result = [];
        $minutes = ['00', '15', '30', '45'];
        for ($i = 0; $i < 24; $i++) {
            foreach ($minutes as $minute) {
                $time = $i . ':' . $minute;
                $result[$time] = $i . ':' . $minute;
            }
        }
        return $result;
    }
}
