<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Shadow;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Postexcerpt extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;


	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-post-excerpt',
			'title'         => 'Sifency Post Excerpt',
			'icon'          => 'eicon-post-excerpt',
			'category'      => array( 'sifency-post-components' ),
			'keywords'      => array( 'heading', 'title', 'text', 'dynamic', 'sifency', 'excerpt', 'content' ),
			'style_depends' => array( 'sifency-typo-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Content', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$source_options = array(
			'the_excerpt'  => esc_html__( 'Post Excerpt', 'sifency-addons' ),
			'custom'       => esc_html__( 'Custom Text', 'sifency-addons' ),
			'post_title'   => esc_html__( 'Post Title', 'sifency-addons' ),
			'the_content'  => esc_html__( 'Post Content (Limited)', 'sifency-addons' ),
			'archive'      => esc_html__( 'Archive Title', 'sifency-addons' ),
			'site_title'   => esc_html__( 'Site Title', 'sifency-addons' ),
			'site_tagline' => esc_html__( 'Site Tagline', 'sifency-addons' ),
		);
		if ( function_exists( 'get_field' ) ) {
			$source_options['acf_field'] = esc_html__( 'ACF Field', 'sifency-addons' );
		}

		$this->add_control(
			'title_source',
			array(
				'label'   => esc_html__( 'Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => $source_options,
				'default' => 'the_excerpt',
			)
		);
		$this->add_control(
			'heading_text',
			array(
				'label'       => esc_html__( 'Text', 'sifency-addons' ),
				'label_block' => true,
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'This is the post excerpt.', 'sifency-addons' ),
				'placeholder' => esc_html__( 'Enter your text', 'sifency-addons' ),
				'condition'   => array( 'title_source' => 'custom' ),
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'acf_field_name',
			array(
				'label'       => esc_html__( 'ACF Field Name', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'condition'   => array( 'title_source' => 'acf_field' ),
				'description' => esc_html__( 'Enter the ACF field key (e.g., my_custom_field).', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'excerpt_length',
			array(
				'label'       => esc_html__( 'Excerpt Length (Words)', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'default'     => 55,
				'condition'   => array(
					'title_source' => 'the_content',
					'title_source' => 'the_excerpt',
				),
				'description' => esc_html__( 'Controls the number of words shown when "Post Content" is the source.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'html_tag',
			array(
				'label'   => esc_html__( 'HTML Tag', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'h1'   => 'H1',
					'h2'   => 'H2',
					'h3'   => 'H3',
					'h4'   => 'H4',
					'h5'   => 'H5',
					'h6'   => 'H6',
					'div'  => 'div',
					'span' => 'span',
					'p'    => 'p',
				),
				'default' => 'p',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Excerpt',
				'selector' => '.sifency-content-widget',
				'exclude'  => array( 'bg', 'tabs', 'radius', 'transition', 'shadow', 'stroke', 'border' ),
			)
		);
	}

	/**
	 * 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 );
		$text              = sifency_addons_get_dynamic_text( $settings );
		if ( empty( $text ) ) {
			return;
		}

		$allowed_tags = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p' );
		$html_tag     = in_array( $settings['html_tag'], $allowed_tags ) ? $settings['html_tag'] : 'p';
		$this->add_render_attribute( 'heading', 'class', 'sifency-content-widget' );
		$animation_attrs_string = $animation_manager->get_wrapper_attributes( array() );
		if ( ! empty( $animation_attrs_string ) ) {
			preg_match( '/class="([^"]+)"/', $animation_attrs_string, $class_matches );
			if ( isset( $class_matches[1] ) ) {
				$this->add_render_attribute( 'heading', 'class', $class_matches[1] );
			}
			preg_match_all( '/(data-[^=]+="[^"]+")/', $animation_attrs_string, $data_matches );
			if ( isset( $data_matches[1] ) ) {
				foreach ( $data_matches[1] as $attr ) {
					list($key, $value) = explode( '=', $attr );
					$this->add_render_attribute( 'heading', $key, trim( $value, '"' ) );
				}
			}
		}

		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'link', $settings['link'] );
			$text = sprintf( '<a %1$s>%2$s</a>', $this->get_render_attribute_string( 'link' ), $text );
		}

		printf(
			'<%1$s %2$s>%3$s</%1$s>',
			esc_html( $html_tag ),
			wp_kses_post( $this->get_render_attribute_string( 'heading' ) ),
			wp_kses_post( $text )
		);
	}
}
