<?php

namespace Elementor;


include get_template_directory() . '/elementor/' . 'contracts.php';


if (!defined('ABSPATH')) exit;


class FeaturedAttributes extends Widget_Base
{

    public function get_name()
    {
        return 'banta_featured_attributes';
    }

    public function get_title()
    {
        return __('Featured Attributes', TEXT_DOMAIN);
    }

    public function get_icon()
    {
        return 'eicon-product-info';
    }

    public function get_categories()
    {
        return ['jiawaz_single_product'];
    }

    public function get_keywords()
    {
        return ['attribute', 'product info', 'info', 'list'];
    }


    protected function _register_controls()
    {
        // content tab
        $this->register_content_tab_products_controls();

        // style tab
        $this->register_style_tab_card_controls();
    }


    // content
    protected function register_content_tab_products_controls(){
        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__( 'Content', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'show_title',
            [
                'label'   => esc_html__( 'Show title', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'yes',
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'title',
            [
                'label' => esc_html__('Title', TEXT_DOMAIN),
                'default' => esc_html__('Featured Attributes', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::TEXT,
                'placeholder' => esc_html__('enter section title', TEXT_DOMAIN),
            ]
        );


        $this->end_controls_section();
    }

    // style box
    protected function register_style_tab_card_controls(){

        // title
        $this->start_controls_section(
            'section_title_style',
            [
                'label' => esc_html__( 'Title', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'title_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .featured-attributes-title',
            ]
        );
        $this->add_responsive_control(
            'title_margin',
            [
                'label'      => __( 'margin', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', 'rem' ],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .featured-attributes-title' => 'margin-bottom: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_control(
            'title_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .featured-attributes-title' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->end_controls_section();

        // list
        $this->start_controls_section(
            'section_list_style',
            [
                'label' => esc_html__( 'Attributes', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'attributes_horizontal_margin',
            [
                'label'      => __( 'Horizontal space', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', 'rem' ],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .featured-attributes dl' => 'grid-column-gap: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_responsive_control(
            'attributes_vertical_margin',
            [
                'label'      => __( 'Vertical space', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', 'rem' ],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .featured-attributes dl' => 'grid-row-gap:{{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'attributes_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .featured-attributes dt',
            ]
        );
        $this->add_control(
            'attributes_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .featured-attributes dt' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'attributes_value_typography',
                'label'    => esc_html__( 'value typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .featured-attributes dd',
            ]
        );
        $this->add_control(
            'attributes_value_color',
            [
                'label'     => esc_html__( 'value color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .featured-attributes dd' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function display_featured_attributes() {
        global $product;

        if (!$product) {
            return;
        }

        $featured_attributes = get_post_meta($product->get_id(), '_featured_attributes', true);

        if (!is_array($featured_attributes) || empty($featured_attributes)) {
            return;
        }

        $attributes = $product->get_attributes();

        if (empty($attributes)) {
            return;
        }

        // Only display featured attributes
        $has_featured = false;
        foreach ($featured_attributes as $attribute_name) {
            if (isset($attributes[$attribute_name])) {
                $has_featured = true;
                break;
            }
        }

        if (!$has_featured) {
            return;
        }

        echo '<dl>';
        foreach ($featured_attributes as $attribute_name) {
            if (isset($attributes[$attribute_name])) {
                $attribute = $attributes[$attribute_name];
                $attribute_label = urldecode(wc_attribute_label($attribute_name));

                echo '<dt>' . esc_html($attribute_label) . '</dt>';

                // Get attribute values
                if ($attribute->is_taxonomy()) {
                    $attribute_taxonomy = $attribute->get_taxonomy_object();
                    $attribute_values = wc_get_product_terms($product->get_id(), $attribute->get_name(), array('fields' => 'names'));
                    echo '<dd>' . esc_html(implode(', ', $attribute_values)) . '</dd>';
                } else {
                    $values = $attribute->get_options();
                    $values = !empty($values) ? $values : array();
                    echo '<dd>' . esc_html(implode(', ', $values)) . '</dd>';
                }
            }
        }
        echo '</dl>';
    }


    protected function render()
    {

        // Don't render if we're not on a single product page (frontend check)
        if (!function_exists('is_product') || !is_product()) {
            if (current_user_can('edit_posts')) {
                echo '<div style="padding: 20px; background: #f0f0f0; border: 1px dashed #ccc; text-align: center;">';
                echo __('This widget is only visible on single product pages.', TEXT_DOMAIN);
                echo '</div>';
            }
            return;
        }

        $settings = $this->get_settings_for_display();
        $sectionTitle = $settings['title'];
        $showTitle = $settings['show_title'] == 'yes';

        echo '<div class="featured-attributes">';
        if($showTitle):
            echo "<div class='featured-attributes-title'>" . $sectionTitle . "</div>";
        endif;
        $this->display_featured_attributes();
        echo '</div>';

    }




}

Plugin::instance()->widgets_manager->register_widget_type(new FeaturedAttributes());