<?php

namespace Dits\CaW\Admin;

use Dits\CaW\Utils\PluginFeatures;

class Fields
{
    public function __construct()
    {
        // TODO
    }
    public function sanitize_checkbox($value): string
    {
        return $value === 'on' ? 'on' : 'off';
    }

    public function sanitize_multicheck($value): array
    {
        if (!is_array($value)) {
            return [];
        }
        return array_map('sanitize_text_field', $value);
    }
    public function get_defaults_field($str): array
    {
        return [
            [
                // translators: %s is the feature name (e.g., Compare).
                'id' => 'enable',
                'label' => __('Enable compare', 'dits-compare'),
                'desc' => __('Turn on compare functionality', 'dits-compare'),
                'type' => 'checkbox',
                'default' => '',
            ],
            [
                'id' => 'dits_compare_button_view',
                'label' => __("Display the compare button as 'button' or 'link'", 'dits-compare'),
                'desc' => __('Choose as the button should view', 'dits-compare'),
                'type' => 'radio',
                'options' => array(
                    'display_btn' => __('Display as button', 'dits-compare'),
                    'display_link' => __('Display as link', 'dits-compare'),
                ),
                'default' => 'display_btn',
            ],
            [
                // translators: %s is the feature name (e.g., Compare).
                'id' => 'single_button_hook',
                'label' => __('Single product button hook', 'dits-compare'),
                'desc' => __('Hook for display compare button.', 'dits-compare'),
                'type' => 'select',
                'options' => [
                    'woocommerce_before_add_to_cart_form' => 'woocommerce_before_add_to_cart_form',
                    'woocommerce_before_add_to_cart_button' => 'woocommerce_before_add_to_cart_button',
                    'woocommerce_after_add_to_cart_button' => 'woocommerce_after_add_to_cart_button',
                    'woocommerce_after_add_to_cart_form' => 'woocommerce_after_add_to_cart_form',
                    'manual' => __('Manual', 'dits-compare') . ' (use shortcode [dcaw_compare_button])'
                ],
                'default' => 'manual'
            ],
            [
                'id' => 'loop_button_hook',
                'label' => __('In loop button hook', 'dits-compare'),
                // translators: %s is the feature name (e.g., Compare).
                'desc' => __('Hook for display compare button.', 'dits-compare'),
                'type' => 'select',
                'options' => [
                    'before' => 'Before add to cart button',
                    'after' => 'After add to cart button',
                    'manual' => __('Manual', 'dits-compare') . ' (use shortcode [dcaw_compare_button])'
                ],
                'default' => 'manual'
            ],
            [
                'id' => 'button_class',
                'label' => __('Button / link class', 'dits-compare'),
                // translators: %s is the feature name (e.g., Compare).
                'desc' => __('Add to compare button css classes (separate comma)', 'dits-compare'),
                'type' => 'text',
            ],
            [
                'id' => 'button_text',
                'label' => __('Button / link text', 'dits-compare'),
                // translators: %s is the feature name (e.g., Compare).
                'desc' => __('Compare button text', 'dits-compare'),
                'type' => 'text',
                // translators: %s is the feature name (e.g., Compare).
                'default' => __('Compare', 'dits-compare'),
            ],
            [
                'id' => 'button_icon',
                'label' => __('Button / link svg icon', 'dits-compare'),
                'desc' => __('Svg icon code', 'dits-compare'),
                'type' => 'textarea',
            ],
            [
                'id' => 'count_selector',
                'label' => __('Css selector for compare counters', 'dits-compare'),
                'type' => 'text',
                'desc' => __('Default selector .js-dcaw-compare-count', 'dits-compare'),
                'default' => ''
            ],
            [
                'id' => 'max',
                'label' => __('Max compared', 'dits-compare'),
                'desc' => __('Max product to compare', 'dits-compare'),
                'type' => 'number',
                'default' => 6
            ],
            [
                'id' => 'compare_page',
                'label' => __('Compare page', 'dits-compare'),
                'desc' => __('Choose compare page', 'dits-compare'),
                'type' => 'select',
                'options' => $this->get_page_list()
            ],
            [
                'id' => 'show_popup',
                'label' => __('Show comparison popup', 'dits-compare'),
                'desc' => __('Enable popup when clicking on compare', 'dits-compare'),
                'type' => 'checkbox',
                'default' => 'no',
            ],
        ];
    }

