<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

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;

class Sifency_Dropcap extends Widget_Base {

	public function get_name() {
		return 'sifency_dropcap';
	}

	public function get_title() {
		return esc_html__( 'Sifency Dropcap', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-editor-quote';
	}

	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}

	public function get_style_depends() {
		return [ 'sifency-dropcap-style' ];
	}

	protected function register_controls() {

		// ==========================
		// CONTENT: GENERAL
		// ==========================
		$this->start_controls_section(
			'section_content',
			[ 'label' => esc_html__( 'Content', 'sifency-addons' ) ]
		);

		$this->add_control(
			'dropcap_type',
			[
				'label' => esc_html__( 'Dropcap Type', 'sifency-addons' ),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'text' => [ 'title' => 'Letter', 'icon' => 'eicon-t-letter' ],
					'icon' => [ 'title' => 'Icon', 'icon' => 'eicon-star' ],
					'image' => [ 'title' => 'Image', 'icon' => 'eicon-image' ],
				],
				'default' => 'text',
				'toggle' => false,
			]
		);

		$this->add_control(
			'dropcap_letter',
			[
				'label' => esc_html__( 'Letter', 'sifency-addons' ),
				'type' => Controls_Manager::TEXT,
				'default' => 'D',
				'placeholder' => 'A',
				'condition' => [ 'dropcap_type' => 'text' ],
				'dynamic' => [ 'active' => true ],
			]
		);

		$this->add_control(
			'dropcap_icon',
			[
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type' => Controls_Manager::ICONS,
				'default' => [ 'value' => 'fas fa-star', 'library' => 'fa-solid' ],
				'condition' => [ 'dropcap_type' => 'icon' ],
			]
		);

		$this->add_control(
			'dropcap_image',
			[
				'label' => esc_html__( 'Image', 'sifency-addons' ),
				'type' => Controls_Manager::MEDIA,
				'default' => [ 'url' => \Elementor\Utils::get_placeholder_image_src() ],
				'condition' => [ 'dropcap_type' => 'image' ],
			]
		);

		$this->add_control(
			'content_text',
			[
				'label' => esc_html__( 'Paragraph Content', 'sifency-addons' ),
				'type' => Controls_Manager::WYSIWYG,
				'default' => 'ropcap widgets add a professional, magazine-style look to your blog posts. It grabs the reader\'s attention immediately at the start of the article.',
			]
		);

		$this->end_controls_section();

