<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Custom setting class for nested option paths
if ( class_exists( 'WP_Customize_Setting' ) && ! class_exists( 'Fl_Nested_Icon_Setting' ) ) {
	/**
	 * Custom setting class that properly handles saving to deeply nested option paths.
	 * WordPress Customizer's default WP_Customize_Setting doesn't correctly save nested paths.
	 */
	class Fl_Nested_Icon_Setting extends WP_Customize_Setting {
		public $network_id = '';

		public function __construct( $manager, $id, $args = array() ) {
			if ( isset( $args['network_id'] ) ) {
				$this->network_id = $args['network_id'];
				unset( $args['network_id'] );
			}
			parent::__construct( $manager, $id, $args );
		}

		/**
		 * Override value() to manually read from nested option structure.
		 * WordPress Customizer's default value() doesn't correctly read deeply nested paths.
		 *
		 * NOTE: We cannot override save() because WP_Customize_Setting::save() is final.
		 * Instead, we rely on the pre_update_option_fl_settings filter to handle saving.
		 */
		public function value() {
			if ( empty( $this->network_id ) ) {
				// Extract network_id from setting ID if not provided
				if ( preg_match( '/\[networks\]\[([^\]]+)\]\[icon\]/', $this->id, $matches ) ) {
					$this->network_id = $matches[1];
				}
			}

			$fl_settings = get_option( 'fl_settings', array() );

			// Check if icon is saved in database (check if key exists, not just if value is non-empty)
			if ( ! empty( $this->network_id ) && isset( $fl_settings['social_icons']['networks'][ $this->network_id ]['icon'] ) ) {
				$saved_icon = $fl_settings['social_icons']['networks'][ $this->network_id ]['icon'];
				// Return saved icon if it exists (even if empty string, as that's a valid "cleared" value)
				// Only use default if the key doesn't exist at all
				return $saved_icon;
			}

			// No saved icon found, return default
			return $this->default;
		}
	}
}

if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'Fl_Icons_Control' ) ) {

	class Fl_Icons_Control extends WP_Customize_Control {

		/**
		 * Whitelist content parameter.
		 *
		 * @var string
		 */
		public $content = '';

		/**
		 * Render the control's content.
		 *
		 * Allows the content to be overridden without having to rewrite the wrapper.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function render_content() {

			if ( empty( $this->choices ) ) {
				return;
			}

			$name = '_customize-radio-' . esc_attr( $this->id );

			if ( ! empty( $this->label ) ) :
				?>
				<span class="customize-control-title">
					<?php echo esc_html( $this->label ); ?>
				</span>
				<?php
			endif;

			if ( ! empty( $this->description ) ) :
				?>
				<span class="description customize-control-description">
					<?php echo esc_html( $this->description ); ?>
				</span>
				<?php
			endif;

			foreach ( $this->choices as $value => $label ) :
				?>
				<label class="label">
					<div class="floating_icon_holder">
						<input 
							type="radio" 
							value="<?php echo esc_attr( $label ); ?>" 
							name="<?php echo esc_attr( $name ); ?>"
							<?php
							$this->link();
							checked( $this->value(), $label );
							?>
						/>
						<div class="fl_bg_holder">
							<?php if ( strpos( $label, 'dashicons' ) !== false ) : ?>
								<span class="<?php echo esc_attr( $label ); ?>"></span>
							<?php else : ?>
								<span class="fa fa-<?php echo esc_attr( $label ); ?>"></span>
							<?php endif; ?>
						</div>
					</div>
				</label>
				<?php
			endforeach;
		}
	}
}


if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'Fl_Social_Icons_Control' ) ) {
	/**
	 * Control for selecting a social icon class (saves the full class string).
	 *
	/**
	 * Control for selecting a social icon class (saves the full class string).
	 *
	 * @since 1.0.0
	 */
	class Fl_Social_Icons_Control extends WP_Customize_Control {
		public $content = '';

		public function render_content() {
			if ( empty( $this->choices ) ) {
				return;
			}

			$name = '_customize-radio-' . $this->id;

			if ( ! empty( $this->label ) ) {
				echo '<span class="customize-control-title">' . esc_html( $this->label ) . '</span>';
			}

			if ( ! empty( $this->description ) ) {
				echo '<span class="description customize-control-description">' . esc_html( $this->description ) . '</span>';
			}

			foreach ( $this->choices as $value => $label ) {
				// sanitize the value/key (this is what gets saved)
				$value_sanitized = sanitize_text_field( $value );
				?>
				<label class="label">
					<div class="floating_icon_holder">
						<input
							type="radio"
							name="<?php echo esc_attr( $name ); ?>"
							value="<?php echo esc_attr( $value_sanitized ); ?>"
							<?php $this->link(); ?>
							<?php checked( $this->value(), $value_sanitized ); ?>
						/>
						<div class="fl_bg_holder">
							<?php if ( 'svg' === $value_sanitized ) : ?>
								<!-- Twitter/X SVG Icon Preview -->
								<svg xmlns="http://www.w3.org/2000/svg" fill="#000" viewBox="0 0 512 512" style="width: 20px; height: 20px; display: inline-block; vertical-align: middle;">
									<path d="M389.2 48h70.6L305.6 224.2 487 464H345L233.7 318.6 106.5 464H35.8L200.7 275.5 26.8 48H172.4L272.9 180.9 389.2 48zM364.4 421.8h39.1L151.1 88h-42L364.4 421.8z"/>
								</svg>
							<?php else : ?>
								<span class="<?php echo esc_attr( $value_sanitized ); ?>"></span>
							<?php endif; ?>
						</div>
					</div>
				</label>
				<?php
			}
		}
	}
}

if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'WP_Customize_Range_Control' ) ) {
	/**
	 * Simple range control for the customizer.
	 *
	 * @since 1.0.0
	 */
	class WP_Customize_Range_Control extends WP_Customize_Control {
		public $type = 'custom_range';

		public function render_content() {
			?>
			<label>
				<?php if ( ! empty( $this->label ) ) : ?>
					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
				<?php endif; ?>

				<div class="range_holder">
					<input
						data-input-type="range"
						type="range"
						<?php $this->input_attrs(); ?>
						value="<?php echo esc_attr( $this->value() ); ?>"
						<?php $this->link(); ?>
					/>
					<div class="cs-range-value"><?php echo esc_html( $this->value() ); ?></div>
				</div>

				<?php if ( ! empty( $this->description ) ) : ?>
					<span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
				<?php endif; ?>
			</label>
			<?php
		}
	}
}
