<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Plugin as ElementorPlugin;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Product_Breadcrumb extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'product-breadcrumb';
    }

    public function get_title() {
        return __('Product Breadcrumb', 'sifency-addons');
    }

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

    public function get_categories() {
        return ['sifency-addons-woo-widgets'];
    }

    public function get_keywords() {
        return ['product', 'breadcrumb', 'navigation', 'trail', 'woocommerce'];
    }

    public function get_style_depends() {
	    return [ 'sifency-frontend-style' ];
    }

    protected function register_controls() {
        if ( ! class_exists('woocommerce') ) {
            $this->start_controls_section('woo_notice', [ 'label' => 'Notice' ]);
            $this->add_control('woo_notice_control', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . esc_html__('WooCommerce is not installed or active.', 'sifency-addons') . '</div>' ]);
            $this->end_controls_section();
            return;
        }

        // --- Style Section ---
        $this->start_controls_section(
            'section_style_breadcrumb',
            [ 'label' => __('Breadcrumb', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_responsive_control(
            'alignment',
            [
                'label' => __('Alignment', 'sifency-addons'), 'type' => Controls_Manager::CHOOSE,
                'options' => [ 'left' => [ 'title' => 'Left', 'icon' => 'eicon-text-align-left' ], 'center' => [ 'title' => 'Center', 'icon' => 'eicon-text-align-center' ], 'right' => [ 'title' => 'Right', 'icon' => 'eicon-text-align-right' ] ],
                'selectors' => [ '{{WRAPPER}} .sifency-breadcrumb-wrapper .woocommerce-breadcrumb' => 'text-align: {{VALUE}};' ],
            ]
        );
        $this->add_control(
            'breadcrumb_gap',
            [
                'label' => __( 'Gap Between Items', 'sifency-addons' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', '%' ],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                    'em' => [
                        'min' => 0,
                        'max' => 5,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .woocommerce-breadcrumb > *:not(:last-child)' => 'margin-right: {{SIZE}}{{UNIT}};',
                ],
                'selectors_hover' => [
                    '{{WRAPPER}} .woocommerce-breadcrumb > *:not(:last-child):hover' => 'margin-right: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [ 'name' => 'typography', 'selector' => '{{WRAPPER}} .sifency-breadcrumb-wrapper .woocommerce-breadcrumb' ]
        );
        $this->add_control(
            'text_color',
            [
                'label' => __('Text Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR,
                'description' => __('Color for the current page text and separators.', 'sifency-addons'),
                'selectors' => [ '{{WRAPPER}} .woocommerce-breadcrumb' => 'color: {{VALUE}};' ],
            ]
        );
        $this->add_control(
            'links_heading',
            [ 'label' => 'Links', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ]
        );
        $this->start_controls_tabs('link_style_tabs');
        $this->start_controls_tab( 'link_normal_tab', ['label' => 'Normal'] );
        $this->add_control( 'link_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .woocommerce-breadcrumb a' => 'color: {{VALUE}};' ] ]);
        $this->end_controls_tab();
        $this->start_controls_tab( 'link_hover_tab', ['label' => 'Hover'] );
        $this->add_control( 'link_hover_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .woocommerce-breadcrumb a:hover' => 'color: {{VALUE}};' ] ]);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->end_controls_section();
        
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-breadcrumb-wrapper'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);

        ?>
        <div <?php 
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                if ( method_exists( $this, 'render_editor_placeholder' ) ) {
                    $this->render_editor_placeholder();
                } else {
                    echo '<div class="sifency-placeholder">' . esc_html__('WooCommerce breadcrumbs will be displayed here.', 'sifency-addons') . '</div>';
                }
            } else {
                if ( function_exists('woocommerce_breadcrumb') ) {
                    woocommerce_breadcrumb();
                }
            }
            ?>
        </div>
        <?php
    }

    private function render_editor_placeholder() {
        $delimiter = get_option('woocommerce_breadcrumb_delimiter', ' / ');
        ?>
        <nav class="woocommerce-breadcrumb" aria-label="<?php esc_attr_e('Breadcrumb', 'sifency-addons'); ?>">
            <a href="#"><?php esc_html_e('Home', 'sifency-addons'); ?></a>
            <?php echo wp_kses_post($delimiter); ?><?php  ?>
            <a href="#"><?php esc_html_e('Category', 'sifency-addons'); ?></a>
            <?php echo wp_kses_post($delimiter); ?><?php  ?>
            <?php esc_html_e('Product Name', 'sifency-addons'); ?>
        </nav>
        <?php
    }

    public function __before_render() {
        if ( ! is_woocommerce() && ! ElementorPlugin::$instance->editor->is_edit_mode() ) {
            return false;
        }
    }
}