<?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 Elementor\Group_Control_Border;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Highlighted_Text extends Sifency_Addons_Widget_Base {
	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_highlighted_text',
			'title'         => 'Sifency Highlighted Text',
			'icon'          => 'eicon-edit',
			'category'      => 'sifency-typography-widgets',
			'keywords'      => array( 'heading', 'title', 'text', 'animation', 'stroke', 'split' ),
			'style_depends' => array( 'sifency-typo-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Text Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'text_content',
			array(
				'label'       => esc_html__( 'Description', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'rows'        => 6,
				'default'     => esc_html__( 'This widget lets you {highlight important words} or phrases inside your text with custom styles.', 'sifency-addons' ),
				'description' => esc_html__( 'Wrap the text you want to highlight in curly braces. Example: {This text is highlighted}', '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' => 'h3',
			)
		);
		$this->add_responsive_control(
			'align',
			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',
					),
					'justify' => array(
						'title' => 'Justify',
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'   => 'center',
				'selectors' => array( '{{WRAPPER}} .sifency-highlight-wrapper' => 'text-align: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_highlight',
			array(
				'label' => esc_html__( 'Highlight Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'highlight_shape',
			array(
				'label'        => esc_html__( 'Shape', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'options'      => array(
					'full'      => 'Full Box (Background)',
					'underline' => 'Underline',
					'marker'    => 'Marker (Bottom Half)',
				),
				'default'      => 'full',
				'prefix_class' => 'sifency-hl-shape-',
			)
		);
		$this->add_control(
			'hover_animation',
			array(
				'label' => esc_html__( 'Hover Animation', 'sifency-addons' ),
				'type'  => Controls_Manager::HOVER_ANIMATION,
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'label_style',
				'label'    => 'Highlighted Text Advance',
				'selector' => '.sifency-label-style',
				'exclude'  => array( 'transition', 'tabs', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Normal Text Style',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'transition', 'tabs', 'alignment' ),
			)
		);
	}

	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();
		if ( empty( $settings['text_content'] ) ) {
			return;
		}
		$raw_text       = $settings['text_content'];
		$tag            = ! empty( $settings['html_tag'] ) ? $settings['html_tag'] : 'div';
		$animation      = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . esc_attr( $settings['hover_animation'] ) : '';
		$processed_text = preg_replace(
			'/\{(.*?)\}/',
			'<span class="sifency-hl-text sifency-label-style ' . $animation . '">$1</span>',
			$raw_text
		);
		?>
		<<?php echo esc_attr( $tag ); ?> class="sifency-highlight-wrapper sifency-heading-style">
			<?php echo wp_kses_post( $processed_text ); ?>
		</<?php echo esc_attr( $tag ); ?>>
		<?php
	}
}
