<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Widget_combine_Posttitle extends Sifency_Addons_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'     => 'post-title',
			'title'    => 'Sifency Post Title',
			'icon'     => 'eicon-post-title',
			'category' => array( 'sifency-post-components' ),
			'keywords' => array( 'heading', 'title', 'text', 'dynamic', 'sifency', 'post-title', 'page-title' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Title Content', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'text_before',
			array(
				'label'       => esc_html__( 'Text Before', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => 'Hello ',
			)
		);
		$this->add_control(
			'title_source',
			array(
				'label'   => esc_html__( 'Dynamic Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'post_title'  => esc_html__( 'Post Title', 'sifency-addons' ),
					'custom'      => esc_html__( 'Custom Text (Static)', 'sifency-addons' ),
					'the_excerpt' => esc_html__( 'Post Excerpt', 'sifency-addons' ),
					'archive'     => esc_html__( 'Archive Title', 'sifency-addons' ),
					'site_title'  => esc_html__( 'Site Title', 'sifency-addons' ),
					'acf_field'   => esc_html__( 'ACF Field', 'sifency-addons' ),
				),
				'default' => 'post_title',
			)
		);
		$this->add_control(
			'heading_text',
			array(
				'label'     => esc_html__( 'Main Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => esc_html__( 'Main Heading', 'sifency-addons' ),
				'condition' => array( 'title_source' => 'custom' ),
				'dynamic'   => array( 'active' => true ),
			)
		);
		$this->add_control(
			'acf_field_name',
			array(
				'label'     => esc_html__( 'ACF Key', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'condition' => array( 'title_source' => 'acf_field' ),
			)
		);
		$this->add_control(
			'text_after',
			array(
				'label'       => esc_html__( 'Text After', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => ' World',
			)
		);
		$this->add_control(
			'link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'dynamic'     => array( 'active' => true ),
				'default'     => array( 'url' => '' ),
				'placeholder' => 'https://your-link.com',
			)
		);

		$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',
					'div'  => 'div',
					'span' => 'span',
					'p'    => 'p',
				),
				'default' => 'h2',
			)
		);
		$this->add_control(
			'text_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => \Elementor\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',
					),
				),
				'toggle'    => true,
				'selectors' => array(
					'{{WRAPPER}} .sifency-advanced-heading-wrapper' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLE SECTION ---
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'dynamic_style',
				'label'           => 'Dynamic Typography',
				'selector'        => '.sifency-dynamic-part',
				'show_blend_mode' => true,
				'exclude'         => array( 'bg', 'radius', 'border', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'heading_style',
				'label'           => 'Main Typography',
				'selector'        => '.sifency-static-part',
				'show_blend_mode' => true,
				'exclude'         => array( 'bg', 'radius', 'alignment' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		// Get Content Parts
		$before  = $settings['text_before'];
		$dynamic = sifency_addons_get_dynamic_text( $settings );
		$after   = $settings['text_after'];

		$html_tag        = $settings['html_tag'];
		$wrapper_classes = array( 'sifency-advanced-heading-wrapper', 'sifency-heading-style' );
		$wrapper_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		// Check if Link
		$has_link = ! empty( $settings['link']['url'] );
		if ( $has_link ) {
			$this->add_link_attributes( 'link_wrapper', $settings['link'] );
		}
		?>
		
		<<?php echo esc_html( $html_tag ); ?> <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php if ( $has_link ) : ?>
				<a <?php echo wp_kses_post( $this->get_render_attribute_string( 'link_wrapper' ) ); ?>>
			<?php endif; ?>

			<?php if ( ! empty( $before ) ) : ?>
				<span class="sifency-static-part sifency-text-before"><?php echo esc_html( $before ); ?></span>
			<?php endif; ?>

			<span class="sifency-dynamic-part"><?php echo wp_kses_post( $dynamic ); ?></span>

			<?php if ( ! empty( $after ) ) : ?>
				<span class="sifency-static-part sifency-text-after"><?php echo esc_html( $after ); ?></span>
			<?php endif; ?>

			<?php if ( $has_link ) : ?>
				</a>
			<?php endif; ?>
		</<?php echo esc_html( $html_tag ); ?>>
		<?php
	}
}
