<?php

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

use DropshippingExportFreeVendor\WPDesk\Forms\Field\SelectField;
use DropshippingExportFreeVendor\WPDesk\Forms\Field\InputTextField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\ContainsCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\EmptyCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\EqualCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\HigherCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\LowerCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\NotContainsCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\NotEmptyCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\NotEqualCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\CategoryField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\DescriptionField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\SKUField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\StockField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\TitleField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\Abstraction\AllowedConditions;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\IsInCategoryCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\IsNotInCategoryCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\IsInTagCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\IsNotInTagCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\TagsField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\PriceField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\SalePriceField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\EANField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Field\SelectLogicField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Integration\EanIntegration;
class ConditionalLogicComponent extends \DropshippingExportFreeVendor\WPDesk\Forms\Field\BasicField
{
    public const FIELD_TYPE = 'type';
    public const FIELD_VALUE = 'value';
    public const FIELD_VALUE_TYPE = 'value_type';
    private array $items;
    public function __construct()
    {
        parent::__construct();
        $this->attributes['multiple'] = \true;
    }
    public function get_items(): array
    {
        if (!isset($this->items)) {
            $this->items = [(new SelectLogicField())->set_label(__('Export product if', 'dropshipping-export-free'))->set_name(self::FIELD_TYPE)->add_class('select select2 short width-100 product-field')->set_attribute('type', 'select')->set_options(self::get_export_options()), (new SelectField())->set_label(__('value', 'dropshipping-export-free'))->set_name(self::FIELD_VALUE_TYPE)->add_class('select select2 short conditional-value-type width-100')->set_attribute('type', 'select')->set_options(self::get_value_types()), (new InputTextField())->add_class('width-100')->set_name(EqualCondition::get_name())->set_attribute('data-value', EqualCondition::get_name())->set_placeholder(__('String or value', 'dropshipping-export-free')), (new InputTextField())->add_class('width-100')->set_name(NotEqualCondition::get_name())->set_attribute('data-value', NotEqualCondition::get_name())->set_placeholder(__('String or value', 'dropshipping-export-free')), (new InputTextField())->add_class('width-100')->set_name(HigherCondition::get_name())->set_attribute('data-value', HigherCondition::get_name())->set_placeholder(__('Numeric value', 'dropshipping-export-free')), (new InputTextField())->add_class('width-100')->set_name(LowerCondition::get_name())->set_attribute('data-value', LowerCondition::get_name())->set_placeholder(__('Numeric value', 'dropshipping-export-free')), (new InputTextField())->add_class('width-100')->set_name(ContainsCondition::get_name())->set_attribute('data-value', ContainsCondition::get_name())->set_placeholder(__('String or value', 'dropshipping-export-free')), (new InputTextField())->add_class('width-100')->set_name(NotContainsCondition::get_name())->set_attribute('data-value', NotContainsCondition::get_name())->set_placeholder(__('String or value', 'dropshipping-export-free')), (new SelectField())->set_name(IsInCategoryCondition::get_name())->add_class('dropshipping-select2-multi width-100')->set_multiple()->set_attribute('type', 'select')->set_attribute('data-value', IsInCategoryCondition::get_name())->set_options($this->get_woocommerce_categories()), (new SelectField())->set_name(IsNotInCategoryCondition::get_name())->add_class('dropshipping-select2-multi width-100')->set_multiple()->set_attribute('type', 'select')->set_attribute('data-value', IsNotInCategoryCondition::get_name())->set_options($this->get_woocommerce_categories()), (new SelectField())->set_name(IsInTagCondition::get_name())->add_class('dropshipping-select2-multi width-100')->set_multiple()->set_attribute('type', 'select')->set_attribute('data-value', IsInTagCondition::get_name())->set_options(self::get_woocommerce_tags()), (new SelectField())->set_name(IsNotInTagCondition::get_name())->add_class('dropshipping-select2-multi width-100')->set_multiple()->set_attribute('type', 'select')->set_attribute('data-value', IsNotInTagCondition::get_name())->set_options(self::get_woocommerce_tags())];
        }
        return $this->items;
    }
    public function get_template_name(): string
    {
        return 'conditional-logic-component';
    }
    public static function get_woocommerce_tags(): array
    {
        $result = [];
        $tags = get_tags(['taxonomy' => 'product_tag', 'orderby' => 'name', 'hide_empty' => \true]);
        if (is_array($tags)) {
            /**
             * @var \WP_Term $tag
             */
            foreach ($tags as $tag) {
                $result[$tag->term_id] = $tag->name;
            }
        }
        return $result;
    }
    public static function get_woocommerce_categories(): array
    {
        $result = [];
        $args = ['taxonomy' => 'product_cat', 'orderby' => 'name', 'show_count' => \false, 'pad_counts' => \false, 'hierarchical' => \true, 'title_li' => '', 'hide_empty' => \false];
        $all_categories = get_categories($args);
        if (is_array($all_categories)) {
            $tree = self::build_tree($all_categories);
            ksort($tree);
            $result = self::create_dropdown_array($tree);
        }
        return $result;
    }
    private static function create_dropdown_array(array $branch, int $depth = 0): array
    {
        $result = [];
        foreach ($branch as $leaf) {
            $result[$leaf['id']] = str_repeat('-', $depth) . $leaf['name'];
            if (isset($leaf['children'])) {
                $result = $result + self::create_dropdown_array($leaf['children'], $depth + 1);
            }
        }
        return $result;
    }
    private static function build_tree(array &$all_categories, int $parent_id = 0): array
    {
        $branch = [];
        foreach ($all_categories as $key => $category) {
            if ($category->parent === $parent_id) {
                $children = self::build_tree($all_categories, $category->term_id);
                $branch[$category->term_id]['id'] = $category->term_id;
                $branch[$category->term_id]['name'] = $category->name;
                if (!empty($children)) {
                    $branch[$category->term_id]['children'] = $children;
                }
                unset($all_categories[$key]);
            }
        }
        return $branch;
    }
    public static function get_supported_value_types(): array
    {
        return [EmptyCondition::class, NotEmptyCondition::class, HigherCondition::class, LowerCondition::class, EqualCondition::class, NotEqualCondition::class, ContainsCondition::class, NotContainsCondition::class, IsINCategoryCondition::class, IsNotInCategoryCondition::class, IsInTagCondition::class, IsNotInTagCondition::class];
    }
    public static function get_value_types(): array
    {
        $fields = self::get_supported_value_types();
        $result = [];
        foreach ($fields as $classname) {
            $result[$classname::get_name()] = $classname::get_nice_name();
        }
        return $result;
    }
    public static function get_value_type_conditions()
    {
        $fields = self::get_product_fields();
        $result = [];
        foreach ($fields as $classname) {
            if (is_subclass_of($classname, AllowedConditions::class)) {
                $conditions = $classname::get_allowed_conditions();
                $result[$classname::get_name()] = array_map(function ($classname) {
                    return $classname::get_name();
                }, $conditions);
            }
        }
        return $result;
    }
    public static function get_export_options(): array
    {
        $fields = self::get_product_fields();
        $result = [];
        foreach ($fields as $classname) {
            $result[$classname::get_name()] = $classname::get_nice_name();
        }
        return $result;
    }
    public static function get_product_fields(): array
    {
        $fields = [TitleField::class, CategoryField::class, PriceField::class, SalePriceField::class, DescriptionField::class, SKUField::class, StockField::class, TagsField::class];
        if (EanIntegration::should_display_ean()) {
            $fields[] = EANField::class;
        }
        return $fields;
    }
}
