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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_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;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Addons_Widget_Multi_Color_Heading extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	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-multi-color-heading',
			'title'         => 'Sifency Multi-Color Heading',
			'icon'          => 'eicon-heading',
			'category'      => 'sifency-typography-widgets',
			'keywords'      => array( 'heading', 'title', 'text', 'animation', 'stroke', 'split' ),
			'style_depends' => array( 'sifency-frontend-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Sentence Builder', 'sifency-addons' ),
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Global Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'    => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center'  => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'right'   => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => 'Justify',
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'   => 'center',
				'selectors' => array(
					'{{WRAPPER}} .sifency-dch-container' => 'text-align: {{VALUE}}; justify-content: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'header_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',
				),
				'default' => 'h2',
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'text',
			array(
				'label'       => esc_html__( 'Text Segment', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => 'Text',
				'dynamic'     => array( 'active' => true ),
				'label_block' => true,
			)
		);
		$repeater->add_control(
			'display_type',
			array(
				'label'     => esc_html__( 'Display Behavior', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'inline',
				'options'   => array(
					'inline' => 'Inline (Continue Sentence)',
					'block'  => 'Block (New Line / Full Width)',
				),
				'selectors' => array(
					'{{WRAPPER}} {{CURRENT_ITEM}}' => 'display: {{VALUE}};',
				),
			)
		);

		$repeater->start_controls_tabs( 'segment_style_tabs' );

		$repeater->start_controls_tab( 'tab_seg_text', array( 'label' => 'Text' ) );

		$repeater->add_control(
			'text_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => 'color: {{VALUE}}' ),
			)
		);

		$repeater->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} {{CURRENT_ITEM}}',
			)
		);

		$repeater->add_control(
			'stroke_width',
			array(
				'label'     => 'Stroke Width',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => '-webkit-text-stroke-width: {{SIZE}}px;' ),
			)
		);

		$repeater->add_control(
			'stroke_color',
			array(
				'label'     => 'Stroke Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => '-webkit-text-stroke-color: {{VALUE}};' ),
				'condition' => array( 'stroke_width[size]!' => '' ),
			)
		);

		$repeater->end_controls_tab();

		$repeater->start_controls_tab( 'tab_seg_box', array( 'label' => 'Box' ) );

		$repeater->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'background',
				'selector' => '{{WRAPPER}} {{CURRENT_ITEM}}',
				'types'    => array( 'classic', 'gradient' ),
			)
		);

		$repeater->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'border',
				'selector' => '{{WRAPPER}} {{CURRENT_ITEM}}',
			)
		);

		$repeater->add_responsive_control(
			'padding',
			array(
				'label'      => 'Padding',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$repeater->add_responsive_control(
			'margin',
			array(
				'label'      => 'Margin',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$repeater->add_responsive_control(
			'border_radius',
			array(
				'label'      => 'Radius',
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( '{{WRAPPER}} {{CURRENT_ITEM}}' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);

		$repeater->end_controls_tab();
		$repeater->end_controls_tabs();

		$this->add_control(
			'heading_items',
			array(
				'label'       => esc_html__( 'Sentence Parts', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'text'         => 'We Create',
						'display_type' => 'inline',
					),
					array(
						'text'         => 'Digital',
						'text_color'   => '#007BFF',
						'display_type' => 'inline',
					),
					array(
						'text'         => 'Experiences',
						'display_type' => 'block',
					),
					array(
						'text'         => 'For You',
						'display_type' => 'inline',
					),
				),
				'title_field' => '{{{ text }}}',
			)
		);
		$this->add_control( 'hr_link', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'link',
			array(
				'label'   => esc_html__( 'Link', 'sifency-addons' ),
				'type'    => Controls_Manager::URL,
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'show_subtitle',
			array(
				'label'     => 'Show Subtitle',
				'type'      => Controls_Manager::SWITCHER,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'subtitle_text',
			array(
				'label'     => 'Subtitle',
				'type'      => Controls_Manager::TEXTAREA,
				'condition' => array( 'show_subtitle' => 'yes' ),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'Subtitle Style',
				'selector'   => '.sifency-dch-subtitle',
				'condition'  => array( 'show_subtitle' => 'yes' ),
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'bg', 'radius', 'stroke', 'shadow' ),
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();
	}

	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 );

		$temp_key = 'sifency_temp_class_for_parsing';
		$this->add_render_attribute( 'wrapper', 'class', $temp_key );
		$elementor_wrapper_attrs_string = $this->get_render_attribute_string( 'wrapper' );
		$this->remove_render_attribute( 'wrapper', 'class', $temp_key );

		$elementor_wrapper_classes = array();
		if ( preg_match( '/class="([^"]*)"/', $elementor_wrapper_attrs_string, $matches ) ) {
			$elementor_wrapper_classes = explode( ' ', $matches[1] );
			$elementor_wrapper_classes = array_filter(
				$elementor_wrapper_classes,
				function ( $class ) use ( $temp_key ) {
					return $class !== $temp_key;
				}
			);
		}

		$final_wrapper_attrs = $animation_manager->get_wrapper_attributes( $elementor_wrapper_classes );

		$this->add_render_attribute( 'heading', 'class', $temp_key );
		$elementor_heading_attrs_string = $this->get_render_attribute_string( 'heading' );
		$this->remove_render_attribute( 'heading', 'class', $temp_key ); // Clean up

		$elementor_heading_classes = array();
		if ( preg_match( '/class="([^"]*)"/', $elementor_heading_attrs_string, $matches ) ) {
			$elementor_heading_classes = explode( ' ', $matches[1] );
			$elementor_heading_classes = array_filter(
				$elementor_heading_classes,
				function ( $class ) use ( $temp_key ) {
					return $class !== $temp_key;
				}
			);
		}

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-dch-wrapper' );
		$this->add_render_attribute( 'heading', 'class', 'sifency-dch-container' );

		$tag = Utils::validate_html_tag( $settings['header_tag'] );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $final_wrapper_attrs ); ?>>
			
			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				<a href="<?php echo esc_url( $settings['link']['url'] ); ?>" class="sifency-dch-link">
			<?php endif; ?>

			<<?php echo wp_kses_post( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( 'heading' ) ); ?>>
				<?php
				foreach ( $settings['heading_items'] as $index => $item ) :
					$item_key = 'item_' . $index;
					$this->add_render_attribute( $item_key, 'class', 'sifency-dch-segment' );
					$this->add_render_attribute( $item_key, 'class', 'elementor-repeater-item-' . $item['_id'] );

					$this->add_render_attribute( $item_key, 'class', $temp_key );
					$elementor_item_attrs_string = $this->get_render_attribute_string( $item_key );
					$this->remove_render_attribute( $item_key, 'class', $temp_key );

					$elementor_item_classes = array();
					if ( preg_match( '/class="([^"]*)"/', $elementor_item_attrs_string, $matches ) ) {
						$elementor_item_classes = explode( ' ', $matches[1] );
						$elementor_item_classes = array_filter(
							$elementor_item_classes,
							function ( $class ) use ( $temp_key ) {
								return $class !== $temp_key;
							}
						);
					}

					$final_item_attrs = $animation_manager->get_item_attributes( $elementor_item_classes );
					?>
					<span <?php echo wp_kses_post( $final_item_attrs ); ?>>
						<?php echo wp_kses_post( $item['text'] ); ?>
					</span>
				<?php endforeach; ?>
			</<?php echo wp_kses_post( $tag ); ?>>

			<?php if ( 'yes' === $settings['show_subtitle'] && ! empty( $settings['subtitle_text'] ) ) : ?>
				<div class="sifency-dch-subtitle sifency-label-style"><?php echo wp_kses_post( $settings['subtitle_text'] ); ?></div>
			<?php endif; ?>

			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				</a>
			<?php endif; ?>

		</div>
		<?php
	}
}
