<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
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_Combine_Button_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Section_Title extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Combine_Button_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-section-title',
			'title'         => 'Sifency Section Title',
			'icon'          => 'eicon-t-letter',
			'category'      => 'sifency-addons-widgets',
			'keywords'      => array( 'title', 'heading', 'section', 'text' ),
			'style_depends' => array( 'sifency-section-title-style', 'sifency-typo-style', 'sifency-button-style' ),
		);
	}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Content', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'show_subtitle',
			array(
				'label'   => esc_html__( 'Show Subtitle', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'subtitle',
			array(
				'label'     => esc_html__( 'Subtitle', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Our Services', 'sifency-addons' ),
				'condition' => array( 'show_subtitle' => 'yes' ),
				'dynamic'   => array( 'active' => true ),
			)
		);
		$this->add_control( 'hr_1', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'show_heading',
			array(
				'label'   => esc_html__( 'Show Heading', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'heading',
			array(
				'label'     => esc_html__( 'Heading Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => esc_html__( 'We Provide Best Solutions', 'sifency-addons' ),
				'condition' => array( 'show_heading' => 'yes' ),
				'dynamic'   => array( 'active' => true ),
			)
		);
		$this->add_control(
			'heading_tag',
			array(
				'label'     => esc_html__( 'Heading Tag', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'h1'  => 'H1',
					'h2'  => 'H2',
					'h3'  => 'H3',
					'h4'  => 'H4',
					'h5'  => 'H5',
					'h6'  => 'H6',
					'div' => 'div',
				),
				'default'   => 'h2',
				'condition' => array( 'show_heading' => 'yes' ),
			)
		);
		$this->add_control( 'hr_2', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'show_large_desc',
			array(
				'label'   => esc_html__( 'Show Large Description', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'large_desc',
			array(
				'label'     => esc_html__( 'Large Paragraph', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => esc_html__( 'This is a larger introductory text that stands out.', 'sifency-addons' ),
				'condition' => array( 'show_large_desc' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'large_desc_margin',
			array(
				'label'       => esc_html__( 'Move Top/Bottom', 'sifency-addons' ),
				'type'        => Controls_Manager::DIMENSIONS,
				'size_units'  => array( 'px', '%', 'em' ),
				'selectors'   => array(
					'{{WRAPPER}} .sifency-title-large-desc' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'condition'   => array( 'show_large_desc' => 'yes' ),
				'description' => esc_html__( 'Use Top and Bottom values to move the text.', 'sifency-addons' ),
			)
		);
		$this->add_control( 'hr_3', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'show_desc',
			array(
				'label'   => esc_html__( 'Show Description', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'desc',
			array(
				'label'     => esc_html__( 'Description', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => esc_html__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'sifency-addons' ),
				'condition' => array( 'show_desc' => 'yes' ),
			)
		);
		$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',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-section-title-wrapper' => 'text-align: {{VALUE}};',
				),
				'separator' => 'before',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_combine_button_content_controls(
			array(
				'prefix' => 'primary_btn',
				'label'  => 'Primary Button',
			)
		);
		$this->add_sifency_addons_help_controls();

		// --- STYLE SECTIONS ---
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => esc_html__( 'Caption', 'sifency-addons' ),
				'selector'  => '.sifency-label-style',
				'condition' => array(
					'subtitle!'     => '',
					'show_subtitle' => 'yes',
				),
				'exclude'   => array( 'tabs', 'transition', 'alignment' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Title',
				'selector'  => '.sifency-heading-style',
				'condition' => array(
					'heading!'     => '',
					'show_heading' => 'yes',
				),
				'exclude'   => array( 'tabs', 'transition', 'alignment' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'meta_style',
				'label'     => 'Large Paragraph',
				'selector'  => '.sifency-meta-style',
				'condition' => array(
					'large_desc!'     => '',
					'show_large_desc' => 'yes',
				),
				'exclude'   => array( 'tabs', 'transition', 'alignment' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Description',
				'selector'   => '.sifency-content-style',
				'condition'  => array(
					'desc!'     => '',
					'show_desc' => 'yes',
				),
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'alignment', 'bg', 'radius', 'shadow', 'stroke' ),
			)
		);

		$this->add_Sifency_Addons_Combine_Button_Controls(
			array(
				'prefix'   => 'primary_btn',
				'selector' => '.sifency-addons-primary-button',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_attrs = $animation_manager->get_wrapper_attributes_array();
		if ( ! empty( $wrapper_attrs ) ) {
			$this->add_render_attribute( 'wrapper', $wrapper_attrs );
		}
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-section-title-wrapper' );

		// Subtitle
		if ( 'yes' === $settings['show_subtitle'] && ! empty( $settings['subtitle'] ) ) {
			$subtitle_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $subtitle_attrs ) ) {
				$this->add_render_attribute( 'subtitle', $subtitle_attrs );
			}
			$this->add_render_attribute( 'subtitle', 'class', array( 'sifency-title-subtitle', 'sifency-label-style' ) );
		}

		// Heading
		if ( 'yes' === $settings['show_heading'] && ! empty( $settings['heading'] ) ) {
			$heading_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $heading_attrs ) ) {
				$this->add_render_attribute( 'heading', $heading_attrs );
			}
			$this->add_render_attribute( 'heading', 'class', array( 'sifency-title-heading', 'sifency-heading-style' ) );
		}

		// Large Desc
		if ( 'yes' === $settings['show_large_desc'] && ! empty( $settings['large_desc'] ) ) {
			$large_desc_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $large_desc_attrs ) ) {
				$this->add_render_attribute( 'large_desc', $large_desc_attrs );
			}
			$this->add_render_attribute( 'large_desc', 'class', array( 'sifency-title-large-desc', 'sifency-meta-style' ) );
		}

		// Desc
		if ( 'yes' === $settings['show_desc'] && ! empty( $settings['desc'] ) ) {
			$desc_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $desc_attrs ) ) {
				$this->add_render_attribute( 'desc', $desc_attrs );
			}
			$this->add_render_attribute( 'desc', 'class', array( 'sifency-title-desc', 'sifency-content-style' ) );
		}

		// Button
		$btn_attrs = $animation_manager->get_item_attributes_array();
		if ( ! empty( $btn_attrs ) ) {
			$this->add_render_attribute( 'btn_wrapper', $btn_attrs );
		}
		$this->add_render_attribute( 'btn_wrapper', 'class', 'sifency-section-title-btn-wrapper' );

		?>
		<div <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
			
			<?php if ( 'yes' === $settings['show_subtitle'] && ! empty( $settings['subtitle'] ) ) : ?>
				<div <?php $this->print_render_attribute_string( 'subtitle' ); ?>><?php echo esc_html( $settings['subtitle'] ); ?></div>
			<?php endif; ?>

			<?php
			if ( 'yes' === $settings['show_heading'] && ! empty( $settings['heading'] ) ) :
				$heading_tag = ! empty( $settings['heading_tag'] ) ? $settings['heading_tag'] : 'h2';
				?>
				<<?php echo esc_attr( $heading_tag ); ?> <?php $this->print_render_attribute_string( 'heading' ); ?>>
					<?php echo wp_kses_post( $settings['heading'] ); ?>
				</<?php echo esc_attr( $heading_tag ); ?>>
			<?php endif; ?>

			<?php if ( 'yes' === $settings['show_large_desc'] && ! empty( $settings['large_desc'] ) ) : ?>
				<div <?php $this->print_render_attribute_string( 'large_desc' ); ?>><?php echo wp_kses_post( $settings['large_desc'] ); ?></div>
			<?php endif; ?>

			<?php if ( 'yes' === $settings['show_desc'] && ! empty( $settings['desc'] ) ) : ?>
				<div <?php $this->print_render_attribute_string( 'desc' ); ?>><?php echo wp_kses_post( $settings['desc'] ); ?></div>
			<?php endif; ?>

			<div <?php $this->print_render_attribute_string( 'btn_wrapper' ); ?>>
				<?php $this->add_sifency_addons_combine_button_render( $settings, 'primary_btn', 'sifency-addons-primary-button' ); ?>
			</div>

		</div>
		<?php
	}
}
