<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;  }

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Icons_Manager;
use Elementor\Group_Control_Css_Filter;
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_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Dropcap extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency_dropcap',
			'title'         => 'Sifency Dropcap',
			'icon'          => 'eicon-editor-quote',
			'category'      => 'sifency-typography-widgets',
			'keywords'      => array( 'heading', 'title', 'text', 'animation', 'stroke', 'split' ),
			'style_depends' => array( 'sifency-dropcap-style', 'sifency-typo-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'dropcap_type',
			array(
				'label'   => esc_html__( 'Dropcap Type', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'text'  => array(
						'title' => 'Letter',
						'icon'  => 'eicon-t-letter',
					),
					'icon'  => array(
						'title' => 'Icon',
						'icon'  => 'eicon-star',
					),
					'image' => array(
						'title' => 'Image',
						'icon'  => 'eicon-image',
					),
				),
				'default' => 'text',
				'toggle'  => false,
			)
		);
		$this->add_control(
			'dropcap_letter',
			array(
				'label'       => esc_html__( 'Letter', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'D',
				'placeholder' => 'A',
				'condition'   => array( 'dropcap_type' => 'text' ),
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'dropcap_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-star',
					'library' => 'fa-solid',
				),
				'condition' => array( 'dropcap_type' => 'icon' ),
			)
		);
		$this->add_control(
			'dropcap_image',
			array(
				'label'     => esc_html__( 'Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => \Elementor\Utils::get_placeholder_image_src() ),
				'condition' => array( 'dropcap_type' => 'image' ),
			)
		);
		$this->add_control(
			'content_text',
			array(
				'label'   => esc_html__( 'Paragraph Content', 'sifency-addons' ),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => esc_html__(
					'Dropcap widgets give your content a bold, magazine-style opening. They highlight the first letter and instantly catch the reader’s attention, making your text more engaging and professional.',
					'sifency-addons'
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Dropcap',
				'selector'  => '.sifency-heading-style',
				'condition' => array( 'dropcap_type' => 'text' ),
				'exclude'   => array( 'tabs', 'transition', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Content',
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'shadow', 'stroke', 'border', 'radius', 'bg' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector'  => '.sifency-dropcap-item.is-icon i, .sifency-dropcap-item.is-icon svg',
				'condition' => array( 'dropcap_type' => 'icon' ),
				'exclude'   => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '.sifency-dropcap-item.is-image img',
				'selectors' => array(
					'image'      => '.sifency-dropcap-item.is-image img',
					'container'  => '.sifency-dropcap-item.is-image img',
					'dimensions' => '.sifency-dropcap-item.is-image img',
				),
				'condition' => array( 'dropcap_type' => 'image' ),
				'exclude'   => array( 'tabs', 'transitions' ),
			)
		);
		$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();

		$type = $settings['dropcap_type'];
		?>
		
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-dropcap-container" <?php echo wp_kses_post( $animation_wrapper_attrs ); ?>>
			
			<div class="sifency-dropcap-item sifency-heading-style is-<?php echo esc_attr( $type ); ?>">
				<?php if ( 'text' === $type ) : ?>
					<?php echo esc_html( $settings['dropcap_letter'] ); ?>
				
				<?php elseif ( 'icon' === $type ) : ?>
					<?php \Elementor\Icons_Manager::render_icon( $settings['dropcap_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				
				<?php elseif ( 'image' === $type ) : ?>
					<?php echo \Elementor\Group_Control_Image_Size::get_attachment_image_html( $settings, 'thumbnail', 'dropcap_image' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally. ?> 
				
				<?php endif; ?>
			</div>

			<div class="sifency-dropcap-content sifency-content-style">
				<?php echo wp_kses_post( $settings['content_text'] ); ?>
			</div>

		</div>

		<?php
	}
}