		// ==========================
		// STYLE: DROPCAP
		// ==========================
		$this->start_controls_section(
			'section_style_dropcap',
			[ 'label' => esc_html__( 'Dropcap Style', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
		);

		// --- Common Position ---
		$this->add_responsive_control(
			'dropcap_margin',
			[
				'label' => esc_html__( 'Margin', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'default' => [ 'top' => 10, 'right' => 20, 'bottom' => 5, 'left' => 0, 'unit' => 'px', 'isLinked' => false ],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);

		$this->add_responsive_control(
			'dropcap_padding',
			[
				'label' => esc_html__( 'Padding', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'dropcap_bg',
				'selector' => '{{WRAPPER}} .sifency-dropcap-item',
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'dropcap_border',
				'selector' => '{{WRAPPER}} .sifency-dropcap-item',
			]
		);

		$this->add_responsive_control(
			'dropcap_radius',
			[
				'label' => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'dropcap_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-dropcap-item',
			]
		);

		// --- Text/Icon Specific ---
		$this->add_control(
			'heading_typography',
			[
				'label' => esc_html__( 'Typography & Color', 'sifency-addons' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => [ 'dropcap_type!' => 'image' ],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'dropcap_typography',
				'selector' => '{{WRAPPER}} .sifency-dropcap-item',
				'condition' => [ 'dropcap_type!' => 'image' ],
			]
		);

		// Modern Gradient Text Feature
		$this->add_control(
			'text_fill',
			[
				'label' => esc_html__( 'Text Fill', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'color' => 'Solid Color',
					'gradient' => 'Gradient',
				],
				'default' => 'color',
				'condition' => [ 'dropcap_type' => 'text' ],
			]
		);

		$this->add_control(
			'dropcap_color',
			[
				'label' => esc_html__( 'Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'default' => '#54a0ff',
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item' => 'color: {{VALUE}};' ],
				'condition' => [ 'text_fill' => 'color', 'dropcap_type!' => 'image' ],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'dropcap_gradient',
				'types' => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .sifency-dropcap-item.is-text',
				'condition' => [ 'text_fill' => 'gradient', 'dropcap_type' => 'text' ],
				'fields_options' => [
					'background' => [ 'label' => 'Gradient Fill' ],
				],
			]
		);

		$this->add_control(
			'gradient_clip_fix',
			[
				'type' => Controls_Manager::HEADING,
				'label' => '',
				'condition' => [ 'text_fill' => 'gradient', 'dropcap_type' => 'text' ],
				'selectors' => [
					'{{WRAPPER}} .sifency-dropcap-item.is-text' => '-webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; color: transparent;',
				],
			]
		);

		// Text Stroke
		$this->add_control(
			'text_stroke_sw',
			[
				'label' => esc_html__( 'Text Stroke', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'condition' => [ 'dropcap_type' => 'text' ],
			]
		);

		$this->add_control(
			'text_stroke_color',
			[
				'label' => esc_html__( 'Stroke Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'condition' => [ 'text_stroke_sw' => 'yes', 'dropcap_type' => 'text' ],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item' => '-webkit-text-stroke-color: {{VALUE}};' ],
			]
		);

		$this->add_responsive_control(
			'text_stroke_width',
			[
				'label' => esc_html__( 'Stroke Width', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'max' => 5, 'step' => 0.1 ] ],
				'condition' => [ 'text_stroke_sw' => 'yes', 'dropcap_type' => 'text' ],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item' => '-webkit-text-stroke-width: {{SIZE}}{{UNIT}};' ],
			]
		);

		// Image Specific
		$this->add_responsive_control(
			'image_width',
			[
				'label' => esc_html__( 'Image Width', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 20, 'max' => 300 ] ],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-item img' => 'width: {{SIZE}}{{UNIT}};' ],
				'condition' => [ 'dropcap_type' => 'image' ],
			]
		);

		$this->end_controls_section();

		// ==========================
		// STYLE: PARAGRAPH
		// ==========================
		$this->start_controls_section(
			'section_style_paragraph',
			[ 'label' => esc_html__( 'Paragraph', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'paragraph_typography',
				'selector' => '{{WRAPPER}} .sifency-dropcap-content',
			]
		);

		$this->add_control(
			'paragraph_color',
			[
				'label' => esc_html__( 'Text Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-content' => 'color: {{VALUE}};' ],
			]
		);

		$this->add_responsive_control(
			'paragraph_align',
			[
				'label' => esc_html__( 'Alignment', 'sifency-addons' ),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'left' => [ 'icon' => 'eicon-text-align-left' ],
					'justify' => [ 'icon' => 'eicon-text-align-justify' ],
					'right' => [ 'icon' => 'eicon-text-align-right' ],
				],
				'selectors' => [ '{{WRAPPER}} .sifency-dropcap-content' => 'text-align: {{VALUE}};' ],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$type = $settings['dropcap_type'];
		?>
		
		<div class="sifency-dropcap-container">
			
			<div class="sifency-dropcap-item 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'], [ 'aria-hidden' => 'true' ] ); ?>
				
				<?php elseif ( 'image' === $type ) : ?>
					<?php echo wp_kses_post ( \Elementor\Group_Control_Image_Size::get_attachment_image_html( $settings, 'thumbnail', 'dropcap_image' ) ); ?>
				
				<?php endif; ?>
			</div>

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

		</div>

		<?php
	}
}