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


class Sifency_Widget_Wishlist extends Widget_Base {
    use Sifency_Animation_Controls;



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

    public function get_title() {
        return __('Sifency Wishlist', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-heart-o';
    }

    public function get_categories() {
        return ['sifency-addons-woo-widgets'];
    }
    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;
        }
        if ( ! function_exists('YITH_WCWL') ) {
             $this->start_controls_section('wishlist_notice', [ 'label' => 'Notice' ]);
            $this->add_control('wishlist_notice_control', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . __('This widget requires the YITH WooCommerce Wishlist plugin to be installed and active.', 'sifency-addons') . '</div>' ]);
            $this->end_controls_section();
            return;
        }
        
        // --- Content Section ---
        $this->start_controls_section( 'section_content', [ 'label' => 'Wishlist' ] );
        $this->add_control( 'icon', [ 'label' => 'Icon', 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'far fa-heart', 'library' => 'fa-regular' ] ] );
        $this->add_control( 'text', [ 'label' => 'Text', 'type' => Controls_Manager::TEXT, 'default' => 'Wishlist' ] );
        $this->add_control( 'show_count', [ 'label' => 'Show Count', 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
        $this->end_controls_section();

        // --- Style Section: General ---
        $this->start_controls_section( 'section_style', [ 'label' => 'Style', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_responsive_control( 'align', [ 'label' => 'Alignment', 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => ['title' => 'Left', 'icon' => 'eicon-h-align-left'], 'center' => ['title' => 'Center', 'icon' => 'eicon-h-align-center'], 'right' => ['title' => 'Right', 'icon' => 'eicon-h-align-right'] ], 'selectors' => [ '{{WRAPPER}}' => 'text-align: {{VALUE}};' ] ]);
        $this->end_controls_section();
        		$this->start_controls_section(
			'section_style_icon',
			[
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'tab' => Controls_Manager::TAB_STYLE,
				'condition' => [
					'icon[value]!' => '',
				],
			]
		);

		$icon_container_selector = '{{WRAPPER}} .sifency-wishlist-icon';
		$icon_element_selector = '{{WRAPPER}} .sifency-wishlist-icon i, {{WRAPPER}} .sifency-wishlist-icon svg';
		$hover_trigger_selector = '{{WRAPPER}} .sifency-wishlist-link:hover';

		$this->add_responsive_control(
			'icon_size',
			[
				'label' => esc_html__( 'Size', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 6, 'max' => 200 ] ],
				'selectors' => [
					$icon_container_selector => 'font-size: {{SIZE}}{{UNIT}};',
					$icon_element_selector => 'width: {{SIZE}}{{UNIT}}; height: auto;',
				],
			]
		);

		$this->add_responsive_control(
			'icon_padding',
			[
				'label' => esc_html__( 'Padding', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					$icon_container_selector => 'display: inline-flex; align-items: center; justify-content: center; padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'icon_border_radius',
			[
				'label' => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [ $icon_container_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);

		$this->start_controls_tabs( 'icon_style_tabs' );
			// --- NORMAL TAB ---
			$this->start_controls_tab(
				'icon_normal_tab',
				[ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ]
			);
			$this->add_control(
				'icon_color',
				[
					'label' => esc_html__( 'Icon Color', 'sifency-addons' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [ $icon_element_selector => 'color: {{VALUE}}; fill: {{VALUE}};' ],
				]
			);
			$this->add_control(
				'icon_bg_color',
				[
					'label' => esc_html__( 'Background Color', 'sifency-addons' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [ $icon_container_selector => 'background-color: {{VALUE}};' ],
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[ 'name' => 'icon_border', 'selector' => $icon_container_selector ]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[ 'name' => 'icon_box_shadow', 'selector' => $icon_container_selector ]
			);
			$this->end_controls_tab();

			// --- HOVER TAB ---
			$this->start_controls_tab(
				'icon_hover_tab',
				[ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ]
			);
			$this->add_control(
				'icon_hover_color',
				[
					'label' => esc_html__( 'Icon Color', 'sifency-addons' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [ $hover_trigger_selector . ' .sifency-wishlist-icon i, ' . $hover_trigger_selector . ' .sifency-wishlist-icon svg' => 'color: {{VALUE}}; fill: {{VALUE}};' ],
				]
			);
			$this->add_control(
				'icon_hover_bg_color',
				[
					'label' => esc_html__( 'Background Color', 'sifency-addons' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [ $hover_trigger_selector . ' .sifency-wishlist-icon' => 'background-color: {{VALUE}};' ],
				]
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				[ 'name' => 'icon_hover_border', 'selector' => $hover_trigger_selector . ' .sifency-wishlist-icon' ]
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				[ 'name' => 'icon_hover_box_shadow', 'selector' => $hover_trigger_selector . ' .sifency-wishlist-icon' ]
			);
			$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Content Style',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_number_style_controls();
        $this->update_control(
            'sifency_number_style_section',
            [
                'label' => __( 'Count Badge', 'sifency-addons' ),
            ]
        );
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        if ( ! function_exists('YITH_WCWL') ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-placeholder">' . esc_html__('YITH WooCommerce Wishlist is not active.', 'sifency-addons') . '</div>';
            }
            return;
        }
        
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_attrs = $animation_manager->get_wrapper_attributes(['sifency-wishlist-wrapper']);

        $this->add_render_attribute( 'link', 'class', 'sifency-wishlist-link' );
        $this->add_render_attribute( 'link', 'href', YITH_WCWL()->get_wishlist_url() );
        ?>
        <div <?php
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <a <?php echo wp_kses_post($this->get_render_attribute_string('link')); ?><?php  ?>>
                <?php if ( ! empty($settings['icon']['value']) ) : ?>
                    <span class="sifency-wishlist-icon">
                        <?php Icons_Manager::render_icon($settings['icon'], ['aria-hidden' => 'true']); ?>
                    </span>
                <?php endif; ?>

                <?php if ( ! empty($settings['text']) ) : ?>
                    <span class="sifency-wishlist-text sifency-content-style">
                        <?php echo esc_html($settings['text']); ?>
                    </span>
                <?php endif; ?>

                <?php if ('yes' === $settings['show_count']) : ?>
                    <span class="sifency-wishlist-count sifency-number-style">
                        <?php
                        if ( class_exists('YITH_WCWL_Wishlists') && method_exists('YITH_WCWL_Wishlists', 'get_instance') ) {
                            echo (int) \YITH_WCWL_Wishlists::get_instance()->count_items_in_wishlist();
                        } else {
                            echo (int) YITH_WCWL()->count_products();
                        }
                        ?>
                    </span>
                <?php endif; ?>
            </a>
        </div>
        <?php
    }

    /**
	 * Render the widget output in the editor for live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
    protected function _content_template() {
        ?>
        <#
            // Don't render anything in the editor if the main plugin check fails on initial load.
            if ( ! <?php echo function_exists('YITH_WCWL') ? 'true' : 'false'; ?> ) {
                return;
            }

            view.addRenderAttribute( 'link', 'class', 'sifency-wishlist-link' );
            view.addRenderAttribute( 'link', 'href', '#' ); // Use a placeholder link for the editor.

            var iconHTML = '';
            if ( settings.icon && settings.icon.value ) {
                iconHTML = elementor.helpers.renderIcon( view, settings.icon, { 'aria-hidden': 'true' }, 'i' , 'object' ).value;
            }

            // Use a placeholder number for the count.
            var count = '3'; 
        #>
        <div class="sifency-wishlist-wrapper">
            <a {{{ view.getRenderAttributeString('link') }}}>
                <# if ( iconHTML ) { #>
                    <span class="sifency-wishlist-icon">
                        {{{ iconHTML }}}
                    </span>
                <# } #>

                <# if ( settings.text ) { #>
                    <span class="sifency-wishlist-text">
                        {{{ settings.text }}}
                    </span>
                <# } #>

                <# if ( 'yes' === settings.show_count ) { #>
                    <span class="sifency-wishlist-count">
                        {{{ count }}}
                    </span>
                <# } #>
            </a>
        </div>
        <?php
    }
}