<?php
/**
 * @fs_premium_only
 *
 * Pro — Full-screen gallery lightbox on single vehicle pages.
 *
 * Renders the design-system .aii-lightbox (scrim, main frame, prev/next +
 * counter, thumb strip) and a small vanilla-JS controller that opens it from
 * the .aii-gallery links, with keyboard nav, focus trap, and swipe. The full
 * image set (not just the few thumbnails shown on the page) is localized so
 * the lightbox can page through every photo.
 *
 * @since 2.6.0
 * @package AutomotiveFeedImport
 */

if (! defined('ABSPATH')) {
	exit;
}

class AFI_Lightbox {

	/** @var AutomotiveFeedImport */
	private $afi;

	/** @var array|null Cached gallery for the current request. */
	private $gallery = null;

	/**
	 * @param AutomotiveFeedImport $afi Main plugin instance.
	 */
	public function __construct($afi) {
		$this->afi = $afi;
		add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
		add_action('wp_footer', array($this, 'render_shell'));
	}

	/**
	 * The current single vehicle's gallery (cached per request).
	 *
	 * @return array
	 */
	private function gallery() {
		if (null !== $this->gallery) {
			return $this->gallery;
		}
		$this->gallery = array();
		if (is_singular('vehicles')) {
			$id = get_queried_object_id();
			if ($id) {
				$this->gallery = $this->afi->get_vehicle_gallery($id);
			}
		}
		return $this->gallery;
	}

	/**
	 * Enqueue + localize the lightbox script with the full image list.
	 *
	 * @return void
	 */
	public function enqueue_assets() {
		$gallery = $this->gallery();
		if (count($gallery) < 1) {
			return;
		}
		$images = array();
		foreach ($gallery as $img) {
			$images[] = array(
				'full'  => $img['full'],
				'thumb' => $img['thumb'],
				'alt'   => $img['alt'],
			);
		}
		$ver = defined('AFI_VERSION') ? AFI_VERSION : '2.6.0';
		wp_enqueue_script('aii-lightbox', plugins_url('assets/aii-lightbox.js', __FILE__), array(), $ver, true);
		wp_localize_script('aii-lightbox', 'aiiLightbox', array('images' => $images));
	}

	/**
	 * Output the lightbox shell (inside an .aii scope for the design tokens).
	 *
	 * @return void
	 */
	public function render_shell() {
		if (count($this->gallery()) < 1) {
			return;
		}
		$scheme = method_exists($this->afi, 'get_aii_color_scheme') ? $this->afi->get_aii_color_scheme() : 'light';
		?>
		<div class="aii" data-theme="<?php echo esc_attr($scheme); ?>">
			<div class="aii-lightbox" id="afi-lightbox" role="dialog" aria-modal="true" aria-label="<?php esc_attr_e('Vehicle photo viewer', 'automotive-feed-import'); ?>">
				<div class="aii-lightbox__scrim" data-lbclose></div>
				<div class="aii-lightbox__stage">
					<button type="button" class="aii-lightbox__close" data-lbclose aria-label="<?php esc_attr_e('Close', 'automotive-feed-import'); ?>"><svg aria-hidden="true" focusable="false"><use href="#i-x"></use></svg></button>
					<div class="aii-lightbox__frame"><div class="aii-photo"><img id="afi-lb-img" src="" alt=""></div></div>
					<div class="aii-lightbox__bar">
						<button type="button" class="aii-lightbox__nav" data-lbnav="-1" aria-label="<?php esc_attr_e('Previous photo', 'automotive-feed-import'); ?>"><svg aria-hidden="true" focusable="false"><use href="#i-chevL"></use></svg></button>
						<span class="aii-lightbox__count" id="afi-lb-count" aria-live="polite"></span>
						<button type="button" class="aii-lightbox__nav" data-lbnav="1" aria-label="<?php esc_attr_e('Next photo', 'automotive-feed-import'); ?>"><svg aria-hidden="true" focusable="false"><use href="#i-chevR"></use></svg></button>
					</div>
					<div class="aii-lightbox__thumbs" id="afi-lb-thumbs"></div>
				</div>
			</div>
		</div>
		<?php
	}
}
