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

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

class Sifency_Addons_Widget_Custom_Cursor 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-custom-cursor',
			'title'          => 'eicon-cursor-move',
			'icon'           => 'eicon-cursor',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-custom-cursor-script' ),
			'style_depends'  => array( 'sifency-custom-cursor-style' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section( 'cursor_settings', array( 'label' => 'Cursor Settings' ) );

		$this->add_control(
			'target_selector',
			array(
				'label'       => 'Choose Custom Cursor Area',
				'type'        => Controls_Manager::TEXT,
				'default'     => 'body',
				'description' => 'CSS Selector (e.g., "body", ".my-section").',
			)
		);

		$this->add_control(
			'cursor_type',
			array(
				'label'   => 'Choose Cursor Type',
				'type'    => Controls_Manager::SELECT,
				'default' => 'circle',
				'options' => array(
					'circle' => 'Circle / Modern Shape',
					'image'  => 'Image',
					'icon'   => 'Icon',
					'text'   => 'Text',
				),
			)
		);

		$this->add_control(
			'cursor_style',
			array(
				'label'     => 'Pre-built Layout Styles',
				'type'      => Controls_Manager::SELECT,
				'default'   => 'style-classic',
				'condition' => array( 'cursor_type' => 'circle' ),
				'options'   => array(
					'style-classic'    => '1. Classic (Dot + Ring)',
					'style-difference' => '2. Difference Blend (Inverted)',
					'style-glass'      => '3. Glassmorphism',
					'style-neon'       => '4. Cyberpunk Neon',
					'style-blob'       => '5. Fluid Blob',
					'style-outline'    => '6. Thick Outline',
					'style-spotlight'  => '7. Spotlight Glow',
					'style-square'     => '8. Geometric Square',
					'style-brutal'     => '9. Brutalism Star',
					'style-shadow'     => '10. Neumorphic Shadow',
					'style-dual-anim'  => '11. Animated Dual Cursor (Script Engine)',
				),
			)
		);

		$this->add_control(
			'cursor_icon',
			array(
				'label'     => 'Choose from Pre-defined Cursor Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'cursor_type' => 'icon' ),
			)
		);

		$this->add_control(
			'cursor_image',
			array(
				'label'     => 'Upload Custom Cursor Icon',
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'cursor_type' => 'image' ),
			)
		);

		$this->add_control(
			'cursor_text',
			array(
				'label'     => 'Cursor Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'EXPLORE',
				'condition' => array( 'cursor_type' => 'text' ),
			)
		);

		$this->add_control(
			'cursor_color',
			array(
				'label'     => 'Cursor Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-modern-cursor' => '--sf-cursor-color: {{VALUE}};',
					'body'                               => '--sf-cursor-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'click_effect',
			array(
				'label'   => 'Change Cursor on Click',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'cursor_speed',
			array(
				'label'       => 'Cursor Speed',
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0.01,
				'max'         => 1.00,
				'step'        => 0.01,
				'default'     => 0.15,
				'description' => 'Lower value = smoother delay. Higher value = tighter/faster follow.',
			)
		);

		$this->add_control(
			'hide_default_cursor',
			array(
				'label'       => 'Hide Default Cursor',
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Disable this if you want the default browser cursor to remain visible.',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
	}

	protected function render_dom( array $context ) {
		$settings = $this->get_settings_for_display();
		$type     = $settings['cursor_type'];
		$style    = ( $type === 'circle' ) ? $settings['cursor_style'] : 'style-base';

		$data_settings = array(
			'target'       => $settings['target_selector'],
			'click_effect' => $settings['click_effect'],
			'style'        => $style,
			'speed'        => ! empty( $settings['cursor_speed'] ) ? (float) $settings['cursor_speed'] : 0.15,
			'hide_default' => $settings['hide_default_cursor'] === 'yes',
		);
		?>
		<div class="sifency-modern-cursor-wrapper" data-settings='<?php echo esc_attr( wp_json_encode( $data_settings, JSON_UNESCAPED_UNICODE ) ); ?>'>
			<div class="sifency-modern-cursor type-<?php echo esc_attr( $type ); ?> <?php echo esc_attr( $style ); ?>">
				
				<div class="sifency-cursor-dot">
					<?php
					if ( 'image' === $type && ! empty( $settings['cursor_image']['url'] ) ) {
						echo '<img src="' . esc_url( $settings['cursor_image']['url'] ) . '" alt="custom-cursor" />';
					} elseif ( 'icon' === $type && ! empty( $settings['cursor_icon']['value'] ) ) {
						\Elementor\Icons_Manager::render_icon( $settings['cursor_icon'] );
					}
					?>
				</div>

				<div class="sifency-cursor-follower">
					<?php
					if ( 'text' === $type ) {
						echo '<span class="cursor-text">' . esc_html( $settings['cursor_text'] ) . '</span>';
					}
					?>
				</div>

			</div>
		</div>
		<?php
	}
}