    public function get_compare_fields(): array
    {
        $is_disabled = PluginFeatures::updateFormValue();
        $disabled_value = $is_disabled ? 'disabled' : '';

        return [
            [
                'id' => 'attributes',
                'label' => __('Compare attributes', 'dits-compare'),
                'type' => 'checkbox',
                'default' => 'on',
                'disabled' => $disabled_value,
                'sanitize_callback' => [$this, 'sanitize_checkbox'],
            ],
            [
                'id' => 'taxonomies',
                'label' => __('Compare taxonomies', 'dits-compare'),
                'type' => 'multicheck',
                'options' => $this->get_compare_taxonomies(),
                'sanitize_callback' => [$this, 'sanitize_multicheck'],
            ],
            [
                'id' => 'dimensions',
                'label' => __('Compare dimensions', 'dits-compare'),
                'type' => 'multicheck',
                'options' => [
                    'length' => __('Length', 'dits-compare'),
                    'height' => __('Height', 'dits-compare'),
                    'width' => __('Width', 'dits-compare'),
                    'weight' => __('Weight', 'dits-compare'),
                ],
                'sanitize_callback' => [$this, 'sanitize_multicheck'],
            ],
            [
                'id' => 'hide_short_description',
                'label' => __('Hide Short Description', 'dits-compare'),
                'desc' => __('Hide the short description in the comparison table.', 'dits-compare'),
                'type' => 'checkbox',
                'default' => '',
                'disabled' => $disabled_value,
                'sanitize_callback' => [$this, 'sanitize_checkbox'],
            ],
            [
                'id' => 'trigger_selector',
                'label' => __('CSS selector for opening the compare modal', 'dits-compare'),
                'type' => 'text',
                'desc' => __('Enter the CSS selector of the element that will open the compare modal when clicked. Example: .open-compare', 'dits-compare'),
                'disabled' => $disabled_value,
                'default' => '',

            ],
        ];
    }

    public function get_wishlist_compare(): array
    {
        return [];
    }

    public function get_sections(): array
    {
        return [
            [
                'id' => 'compare',
                'title' => __('Main settings', 'dits-compare')
            ],
            [
                'id' => 'field_settings',
                'title' => __('Fields settings', 'dits-compare')
            ]
        ];
    }

    public function get_main_fields(): array
    {
        return [
            'compare' => array_merge(
                $this->get_defaults_field('compare')
            )
        ];
    }
    public function get_meta_fields(): array
    {
        return [
            'field_settings' => array_merge(
                $this->get_compare_fields()
            )
        ];
    }
    private function get_page_list()
    {
        $args = [
            'sort_order' => 'ASC',
            'sort_column' => 'post_title',
            'hierarchical' => false,
            'parent' => -1,
            'post_type' => 'page',
        ];

        $page_list = ["" => ""];

        $pages = get_pages($args);

        foreach ($pages as $page) {
            // Замість ID використовуємо permalink
            $page_list[get_permalink($page->ID)] = $page->post_title;
        }

        return $page_list;
    }
    private function get_compare_taxonomies(): array
    {
        $taxonomies = get_object_taxonomies('product', 'objects');

        $taxonomies = array_filter($taxonomies, function ($item) {
            return $item->show_ui;
        });

        $taxonomies = array_filter($taxonomies, function ($item) { // for free
            if ($item->name == 'product_cat' || $item->name == 'product_tag')
                return $item;
        });

        return array_map(function ($taxonomy) {
            return $taxonomy->label;
        }, $taxonomies);
    }
}
