<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
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;

class Sifency_Widget_PostContent extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-post-content',
			'title'         => 'Sifency Post Content',
			'icon'          => 'eicon-post-content',
			'category'      => array( 'sifency-post-components' ),
			'keywords'      => array( 'content', 'post', 'article', 'text', 'body' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-post-single-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'info_notice',
			array(
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => esc_html__( 'This widget dynamically displays the main content of the current post. Styling options are available in the Style tab.', 'sifency-addons' ),
				'content_classes' => 'elementor-descriptor',
			)
		);
		$this->end_controls_section();

		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			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-post-content-wrapper' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// -- 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}} p' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'paragraph_typography',
				'selector' => '{{WRAPPER}} 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}} h1, {{WRAPPER}} h2, {{WRAPPER}} h3, {{WRAPPER}} h4, {{WRAPPER}} h5, {{WRAPPER}} h6' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} h1, {{WRAPPER}} h2, {{WRAPPER}} h3, {{WRAPPER}} h4, {{WRAPPER}} h5, {{WRAPPER}} 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}} 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}} 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() {
		$settings             = $this->get_settings_for_display();
		$animation_manager    = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs_string = $animation_manager->get_wrapper_attributes( array( 'sifency-post-content-wrapper' ) );

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

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->render_editor_placeholder();
		} else {
			the_content();// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
		}

		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__( 'This is a Demo Heading', 'sifency-addons' ); ?></h2>
		<p>
			<?php echo esc_html__( 'This is a placeholder for your post content. 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 post 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>
		<p>
			Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
		</p>
		<blockquote><?php echo esc_html__( 'This is a blockquote. It is a great way to highlight a section of your content.', 'sifency-addons' ); ?></blockquote>
		<ul>
			<li><?php echo esc_html__( 'Unordered List Item 1', 'sifency-addons' ); ?></li>
			<li><?php echo esc_html__( 'Unordered List Item 2', 'sifency-addons' ); ?></li>
		</ul>
		<h3><?php echo esc_html__( 'Another Heading (H3)', 'sifency-addons' ); ?></h3>
		<ol>
			<li><?php echo esc_html__( 'Ordered List Item 1', 'sifency-addons' ); ?></li>
			<li><?php echo esc_html__( 'Ordered List Item 2', 'sifency-addons' ); ?></li>
		</ol>
		<?php
	}

	/**
	 * Render the widget output in the editor for live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		?>
		<div class="sifency-post-content-wrapper">
			<h2>This is a Demo Heading</h2>
			<p>
				This is a placeholder for your post content. 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 post content on the live site. Here is a sample
				<a href="#">link to demonstrate styling.</a>
			</p>
			<p>
				Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
			</p>
			<blockquote>This is a blockquote. It is a great way to highlight a section of your content.</blockquote>
			<ul>
				<li>Unordered List Item 1</li>
				<li>Unordered List Item 2</li>
			</ul>
			<h3>Another Heading (H3)</h3>
			<ol>
				<li>Ordered List Item 1</li>
				<li>Ordered List Item 2</li>
			</ol>
		</div>
		<?php
	}
}
