<?php
/**
 * Caption Layout for Product Display.
 *
 * @package Product_Display.
 */

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

/**
 * Class PRODDISP_Caption.
 *
 * This class handles the rendering of the caption layout for product display.
 */
class PRODDISP_Caption extends PRODDISP_Layout_Base {
	/**
	 * Render the caption layout.
	 *
	 * @param array $products WooCommerce product objects.
	 * @return string HTML output.
	 */
	public function render( $products ) {
		// Enqueue caption-specific assets
		PRODDISP_Assets::get_instance()->enqueue_caption_assets();

		$pro_result = $this->handle_pro_layout_mode('book-open-horizontal', 'PRODDISP_Pro_Caption', 'caption-locked-style-3.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('border-reveal-top-left', 'PRODDISP_Pro_Caption', 'caption-locked-style-4.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('bounce-out-right', 'PRODDISP_Pro_Caption', 'caption-locked-style-5.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('circle-top-left', 'PRODDISP_Pro_Caption', 'caption-locked-style-6.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('cube-left', 'PRODDISP_Pro_Caption', 'caption-locked-style-7.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		$pro_result = $this->handle_pro_layout_mode('swiper-slide', 'PRODDISP_Pro_Caption', 'caption-locked-style-8.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		// Others
		$pro_result = $this->handle_pro_layout_mode('liquid-morph', 'PRODDISP_Pro_Caption', 'caption-locked-style-9.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('neon-glow', 'PRODDISP_Pro_Caption', 'caption-locked-style-9.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('origami-fold', 'PRODDISP_Pro_Caption', 'caption-locked-style-10.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('particle-burst', 'PRODDISP_Pro_Caption', 'caption-locked-style-11.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('hologram', 'PRODDISP_Pro_Caption', 'caption-locked-style-12.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('glass-shatter', 'PRODDISP_Pro_Caption', 'caption-locked-style-13.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('magnetic-pull', 'PRODDISP_Pro_Caption', 'caption-locked-style-13.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('quantum-tunnel', 'PRODDISP_Pro_Caption', 'caption-locked-style-13.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('fire-ignition', 'PRODDISP_Pro_Caption', 'caption-locked-style-13.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}
		$pro_result = $this->handle_pro_layout_mode('matrix-rain', 'PRODDISP_Pro_Caption', 'caption-locked-style-13.gif', $products);
		if ( $pro_result !== false ) {
			return $pro_result;
		}

		// Original caption rendering for free layouts
		$columns = absint($this->settings['columns'] ?? 4);
		if ( $columns < 1 ) {
			$columns = 4;
		}

		// Get the current caption mode
		$caption_mode = $this->settings['proddisp_layout_mode'] ?? 'blinds-vertical';

		$output = '<div class="wcps-caption-container" data-caption-mode="' . esc_attr($caption_mode) . '">';

		if ( empty($products) ) {
			$output .= '<p class="wcps-no-products">' . esc_html__('No products found.', 'product-display') . '</p>';
		} else {
			foreach ( $products as $product ) {
				$output .= $this->render_product($product);
			}
		}

		$output .= '</div>';
		return $output;
	}

	/**
	 * Render a single product in the caption layout.
	 *
	 * @param Product_Display $product The product object.
	 * @return string HTML for the product.
	 */
	private function render_product( $product ) {
		$output = '<div class="wcps-caption-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 with caption overlay.
		$output .= '<div class="wcps-product-image">';

		if ( $product->is_on_sale() ) {
			$output .= '<span class="wcps-product-badge" aria-label="' . esc_attr__('On Sale', 'product-display') . '">' . esc_html__('Sale', 'product-display') . '</span>';
		}

		$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',
				'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>';

		// Caption overlay
		$output .= $this->render_caption_overlay($product);

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

		// Product content (below image for caption layouts)
		if ( 'yes' === ( $this->settings['show_title'] ?? 'yes' ) ||
			 'yes' === ( $this->settings['show_price'] ?? 'yes' ) ) {
			$output .= $this->render_product_content($product);
		}

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

		return $output;
	}

	/**
	 * Render caption overlay with content and actions.
	 *
	 * @param Product_Display $product The product object.
	 * @return string HTML for the caption overlay.
	 */
	private function render_caption_overlay( $product ) {
		$output = '<div class="wcps-caption-overlay">';
		$output .= '<div class="wcps-caption-content">';

		// Caption title (always show in overlay)
		$output .= '<h3 class="wcps-caption-title">' . esc_html($product->get_name()) . '</h3>';

		// Caption description (if enabled)
		if ( 'yes' === ( $this->settings['show_description'] ?? 'no' ) ) {
			$excerpt = $product->get_short_description();
			if ( empty($excerpt) ) {
				$excerpt = $product->get_description();
			}

			if ( ! empty($excerpt) ) {
				$excerpt = wp_trim_words($excerpt, ( $this->settings['excerpt_length'] ?? 15 ), '...');
				$output .= '<p class="wcps-caption-description">' . wp_kses_post($excerpt) . '</p>';
			}
		}

		// Caption price (if enabled)
		if ( 'yes' === ( $this->settings['show_price'] ?? 'yes' ) ) {
			$output .= '<div class="wcps-caption-price">' . $product->get_price_html() . '</div>';
		}

		// Caption actions
		$output .= '<div class="wcps-caption-actions">';

		// Add to Cart button
		if ( 'yes' === ( $this->settings['show_addtocart'] ?? 'yes' ) ) {
			$output .= '<a href="' . esc_url($product->add_to_cart_url()) . '" class="wcps-caption-btn wcps-add-to-cart" data-product-id="' . esc_attr($product->get_id()) . '">';
			$output .= '<i class="fas fa-shopping-cart"></i> ' . esc_html($product->add_to_cart_text());
			$output .= '</a>';
		}

		// Quick View button
		if ( 'yes' === ( $this->settings['show_quickview'] ?? 'no' ) ) {
			$output .= '<a href="#" class="wcps-caption-btn wcps-quickview" data-product-id="' . esc_attr($product->get_id()) . '">';
			$output .= '<i class="fas fa-eye"></i> ' . esc_html__('Quick View', 'product-display');
			$output .= '</a>';
		}

		// Wishlist button
		if ( 'yes' === ( $this->settings['show_wishlist'] ?? 'no' ) ) {
			$output .= '<a href="#" class="wcps-caption-btn wcps-wishlist" data-product-id="' . esc_attr($product->get_id()) . '">';
			$output .= '<i class="fas fa-heart"></i> ' . esc_html__('Wishlist', 'product-display');
			$output .= '</a>';
		}

		// Compare button
		if ( 'yes' === ( $this->settings['show_compare'] ?? 'no' ) ) {
			$output .= '<a href="#" class="wcps-caption-btn wcps-compare" data-product-id="' . esc_attr($product->get_id()) . '">';
			$output .= '<i class="fas fa-balance-scale"></i> ' . esc_html__('Compare', 'product-display');
			$output .= '</a>';
		}

		$output .= '</div>'; // End .wcps-caption-actions
		$output .= '</div>'; // End .wcps-caption-content
		$output .= '</div>'; // End .wcps-caption-overlay

		return $output;
	}
}
