<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! class_exists( 'Sifency_Elementor_Sticky_Controller' ) ) :
	final class Sifency_Elementor_Sticky_Controller {

		private static $instance = null;

		/**
		 * Singleton instance
		 */
		public static function instance() {
			if ( null === self::$instance ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Constructor – set up hooks
		 */
		private function __construct() {

			// Add sticky controls to major element types.
			add_action( 'elementor/element/common/_section_style/after_section_end', array( $this, 'register_controls' ), 10, 2 );
			add_action( 'elementor/element/column/section_layout/after_section_end', array( $this, 'register_controls' ), 10, 2 );
			add_action( 'elementor/element/section/section_layout/after_section_end', array( $this, 'register_controls' ), 10, 2 );
			add_action( 'elementor/element/container/section_layout/after_section_end', array( $this, 'register_controls' ), 10, 2 );
			add_action( 'elementor/element/widget/section_effects/after_section_end', array( $this, 'register_controls' ), 10, 2 );

			// Apply data attributes before rendering on the frontend.
			add_action( 'elementor/frontend/section/before_render', array( $this, 'apply_render_attributes' ) );
			add_action( 'elementor/frontend/column/before_render', array( $this, 'apply_render_attributes' ) );
			add_action( 'elementor/frontend/container/before_render', array( $this, 'apply_render_attributes' ) );
			add_action( 'elementor/frontend/widget/before_render', array( $this, 'apply_render_attributes' ) );

			// Enqueue the frontend JS.
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		}

		/**
		 * Register sticky controls inside the Elementor editor.
		 */
		public function register_controls( $element, $args ) {

			$element->start_controls_section(
				'sifency_sticky_section',
				array(
					'label' => __( 'Sifency Sticky Effects', 'sifency-addons' ),
					'tab'   => \Elementor\Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'sticky_enable_sifency',
				array(
					'label'        => __( 'Enable Sticky', 'sifency-addons' ),
					'type'         => \Elementor\Controls_Manager::SWITCHER,
					'return_value' => 'yes',
					'separator'    => 'before',
				)
			);

			$element->add_control(
				'sticky_offset_sifency',
				array(
					'label'      => __( 'Sticky Offset (px)', 'sifency-addons' ),
					'type'       => \Elementor\Controls_Manager::SLIDER,
					'size_units' => array( 'px' ),
					'range'      => array(
						'px' => array(
							'min' => 0,
							'max' => 300,
						),
					),
					'default'    => array(
						'unit' => 'px',
						'size' => 20,
					),
					'condition'  => array(
						'sticky_enable_sifency' => 'yes',
					),
				)
			);

			$element->add_control(
				'sticky_on_sifency',
				array(
					'label'       => __( 'Enable On Devices', 'sifency-addons' ),
					'type'        => \Elementor\Controls_Manager::SELECT2,
					'multiple'    => true,
					'label_block' => true,
					'default'     => array( 'desktop', 'tablet', 'mobile' ),
					'options'     => array(
						'desktop' => __( 'Desktop', 'sifency-addons' ),
						'tablet'  => __( 'Tablet', 'sifency-addons' ),
						'mobile'  => __( 'Mobile', 'sifency-addons' ),
					),
					'condition'   => array(
						'sticky_enable_sifency' => 'yes',
					),
				)
			);

			$element->end_controls_section();
		}

		/**
		 * Output sticky data attributes on the element wrapper.
		 */
		public function apply_render_attributes( $element ) {

			$settings = $element->get_settings_for_display();

			if ( empty( $settings['sticky_enable_sifency'] ) || 'yes' !== $settings['sticky_enable_sifency'] ) {
				return;
			}

			$offset = ! empty( $settings['sticky_offset_sifency']['size'] )
			? (int) $settings['sticky_offset_sifency']['size']
			: 20;

			$devices = ! empty( $settings['sticky_on_sifency'] )
			? array_map( 'sanitize_text_field', (array) $settings['sticky_on_sifency'] )
			: array( 'desktop', 'tablet', 'mobile' );

			$element->add_render_attribute(
				'_wrapper',
				array(
					'data-sifency-sticky'        => 'true',
					'data-sifency-sticky-offset' => esc_attr( $offset ),
					'data-sifency-sticky-on'     => esc_attr( implode( ',', $devices ) ),
				)
			);
		}

		/**
		 * Enqueue the frontend JS that implements sticky behavior.
		 */
		public function enqueue_scripts() {
			wp_enqueue_script(
				'sifency-sticky-script',
				plugin_dir_url( __FILE__ ) . 'sifency-sticky.js',
				array(),
				'1.0.4',
				true
			);
		}
	}
	Sifency_Elementor_Sticky_Controller::instance();
endif;
