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

class Sifency_Widget_Product_Description extends Widget_Base {
    use Sifency_Animation_Controls;

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

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

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

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

    public function get_keywords() {
        return ['product', 'description', 'content', 'woocommerce', 'woo'];
    }

    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_info',
            [ 'label' => esc_html__('Information', 'sifency-addons') ]
        );
        $this->add_control(
			'info_notice',
			[
				'type' => Controls_Manager::RAW_HTML,
				'raw' => esc_html__( 'This widget dynamically displays the main description of the current product. It can only be used within a WooCommerce Product Template.', 'sifency-addons' ),
				'content_classes' => 'elementor-descriptor',
			]
		);
        $this->end_controls_section();

        // --- Style Section: General ---
        $this->start_controls_section(
            'section_style_general',
            [ 'label' => esc_html__('General', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_responsive_control(
			'align',
			[
				'label' => esc_html__('Alignment', 'sifency-addons'),
                'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [ 'title' => esc_html__('Left', 'sifency-addons'), 'icon' => 'eicon-text-align-left' ],
					'center' => [ 'title' => esc_html__('Center', 'sifency-addons'), 'icon' => 'eicon-text-align-center' ],
					'right' => [ 'title' => esc_html__('Right', 'sifency-addons'), 'icon' => 'eicon-text-align-right' ],
					'justify' => [ 'title' => esc_html__('Justified', 'sifency-addons'), 'icon' => 'eicon-text-align-justify' ],
				],
				'selectors' => [ '{{WRAPPER}} .sifency-product-description' => 'text-align: {{VALUE}};' ],
			]
		);
        $this->end_controls_section();

        // -- Paragraph Style --
        $this->start_controls_section(
            'section_paragraph_style',
            [ 'label' => esc_html__('Paragraphs', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_control( 'paragraph_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-product-description p' => 'color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'paragraph_typography', 'selector' => '{{WRAPPER}} .sifency-product-description p' ] );
        $this->end_controls_section();

        // -- Heading Style --
        $this->start_controls_section(
            'section_heading_style',
            [ 'label' => esc_html__('Headings (H1-H6)', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->add_control( 'heading_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-product-description h1, {{WRAPPER}} .sifency-product-description h2, {{WRAPPER}} .sifency-product-description h3, {{WRAPPER}} .sifency-product-description h4, {{WRAPPER}} .sifency-product-description h5, {{WRAPPER}} .sifency-product-description h6' => 'color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'heading_typography', 'selector' => '{{WRAPPER}} .sifency-product-description h1, {{WRAPPER}} .sifency-product-description h2, {{WRAPPER}} .sifency-product-description h3, {{WRAPPER}} .sifency-product-description h4, {{WRAPPER}} .sifency-product-description h5, {{WRAPPER}} .sifency-product-description h6' ] );
        $this->end_controls_section();

        // -- Link Style --
        $this->start_controls_section(
            'section_link_style',
            [ 'label' => esc_html__('Links', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
        $this->start_controls_tabs('link_style_tabs');
        $this->start_controls_tab('link_normal', ['label' => esc_html__('Normal', 'sifency-addons')]);
        $this->add_control('link_color', ['label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-product-description a' => 'color: {{VALUE}};']]);
        $this->end_controls_tab();
        $this->start_controls_tab('link_hover', ['label' => esc_html__('Hover', 'sifency-addons')]);
        $this->add_control('link_hover_color', ['label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-product-description 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' ]
        );
    }

    /**
     * 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-description']);
        
        echo '<div ' . wp_kses_post ($wrapper_attrs_string) . '>';

        if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
            $this->render_editor_placeholder();
        } else {
            the_content();
        }

        echo '</div>';
    }

    /**
	 * Render the placeholder content for the editor.
	 * This provides a rich structure for styling purposes.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
    protected function render_editor_placeholder() {
        ?>
        <h2><?php echo esc_html__('Sample Product Heading', 'sifency-addons'); ?></h2>
        <p>
            <?php echo esc_html__('This is a placeholder for your product description. You can style the appearance of paragraphs, headings, lists, and links using the controls in the Style tab. This text will be replaced with the actual product content on the live site. Here is a sample', 'sifency-addons'); ?>
            <a href="#"><?php echo esc_html__('link to demonstrate styling.', 'sifency-addons'); ?></a>
        </p>
        <blockquote><?php echo esc_html__('This is a blockquote. It is a great way to highlight a key feature or testimonial within your description.', 'sifency-addons'); ?></blockquote>
        <ul>
            <li><?php echo esc_html__('Feature list item one', 'sifency-addons'); ?></li>
            <li><?php echo esc_html__('Feature list item two', 'sifency-addons'); ?></li>
        </ul>
        <?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-description">
            <h2>Sample Product Heading</h2>
            <p>
                This is a placeholder for your product description. You can style the appearance of paragraphs, headings, lists, and links using the controls in the Style tab. This text will be replaced with the actual product content on the live site. Here is a sample
                <a href="#">link to demonstrate styling.</a>
            </p>
            <blockquote>This is a blockquote. It is a great way to highlight a key feature or testimonial within your description.</blockquote>
            <ul>
                <li>Feature list item one</li>
                <li>Feature list item two</li>
            </ul>
        </div>
		<?php
	}
}