<?php
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Dnxte_Browser_Scrollbar_Customizer' ) ) {

	class Dnxte_Browser_Scrollbar_Customizer {

		public static function register_customizer_options( $wp_customize ) {

			// Panel and section are already added in the main render_browser_scrollbar function

			// Utility for text controls
			$add_text_control = function ( $id, $label, $default = '', $sanitize = 'sanitize_text_field' ) use ( $wp_customize ) {
				$wp_customize->add_setting(
					$id,
					array(
						'default'           => $default,
						'sanitize_callback' => $sanitize,
					)
				);

				$wp_customize->add_control(
					$id,
					array(
						'label'    => $label,
						'type'     => 'text',
						'section'  => 'dnxte_browser_scrollbar_section',
						'settings' => $id,
					)
				);
			};

			$add_number_control = function ( $id, $label, $default = '', $sanitize = 'absint', $input_attrs = array(), $active_callback = null ) use ( $wp_customize ) {
				$wp_customize->add_setting(
					$id,
					array(
						'default'           => $default,
						'sanitize_callback' => $sanitize,
					)
				);

				$args = array(
					'label'       => $label,
					'type'        => 'number',
					'section'     => 'dnxte_browser_scrollbar_section',
					'settings'    => $id,
					'input_attrs' => wp_parse_args(
						$input_attrs,
						array(
							'min'  => 0,
							'step' => 1,
						)
					),
				);

				if ( is_callable( $active_callback ) ) {
					$args['active_callback'] = $active_callback;
				}

				$wp_customize->add_control( $id, $args );
			};

			// Utility for color controls
			$add_color_control = function ( $id, $label, $default = '', $active_callback = null ) use ( $wp_customize ) {
				$wp_customize->add_setting(
					$id,
					array(
						'default'           => $default,
						'sanitize_callback' => 'sanitize_hex_color',
					)
				);

				$args = array(
					'label'    => $label,
					'section'  => 'dnxte_browser_scrollbar_section',
					'settings' => $id,
				);

				if ( is_callable( $active_callback ) ) {
					$args['active_callback'] = $active_callback;
				}

				$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $id, $args ) );
			};

			// Base Controls
			$add_number_control( 'dnxte_scrollbar_width', __( 'Scrollbar Width', 'divi-essential' ), 8, array( __CLASS__, 'sanitize_css_value' ) );
			$add_number_control( 'dnxte_scrollbar_border_radius', __( 'Scrollbar Border Radius', 'divi-essential' ), 4, array( __CLASS__, 'sanitize_css_value' ) );
			$add_color_control( 'dnxte_scrollbar_thumb_color', __( 'Scrollbar Thumb Color', 'divi-essential' ), '#555' );
			$add_color_control( 'dnxte_scrollbar_track_bg_color', __( 'Scrollbar Track Background Color', 'divi-essential' ), '#ccc' );

			// Scrollbar Shadow Effect (Checkbox)
			$wp_customize->add_setting(
				'dnxte_scrollbar_shadow_enable',
				array(
					'default'           => false,
					'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
				)
			);

			$wp_customize->add_control(
				'dnxte_scrollbar_shadow_enable',
				array(
					'label'   => __( 'Enable Scrollbar Shadow Effect', 'divi-essential' ),
					'type'    => 'checkbox',
					'section' => 'dnxte_browser_scrollbar_section',
				)
			);

			// Shadow effect active callback function
			$shadow_active_callback = function () {
				return get_theme_mod( 'dnxte_scrollbar_shadow_enable', false );
			};

			// Conditionally visible shadow color controls
			$add_color_control(
				'dnxte_scrollbar_thumb_shadow_color',
				__( 'Scrollbar Thumb Shadow Color', 'divi-essential' ),
				'#000000',
				$shadow_active_callback
			);

			$add_color_control(
				'dnxte_scrollbar_rail_shadow_color',
				__( 'Scrollbar Rail Background Shadow Color', 'divi-essential' ),
				'#000000',
				$shadow_active_callback
			);

			// Shadow offset and blur controls (also conditional)
			$add_number_control(
				'dnxte_scrollbar_shadow_offset_y',
				__( 'Scrollbar Shadow Offset-Y', 'divi-essential' ),
				2,
				array( __CLASS__, 'sanitize_css_value' ),
				array(),
				$shadow_active_callback
			);

			$add_number_control(
				'dnxte_scrollbar_shadow_blur',
				__( 'Scrollbar Shadow Blur', 'divi-essential' ),
				4,
				array( __CLASS__, 'sanitize_css_value' ),
				array(),
				$shadow_active_callback
			);

			// Auto Hide Options
			$wp_customize->add_setting(
				'dnxte_scrollbar_autohide',
				array(
					'default'           => 'true',
					'sanitize_callback' => array( __CLASS__, 'sanitize_autohide_option' ),
				)
			);

			$wp_customize->add_control(
				'dnxte_scrollbar_autohide',
				array(
					'label'   => __( 'Auto Hide Scrollbar', 'divi-essential' ),
					'type'    => 'radio',
					'section' => 'dnxte_browser_scrollbar_section',
					'choices' => array(
						'false' => __( 'Always Show', 'divi-essential' ),
						'true'  => __( 'When Scrolling', 'divi-essential' ),
						'auto'  => __( 'Automatically (based on mouse/trackpad)', 'divi-essential' ),
					),
				)
			);

			// Mouse Scroll Step
			$add_number_control( 'dnxte_mouse_scroll_step', __( 'Mouse Scroll Step', 'divi-essential' ), '40', 'absint' );

			// Scroll Speed
			$add_number_control( 'dnxte_scroll_speed', __( 'Scroll Speed', 'divi-essential' ), '1', 'absint' );

			// Scrollbar Align
			$wp_customize->add_setting(
				'dnxte_scrollbar_align',
				array(
					'default'           => 'ltr',
					'sanitize_callback' => array( __CLASS__, 'sanitize_align_option' ),
				)
			);

			$wp_customize->add_control(
				'dnxte_scrollbar_align',
				array(
					'label'   => __( 'Scrollbar Align', 'divi-essential' ),
					'type'    => 'radio',
					'section' => 'dnxte_browser_scrollbar_section',
					'choices' => array(
						'ltr' => __( 'Right Sidebar', 'divi-essential' ),
						'rtl' => __( 'Left Sidebar', 'divi-essential' ),
					),
				)
			);
		}

		public static function sanitize_css_value( $value ) {
			return absint( $value );
		}

		public static function sanitize_checkbox( $checked ) {
			return ( isset( $checked ) && true == $checked ) ? true : false;
		}

		public static function sanitize_autohide_option( $value ) {
			$valid = array( 'true', 'false', 'auto' );

			if ( in_array( $value, $valid, true ) ) {
				return $value;
			}

			// Fallback to default
			return 'true';
		}

		public static function sanitize_align_option( $value ) {
			return in_array( $value, array( 'ltr', 'rtl' ), true ) ? $value : 'ltr';
		}
	}
}
