<?php

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

use DropshippingExportFreeVendor\WPDesk\Forms\Field\HiddenField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\CheckboxField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\InputTextField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ProductFieldsValueFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Field\SelectOptField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\OptionalProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldDefaultValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\NoProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\CustomTextProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\GmcConditionProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\GmcIdentifierExistsProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\GmcShippingProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\GpsrProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\ResponsiblePersonProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\ResponsibleProducersProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\SafetyInstructionsProductFieldValue;
class PriceCompareProductFieldsComponent extends \DropshippingExportFreeVendor\WPDesk\Forms\Field\BasicField
{
    public const PRODUCT_FIELDS = 'product_fields';
    public const FIELD_ACTIVE = 'is_active';
    public const FIELD_VALUE = 'value';
    public const FIELD_CUSTOM_VALUE = 'custom_value';
    public const FIELD_KEY = 'key';
    private array $product_fields = [];
    private array $items = [];
    private array $select_options;
    private ProductFieldsValueFactory $product_fields_value_factory;
    public function __construct(ProductFieldsValueFactory $product_fields_value_factory)
    {
        parent::__construct();
        $this->attributes['multiple'] = \true;
        $this->product_fields_value_factory = $product_fields_value_factory;
        $this->set_name(self::PRODUCT_FIELDS);
    }
    public function set_product_fields(array $product_fields)
    {
        $this->product_fields = $product_fields;
        return $this;
    }
    private function generate_items(): array
    {
        $items = [];
        if (empty($this->product_fields)) {
            return $items;
        }
        foreach ($this->product_fields as $field) {
            if (is_subclass_of($field, ProductField::class)) {
                $checkbox = (new CheckboxField())->add_class('product_field_checkbox')->set_name(self::FIELD_ACTIVE)->set_default_value(CheckboxField::VALUE_TRUE);
                if (is_subclass_of($field, OptionalProductField::class)) {
                    if (\false === $field::is_optional()) {
                        $checkbox->set_readonly();
                    }
                }
                $select = (new SelectOptField())->set_name(self::FIELD_VALUE)->set_label($field::get_default_field_nice_name())->add_class('width-100 dropshipping-select2 product-field-value-selector')->set_options($this->generate_select_options());
                if (is_subclass_of($field, ProductFieldDefaultValue::class)) {
                    $value_class = $field::get_default_class_value();
                    $select->set_default_value($value_class::get_key());
                    if (\false === $field::is_allowed_to_change()) {
                        $select->set_readonly();
                    }
                }
                $items[] = [$checkbox, $select, (new InputTextField())->add_class('width-100 margin-top-md hidden custom-value')->set_name(self::FIELD_CUSTOM_VALUE), (new HiddenField())->set_name(self::FIELD_KEY)->set_default_value($field::get_default_field_name())];
            }
        }
        return $items;
    }
    public function get_items(): array
    {
        if (empty($this->items)) {
            $this->items = $this->generate_items();
        }
        return $this->items;
    }
    public function get_template_name(): string
    {
        return 'price-compare-product-fields-component';
    }
    private function generate_select_options(): array
    {
        if (isset($this->select_options)) {
            return $this->select_options;
        }
        $this->select_options = [];
        $this->select_options[] = [NoProductFieldValue::get_key() => NoProductFieldValue::get_name()];
        $this->select_options[] = [CustomTextProductFieldValue::get_key() => CustomTextProductFieldValue::get_name()];
        $this->select_options[\esc_attr__('Product fields', 'dropshipping-export-free')] = $this->product_fields_value_factory->get_key_and_values([NoProductFieldValue::get_key(), CustomTextProductFieldValue::get_key(), GmcConditionProductFieldValue::get_key(), GmcIdentifierExistsProductFieldValue::get_key(), GmcShippingProductFieldValue::get_key(), ResponsiblePersonProductFieldValue::get_key(), ResponsibleProducersProductFieldValue::get_key(), SafetyInstructionsProductFieldValue::get_key(), GpsrProductFieldValue::get_key()]);
        $this->select_options[\esc_attr__('Google Merchant Center', 'dropshipping-export-free')] = [GmcConditionProductFieldValue::get_key() => GmcConditionProductFieldValue::get_name(), GmcIdentifierExistsProductFieldValue::get_key() => GmcIdentifierExistsProductFieldValue::get_name(), GmcShippingProductFieldValue::get_key() => GmcShippingProductFieldValue::get_name()];
        $this->select_options[\esc_attr__('Ceneo', 'dropshipping-export-free')] = [GpsrProductFieldValue::get_key() => GpsrProductFieldValue::get_name()];
        $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;
    }
}
