<?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_Custom_CSS_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Custom_Title_Widget extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-addons-title',
			'title'         => 'Sifency Title',
			'icon'          => 'eicon-heading',
			'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(
			'heading_content_section',
			array( 'label' => esc_html__( 'Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'title_source',
			array(
				'label'   => esc_html__( 'Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'custom'     => esc_html__( 'Custom', 'sifency-addons' ),
					'post_title' => esc_html__( 'Post Title', 'sifency-addons' ),
				),
				'default' => 'custom',
			)
		);
		$this->add_control(
			'heading_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => esc_html__( 'Sifency Heading Title', 'sifency-addons' ),
				'condition' => array( 'title_source' => 'custom' ),
				'dynamic'   => array( 'active' => true ),
			)
		);
		$this->add_control(
			'link',
			array(
				'label'   => esc_html__( 'Link', 'sifency-addons' ),
				'type'    => Controls_Manager::URL,
				'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' => 'h2',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'heading_style',
				'label'           => 'Heading Style',
				'selector'        => '.sifency-heading-style',
				'show_blend_mode' => true,
				'show_indent'     => true,
			)
		);
		$this->add_Sifency_Addons_Custom_CSS_Controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$text     = ( 'post_title' === $settings['title_source'] ) ? get_the_title() : $settings['heading_text'];
		$this->sifency_addons_render_custom_css();
		if ( empty( $text ) ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$html_tag          = tag_escape( $settings['html_tag'] );
		$this->add_render_attribute( 'heading', 'class', 'sifency-heading sifency-heading-style' );
		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'link', $settings['link'] );
		}
		$wrapper_classes = array( 'sifency-heading-wrapper' );
		$fill_type       = $settings['text_fill_advanced_background'] ?? '';
		$has_image       = ! empty( $settings['text_fill_advanced_image']['url'] );
		if ( 'gradient' === $fill_type || ( 'classic' === $fill_type && $has_image ) ) {
			$wrapper_classes[] = 'sifency-heading--text-fill';
		}
		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				<a <?php echo wp_kses_post( $this->get_render_attribute_string( 'link' ) ); ?>>
			<?php endif; ?>
			<<?php echo esc_html( $html_tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( 'heading' ) ); ?>>
				<?php echo wp_kses_post( $text ); ?>
			</<?php echo esc_html( $html_tag ); ?>>
			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				</a>
			<?php endif; ?>
		</div>
		<?php
	}
}
