<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Product_Description extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	protected function get_allowed_template_types() {
		return 'product';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-product-description',
			'title'         => 'Sifency Product Description',
			'icon'          => 'eicon-product-description',
			'category'      => 'sifency-addons-woo-widgets',
			'keywords'      => array( 'product', 'description', 'content', 'woocommerce', 'woo' ),
			'style_depends' => array( 'sifency-frontend-style' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->start_controls_section(
			'section_info',
			array( 'label' => esc_html__( 'Information', 'sifency-addons' ) )
		);
		$this->add_control(
			'info_notice',
			array(
				'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();
		$this->add_sifency_addons_help_controls();

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

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

		// -- Heading Style --
		$this->start_controls_section(
			'section_heading_style',
			array(
				'label' => esc_html__( 'Headings (H1-H6)', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'heading_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{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(),
			array(
				'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',
			array(
				'label' => esc_html__( 'Links', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->start_controls_tabs( 'link_style_tabs' );
		$this->start_controls_tab( 'link_normal', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );
		$this->add_control(
			'link_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-description a' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'link_hover', array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) ) );
		$this->add_control(
			'link_hover_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-description a:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! class_exists( 'woocommerce' ) ) {
			return;
		}

		$settings             = $this->get_settings_for_display();
		$animation_manager    = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs_string = $animation_manager->get_wrapper_attributes( array( 'sifency-product-description' ) );

		echo '<div ' . wp_kses_post( $wrapper_attrs_string ) . '>';

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

		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
	}
}
