<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
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_Dual_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-dual-color-heading',
			'title'         => 'Sifency Dual Color Heading',
			'icon'          => 'eicon-heading',
			'category'      => 'sifency-typography-widgets',
			'keywords'      => array( 'heading', 'title', 'text', 'animation', 'stroke', 'split' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Heading Content', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_style',
			array(
				'label'   => esc_html__( 'Layout', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'inline',
				'options' => array(
					'inline' => esc_html__( 'Inline (Side by Side)', 'sifency-addons' ),
					'stack'  => esc_html__( 'Stack (Top/Bottom)', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'first_heading',
			array(
				'label'   => esc_html__( 'First Part Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => esc_html__( 'Dual', 'sifency-addons' ),
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'second_heading',
			array(
				'label'   => esc_html__( 'Second Part Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => esc_html__( 'Color', 'sifency-addons' ),
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'show_subtitle',
			array(
				'label'   => esc_html__( 'Show Subtitle', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'subtitle_text',
			array(
				'label'     => esc_html__( 'Subtitle', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => esc_html__( 'Add a catchy subtitle here', 'sifency-addons' ),
				'condition' => array( 'show_subtitle' => 'yes' ),
			)
		);
		$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(
			'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',
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( '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',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-dch-wrapper' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'heading_style',
				'label'      => 'First Part Style',
				'selector'   => '.sifency-heading-style',
				'color_only' => false,
				'exclude'    => array( 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Second Part',
				'selector'   => '.sifency-content-style',
				'color_only' => false,
				'exclude'    => array( 'alignment' ),

			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'Subtitle',
				'selector'   => '.sifency-label-style',
				'color_only' => true,
				'condition'  => array(
					'show_subtitle'  => 'yes',
					'subtitle_text!' => '',
				),
			)
		);

		$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 );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-dch-wrapper' );
		$this->add_render_attribute( 'heading', 'class', 'sifency-dch-container' );
		$this->add_render_attribute( 'heading', 'class', 'sifency-dch-layout-' . $settings['layout_style'] );

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

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_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 if ( ! empty( $settings['first_heading'] ) ) : ?>
					<span class="sifency-dch-part-1 sifency-heading-style"><?php echo wp_kses_post( $settings['first_heading'] ); ?></span>
				<?php endif; ?>

				<?php if ( ! empty( $settings['second_heading'] ) ) : ?>
					<span class="sifency-dch-part-2 sifency-content-style"><?php echo wp_kses_post( $settings['second_heading'] ); ?></span>
				<?php endif; ?>
			</<?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
	}
}
