<?php
/**
 * Slider Layout for Product Showcase
 *
 * @package Product_Display
 */

// Exit if accessed directly
if ( ! defined('ABSPATH') ) {
	exit;
}

/**
 * Class PRODDISP_Slider
 *
 * This class handles the rendering of the slider layout for product showcase.
 * It extends the base layout class and provides methods to render products in a slider format,
 * including category filtering and product actions.
 */

class PRODDISP_Slider extends PRODDISP_Layout_Base {
	/**
	 * Render the slider layout
	 *
	 * @param array $products WooCommerce product objects
	 * @return string HTML output
	 */
	public function render( $products ) {
		// Enqueue slider-specific assets
		PRODDISP_Assets::get_instance()->enqueue_slider_style_assets();

		// Handle pro layout modes
		$pro_result = $this->handle_pro_layout_mode('slider-2', 'PRODDISP_PRO_Slider', 'slider-2-locked-style-2.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('slider-3', 'PRODDISP_PRO_Slider', 'slider-3-locked-style-3.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('slider-4', 'PRODDISP_PRO_Slider', 'slider-4-locked-style-4.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('slider-5', 'PRODDISP_PRO_Slider', 'slider-5-locked-style-5.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('slider-6', 'PRODDISP_PRO_Slider', 'slider-6-locked-style-6.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		// Only output slider if there are products
		if ( empty($products) ) {
			// Optionally, return a message or nothing
			return '<div class="wcps-slider-empty">No products found.</div>';
		}

		$slider_id = 'wcps-slider-' . rand(1000, 9999);

		$slider_options = array(
			'slidesToShow'   => absint($this->settings['columns'] ?? 4),
			'slidesToScroll' => 1,
			'autoplay'       => ( $this->settings['autoplay'] ?? 'no' ) === 'yes',
			'autoplaySpeed'  => absint($this->settings['slide_speed'] ?? 3000),
			'arrows'         => ( $this->settings['arrows'] ?? 'yes' ) === 'yes',
			'dots'           => ( $this->settings['dots'] ?? 'yes' ) === 'yes',
			'responsive'     => array(
				array(
					'breakpoint' => 1024,
					'settings'   => array(
						'slidesToShow' => min(absint($this->settings['columns'] ?? 4), 3),
					),
				),
				array(
					'breakpoint' => 768,
					'settings'   => array(
						'slidesToShow' => min(absint($this->settings['columns'] ?? 4), 2),
					),
				),
				array(
					'breakpoint' => 480,
					'settings'   => array(
						'slidesToShow' => 1,
					),
				),
			),
		);

		$output = '<div id="' . esc_attr($slider_id) . '" class="wcps-slider-container">';

		foreach ( $products as $product ) {
			$output .= $this->render_product($product);
		}

		$output .= '</div>';

		// Add initialization script
		$output .= '<script type="text/javascript">
            jQuery(document).ready(function($) {
                $("#' . esc_attr($slider_id) . '").slick(' . json_encode($slider_options) . ');
            });
        </script>';

		return $output;
	}

	/**
	 * Render a single product in the slider
	 *
	 * @param WC_Product $product The product object
	 * @return string HTML for the product
	 */
	private function render_product( $product ) {
		$output = '<div class="wcps-slider-item" itemscope itemtype="https://schema.org/Product">';
		$output .= '<div class="wcps-product">';

		// Add structured data
		$output .= '<meta itemprop="name" content="' . esc_attr($product->get_name()) . '">';
		$output .= '<meta itemprop="url" content="' . esc_url(get_permalink($product->get_id())) . '">';
		if ( $product->get_sku() ) {
			$output .= '<meta itemprop="sku" content="' . esc_attr($product->get_sku()) . '">';
		}

		// Product image
		$output .= '<div class="wcps-product-image">';
		$output .= '<a href="' . esc_url(get_permalink($product->get_id())) . '">';

		if ( $product->get_image_id() ) {
			$alt_text = get_post_meta($product->get_image_id(), '_wp_attachment_image_alt', true);
			if ( empty($alt_text) ) {
				$alt_text = $product->get_name();
			}
			$output .= wp_get_attachment_image($product->get_image_id(), $this->settings['image_size'] ?? 'woocommerce_thumbnail', false, array(
				'class' => 'wcps-image',
				'style' => 'width: 100%; height: auto; display: block; transition: transform 0.3s ease;',
				'loading' => 'lazy',
				'alt' => esc_attr($alt_text),
			));
		} else {
			$output .= wc_placeholder_img($this->settings['image_size'] ?? 'woocommerce_thumbnail', array(
				'class' => 'wcps-image',
				'alt' => esc_attr($product->get_name() . ' - ' . __('Product Image', 'product-display')),
			));
		}

		$output .= '</a>';

		// Show action buttons if position is hover
		if ( ( $this->settings['action_position'] ?? 'below' ) === 'hover' ) {
			$output .= $this->render_product_actions($product);
		}

		$output .= '</div>'; // End .wcps-product-image

		// Product content
		$output .= $this->render_product_content($product);

		// Show action buttons if position is below
		if ( ( $this->settings['action_position'] ?? 'below' ) === 'below' ) {
			$output .= $this->render_product_actions($product);
		}

		$output .= '</div>'; // End .wcps-product
		$output .= '</div>'; // End .wcps-slider-item

		return $output;
	}
}
