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

class Sifency_Widget_Product_Additional_Info extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'sifency-product-additional-info';
    }

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

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

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

    public function get_keywords() {
        return ['product', 'additional', 'information', 'attributes', 'woocommerce', 'table'];
    }

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

    protected function register_controls() {
        // --- Dependency Check ---
        if ( ! class_exists('woocommerce') ) {
            $this->start_controls_section('woo_notice', [ 'label' => esc_html__('Notice', 'sifency-addons') ]);
            $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;
        }

        // --- Content Section ---
        $this->start_controls_section(
            'section_content_heading',
            [ 'label' => esc_html__('Heading', 'sifency-addons') ]
        );
        $this->add_control(
			'show_heading',
			[
				'label' => esc_html__( 'Show Heading', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__( 'Show', 'sifency-addons' ),
				'label_off' => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default' => 'yes',
			]
		);
		$this->add_control(
			'heading_tag',
			[
				'label' => esc_html__( 'HTML Tag', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'options' => [ 'h1' => 'H1', 'h2' => 'H2', 'h3' => 'H3', 'h4' => 'H4', 'h5' => 'H5', 'h6' => 'H6', 'div' => 'div', 'p' => 'p' ],
				'default' => 'h2',
				'condition' => [ 'show_heading' => 'yes' ],
			]
		);
        $this->end_controls_section();

        // --- Style Section: Heading ---
        $this->start_controls_section(
            'section_style_heading',
            [
                'label' => esc_html__('Heading', 'sifency-addons'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [ 'show_heading' => 'yes' ],
            ]
        );
        $this->add_control(
            'heading_color',
            [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-product-additional-info .sifency-heading' => 'color: {{VALUE}};' ] ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [ 'name' => 'heading_typography', 'selector' => '{{WRAPPER}} .sifency-product-additional-info .sifency-heading' ]
        );
        $this->end_controls_section();

        // --- Style Section: Table ---
        $this->start_controls_section(
            'section_style_table',
            [ 'label' => esc_html__('Table', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [ 'name' => 'table_border', 'label' => esc_html__( 'Table Border', 'sifency-addons' ), 'selector' => '{{WRAPPER}} table.shop_attributes, {{WRAPPER}} table.shop_attributes th, {{WRAPPER}} table.shop_attributes td' ]
        );
        $this->add_responsive_control(
			'cell_padding',
			[ 'label' => esc_html__( 'Cell Padding', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%', 'em' ], 'selectors' => [ '{{WRAPPER}} table.shop_attributes th, {{WRAPPER}} table.shop_attributes td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ]
		);

        $this->add_control('heading_labels', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__('Labels', 'sifency-addons'), 'separator' => 'before' ]);
        $this->add_control('label_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} table.shop_attributes th' => 'color: {{VALUE}};' ] ]);
        $this->add_control('label_bg_color', [ 'label' => esc_html__('Background Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} table.shop_attributes th' => 'background-color: {{VALUE}};' ] ]);
        $this->add_group_control(Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'selector' => '{{WRAPPER}} table.shop_attributes th' ]);

        $this->add_control('heading_values', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__('Values', 'sifency-addons'), 'separator' => 'before' ]);
        $this->add_control('value_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} table.shop_attributes td' => 'color: {{VALUE}};' ] ]);
        $this->add_control('value_bg_color', [ 'label' => esc_html__('Background Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} table.shop_attributes td' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control('value_bg_alt_color', [ 'label' => esc_html__('Striped Row Background', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} table.shop_attributes tr.alt td' => 'background-color: {{VALUE}};' ] ]);
        $this->add_group_control(Group_Control_Typography::get_type(), [ 'name' => 'value_typography', 'selector' => '{{WRAPPER}} table.shop_attributes td p, {{WRAPPER}} table.shop_attributes td a' ]);

        $this->end_controls_section();

        $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 ( ! class_exists('woocommerce') ) { return; }

        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_attrs_string = $animation_manager->get_wrapper_attributes(['sifency-product-additional-info']);
        
        echo '<div ' . wp_kses_post ($wrapper_attrs_string ) . '>';

        if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
            $this->render_editor_placeholder();
        } else {
            // Accessing the global $product object from WooCommerce.
            global $product; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
            if ( empty( $product ) ) { return; }
            
            wc_display_product_attributes( $product );
        }

        echo '</div>';
    }

    /**
	 * Render the placeholder content for the editor.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
    protected function render_editor_placeholder() {
        $settings = $this->get_settings_for_display();

        if ( 'yes' === $settings['show_heading'] ) {
            $heading_tag = $settings['heading_tag'];
            $heading_text = apply_filters( 'woocommerce_product_additional_information_heading', esc_html__( 'Additional information', 'sifency-addons' ) );
            printf( '<%1$s class="sifency-heading">%2$s</%1$s>', tag_escape($heading_tag), esc_html($heading_text) );
        }
        ?>
        <table class="woocommerce-product-attributes shop_attributes">
            <tbody>
                <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--color">
                    <th class="woocommerce-product-attributes-item__label"><?php echo esc_html__('Color', 'sifency-addons'); ?></th>
                    <td class="woocommerce-product-attributes-item__value"><p><a href="#" rel="tag">Blue</a>, <a href="#" rel="tag">Green</a></p></td>
                </tr>
                <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--size alt">
                    <th class="woocommerce-product-attributes-item__label"><?php echo esc_html__('Size', 'sifency-addons'); ?></th>
                    <td class="woocommerce-product-attributes-item__value"><p>Large, Medium, Small</p></td>
                </tr>
            </tbody>
        </table>
        <?php
    }

    /**
	 * Render the widget output in the editor for live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		?>
        <div class="sifency-product-additional-info">
            <# if ( 'yes' === settings.show_heading ) {
                var heading_text = '<?php echo esc_js( apply_filters( 'woocommerce_product_additional_information_heading', __( 'Additional information', 'sifency-addons' ) ) ); ?>';
            #>
                <{{ settings.heading_tag }} class="sifency-heading">{{{ heading_text }}}</{{ settings.heading_tag }}>
            <# } #>
            <table class="woocommerce-product-attributes shop_attributes">
                <tbody>
                    <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--color">
                        <th class="woocommerce-product-attributes-item__label">Color</th>
                        <td class="woocommerce-product-attributes-item__value"><p><a href="#" rel="tag">Blue</a>, <a href="#" rel="tag">Green</a></p></td>
                    </tr>
                    <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--size alt">
                        <th class="woocommerce-product-attributes-item__label">Size</th>
                        <td class="woocommerce-product-attributes-item__value"><p>Large, Medium, Small</p></td>
                    </tr>
                </tbody>
            </table>
        </div>
		<?php
	}
}