<?php

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

use DropshippingExportFreeVendor\WPDesk\Forms\FieldProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Component\CategoryAttributeComponent;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\NoOnceField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\SubmitField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Field\SelectOptField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ProductFieldsValueFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\CustomAttributesProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\CategoryCeneoDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\Config\CeneoConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareCategoryConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\EanProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\SkuProductFieldValue;
class AttributesCeneoFormFields implements FieldProvider
{
    public const FIELD_NONCE_ACTION = 'settings_attributes_ceneo_action';
    public const FIELD_NONCE_NAME = 'settings_attributes_ceneo_nonce';
    public const FIELD_SUBMIT = 'save';
    private const INTEGRATION_FIELD_PRODUCER = 'Producent';
    private const INTEGRATION_FIELD_SKU = 'SKU';
    private const INTEGRATION_FIELD_EAN = 'EAN';
    private const INTEGRATION_FIELD_ISBN = 'ISBN';
    private const INTEGRATION_FIELD_OTC = 'OTC';
    public const FIELD_INTEGRATION_COMPONENT = 'integration';
    public const FIELD_CATEGORIES_COMPONENT = 'category_';
    private array $select_options;
    private ProductFieldsValueFactory $product_fields_value_factory;
    private CategoryCeneoDataProvider $category_data_provider;
    private CeneoConfig $ceneo_config;
    public function __construct(ProductFieldsValueFactory $product_fields_value_factory, CategoryCeneoDataProvider $category_data_provider, CeneoConfig $ceneo_config)
    {
        $this->product_fields_value_factory = $product_fields_value_factory;
        $this->category_data_provider = $category_data_provider;
        $this->ceneo_config = $ceneo_config;
    }
    /**
     * @see FieldProvider::get_fields()
     */
    public function get_fields()
    {
        $result = [];
        $result[] = $this->create_integration_component();
        $result = \array_merge($result, $this->create_categories_components());
        $result[] = (new NoOnceField(self::FIELD_NONCE_ACTION))->set_name(self::FIELD_NONCE_NAME);
        $result[] = (new SubmitField())->set_name(self::FIELD_SUBMIT)->set_label(__('Apply settings', 'dropshipping-export-free'))->add_class('button button-hero button-primary')->set_attribute('id', self::FIELD_SUBMIT);
        return $result;
    }
    private function create_categories_components(): array
    {
        $result = [];
        try {
            $selected_categories = $this->category_data_provider->get_all();
            if ($this->ceneo_config instanceof PriceCompareCategoryConfig) {
                foreach ($selected_categories as $wc_category_id => $xml_category_id) {
                    $items = [];
                    $attributes = $this->ceneo_config->get_category()->get_category_attributes((int) $xml_category_id);
                    $xml_category_name = $this->ceneo_config->get_category()->get_category_full_name((int) $xml_category_id);
                    if (!empty($xml_category_name) && !empty($attributes)) {
                        foreach ($attributes as $attribute) {
                            $attribute_name = (string) $attribute['Name'];
                            $attribute_postfix = (string) $attribute['IsKeyAttribute'] === 'True' ? '*' : '';
                            $items[] = (new SelectOptField())->set_label($attribute_name . $attribute_postfix)->set_attribute('id', self::FIELD_CATEGORIES_COMPONENT . '_' . $attribute_name)->set_name($attribute_name)->add_class('dropshipping-select2')->set_options($this->generate_select_options());
                        }
                        $result[] = (new CategoryAttributeComponent())->set_items($items)->set_name(self::FIELD_CATEGORIES_COMPONENT . $wc_category_id)->set_label($xml_category_name);
                    }
                }
            }
        } catch (\Exception $e) {
            $result = [];
        }
        return \is_array($result) ? $result : [];
    }
    private function create_integration_component(): CategoryAttributeComponent
    {
        $fileds = [self::INTEGRATION_FIELD_PRODUCER => \esc_attr__('Producer', 'dropshipping-export-free'), self::INTEGRATION_FIELD_SKU => \esc_attr__('SKU', 'dropshipping-export-free'), self::INTEGRATION_FIELD_EAN => \esc_attr__('EAN', 'dropshipping-export-free'), self::INTEGRATION_FIELD_ISBN => \esc_attr__('ISBN', 'dropshipping-export-free'), self::INTEGRATION_FIELD_OTC => \esc_attr__('OTC', 'dropshipping-export-free')];
        $items = [];
        foreach ($fileds as $key => $value) {
            $items[] = (new SelectOptField())->set_label($value)->set_attribute('id', self::FIELD_INTEGRATION_COMPONENT . '_' . $key)->set_name($key)->add_class('dropshipping-select2')->set_options($this->generate_select_options());
        }
        return (new CategoryAttributeComponent())->set_items($items)->set_name(self::FIELD_INTEGRATION_COMPONENT)->set_label(__('Integration attributes', 'dropshipping-export-free'));
    }
    private function generate_select_options(): array
    {
        if (isset($this->select_options)) {
            return $this->select_options;
        }
        $this->select_options = [];
        $this->select_options[] = ['' => \esc_attr__('Select field', 'dropshipping-export-free')];
        $this->select_options[\esc_attr__('Product fields', 'dropshipping-export-free')] = $this->product_fields_value_factory->get_selected_key_and_values([EanProductFieldValue::get_key(), SkuProductFieldValue::get_key()]);
        $attribute_taxonomies_options = [];
        foreach (wc_get_attribute_taxonomies() as $tax) {
            $attribute_taxonomies_options[wc_attribute_taxonomy_name($tax->attribute_name)] = $tax->attribute_label;
        }
        $this->select_options[\esc_attr__('Attributes fields', 'dropshipping-export-free')] = $attribute_taxonomies_options;
        return $this->select_options;
    }
}
