<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_3D_Letter extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-3d-letter',
			'title'          => 'Sifency 3D Letter',
			'icon'           => 'eicon-typography',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'fontloader', 'textgeometry', 'sifency-3d-letter-script' ),
			'style_depends'  => array( 'sifency-sun-style' ),
			'keywords'       => array( 'typography', '3d', 'webgl', 'three', 'extrusion' ),
		);
	}

	protected function register_controls() {

		// ================= Content Tab =================
		$this->start_controls_section(
			'content_section',
			array(
				'label' => esc_html__( 'Text Settings', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'title_text',
			array(
				'label'   => esc_html__( 'Main Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'AVA SRG', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'target_letter',
			array(
				'label'   => esc_html__( 'Letter to Animate', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'R', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'3d_depth',
			array(
				'label'       => esc_html__( '3D Depth (Thickness)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'size_units'  => array( 'px' ),
				'range'       => array(
					'px' => array(
						'min'  => 5,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default'     => array(
					'unit' => 'px',
					'size' => 20,
				),
				'description' => esc_html__( 'How thick the 3D letter should be.', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'animation_speed',
			array(
				'label'      => esc_html__( 'Animation Speed (s)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 's' ),
				'range'      => array(
					's' => array(
						'min'  => 1,
						'max'  => 20,
						'step' => 0.5,
					),
				),
				'default'    => array(
					'unit' => 's',
					'size' => 6,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-3d-extruded-wrap' => 'animation-duration: {{SIZE}}s;',
				),
			)
		);

		$this->end_controls_section();

		// ================= Style Tab =================
		$this->start_controls_section(
			'style_section',
			array(
				'label' => esc_html__( 'Typography & Colors', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .sifency-3d-title',
			)
		);

		$this->add_control(
			'normal_text_color',
			array(
				'label'     => esc_html__( 'Normal Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-normal-letter' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'front_face_color',
			array(
				'label'     => esc_html__( '3D Letter - Front Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-3d-front' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'side_face_color',
			array(
				'label'     => esc_html__( '3D Letter - Extrusion (Side) Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-3d-side' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-3d-back' => 'color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function render_dom( array $context ) {
		$settings      = $this->get_settings_for_display();
		$text          = $settings['title_text'];
		$target_letter = $settings['target_letter'];
		$depth         = $settings['3d_depth']['size']; // e.g., 20px
		$wrapper_id    = esc_attr( $context['dom_id'] );

		if ( empty( $text ) ) {
			return;
		}

		$chars = preg_split( '//u', $text, -1, PREG_SPLIT_NO_EMPTY );
		?>

		<div class="sifency-3d-title-wrap">
			<h2 class="sifency-3d-title">
				<span class="sifency-word-span">
					<?php
					foreach ( $chars as $char ) {
						if ( $char === ' ' ) {
							echo '&nbsp;';
						} elseif ( mb_strtolower( $char ) === mb_strtolower( $target_letter ) ) {

							// Generate the Deep 3D Extruded Letter
							echo '<span class="sifency-letter-span sifency-3d-extruded-wrap">';

							// Generate Layers for depth
							for ( $i = 0; $i <= $depth; $i++ ) {
								if ( $i === 0 ) {
									$class = 'sifency-3d-layer sifency-3d-front';
								} elseif ( $i === $depth ) {
									$class = 'sifency-3d-layer sifency-3d-back';
								} else {
									$class = 'sifency-3d-layer sifency-3d-side';
								}
								// Pass the index as a CSS variable for translateZ calculation
								echo '<span class="' . esc_attr( $class ) . '" style="--layer-index: ' . esc_attr( $i ) . ';">' . esc_html( $char ) . '</span>';
							}

							echo '</span>';

						} else {
							echo '<span class="sifency-letter-span sifency-normal-letter">' . esc_html( $char ) . '</span>';
						}
					}
					?>
				</span>
			</h2>
		</div>

		<?php
	}
}
