<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Smooth_Scroll 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-smooth-scroll',
			'title'          => 'Sifency Smooth Scroll (Lenis)',
			'icon'           => 'eicon-scroll',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'lenis', 'sifency-smooth-scroll-script' ),
			'style_depends'  => array( 'sifency-frontend-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_settings',
			array( 'label' => esc_html__( 'Scroll Configuration', 'sifency-addons' ) )
		);
		$this->add_control(
			'info_alert',
			array(
				'type' => Controls_Manager::RAW_HTML,
				'raw'  => '<div style="background: #e0f2f1; padding: 12px; border-left: 4px solid #009688; color: #00695c; font-size: 13px;">
                    <strong>Active:</strong> This widget activates smooth scroll globally. Place it in your <strong>Header or Footer</strong> template for best results.
                </div>',
			)
		);
		$this->add_control(
			'lerp',
			array(
				'label'       => esc_html__( 'Damping (Lerp)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0.01,
						'max'  => 0.5,
						'step' => 0.01,
					),
				),
				'default'     => array( 'size' => 0.1 ),
				'description' => 'The "heaviness" of the scroll. Lower = Smoother/Slower.',
			)
		);
		$this->add_control(
			'wheelMultiplier',
			array(
				'label'       => esc_html__( 'Speed Multiplier', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0.5,
						'max'  => 4,
						'step' => 0.1,
					),
				),
				'default'     => array( 'size' => 1 ),
				'description' => 'Multiplies mouse wheel input. 1 is standard. 1.5 is faster.',
			)
		);
		$this->add_control(
			'smooth_touch',
			array(
				'label'       => esc_html__( 'Enable on Mobile', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'no',
				'description' => 'Not recommended. Native mobile scrolling usually feels best.',
			)
		);
		$this->add_control(
			'orientation',
			array(
				'label'   => esc_html__( 'Orientation', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'vertical',
				'options' => array(
					'vertical'   => 'Vertical',
					'horizontal' => 'Horizontal',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			echo '<div class="elementor-alert elementor-alert-info">Smooth Scroll is disabled in Editor Mode.</div>';
			return;
		}

		$config = array(
			'lerp'            => $settings['lerp']['size'] ?? 0.1,
			'wheelMultiplier' => $settings['wheelMultiplier']['size'] ?? 1,
			'orientation'     => $settings['orientation'],
			'smoothTouch'     => $settings['smooth_touch'] === 'yes',
			'infinite'        => false,
		);
		?>
		<div id="sifency-lenis-config" data-lenis-config='<?php echo wp_json_encode( $config ); ?>'></div>
		<?php
	}
}
