<?php
/**
 * Single Product Image
 *
 */

use Automattic\WooCommerce\Enums\ProductType;

defined( 'ABSPATH' ) || exit;
	

$pgsfw_layout_gallery = get_option('pgsfw_layout');
$pgsfw_style_gallery = get_option('pgsfw_lstyle');

// Note: `wc_get_gallery_image_html` was added in WC 3.3.2 and did not exist prior. This check protects against theme overrides being used on older versions of WC.
if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
	return;
}

global $product;

$columns           = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$post_thumbnail_id = $product->get_image_id();
$wrapper_classes   = apply_filters(
	'woocommerce_single_product_image_gallery_classes',
	array(
		'woocommerce-product-gallery',
		'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ),
		'woocommerce-product-gallery--columns-' . absint( $columns ),
		'images',
	)
);
?>

<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 0; transition: opacity .25s ease-in-out;">
	<div class="woocommerce-product-gallery__wrapper">

	<?php
		global $product;
		$attachment_ids = $product->get_gallery_image_ids();
		$main_image_id = $product->get_image_id();
		$main_img_src = wp_get_attachment_image_url($main_image_id, 'large');
		$main_img_full = wp_get_attachment_image_url($main_image_id, 'full');
		
		if ($attachment_ids) { ?>
			<div class="pgsfw-slider-for <?php if($pgsfw_layout_gallery == true) { ?>pgsfw-<?php echo esc_attr($pgsfw_style_gallery); }?>">	
				
			<?php
			
			foreach ($attachment_ids as $attachment_id) {
				$large = wp_get_attachment_image_url($attachment_id, 'large');
				$video = get_post_meta($attachment_id, '_pgsw_youtube_url_field', true);
				$loading_icon = plugins_url('img/loading.gif' , __FILE__) ;
				 if (!empty($video)) {
					echo '<div class="video-slide">
						<div class="iframe-loader">
							<img src="' . esc_url($loading_icon) . '" alt="Loading...">
						</div>
						
						<iframe 
							loading="lazy"
							data-src="https://www.youtube.com/embed/' . esc_attr($video) . '?rel=0&modestbranding=1&autoplay=0&controls=1" 
							frameborder="0" 
							allow="autoplay; encrypted-media" 
							allowfullscreen>
						</iframe>
					</div>';
				} else {
					echo '<div><a href="javascript:;" data-fancybox="gallery" data-src="' . esc_url($large) . '">';
					echo '<img class="wc_main_img" src="' . esc_url($large) . '" data-large="' . esc_url($large) . '">';
					echo '</a></div>';
				}
				
			}		
			echo '</div>';
			
		}else{
			echo '<div class="single_wimg"><a href="javascript:;" data-fancybox="gallery" data-src="' . esc_url($main_img_full) . '">';
			echo '<img class="wc_main_img" src="' . esc_url($main_img_full) . '" >';
			echo '</a></div>';
		}

		if ($attachment_ids) { ?>
			<div id="pgsfw-gallery" class="pgsfw-slider-thumb <?php if($pgsfw_layout_gallery == true) { ?>vertical_gt<?php echo esc_attr($pgsfw_style_gallery); }?>">
			<?php
			foreach ($attachment_ids as $attachment_id) {
				$video = get_post_meta($attachment_id, '_pgsw_youtube_url_field', true);
				$thumb = wp_get_attachment_image_url($attachment_id, 'thumbnail');
				$large = wp_get_attachment_image_url($attachment_id, 'large');
				$video_icon = plugins_url('img/video.png' , __FILE__) ;
				echo '<div class="single_gallery">';
				echo '<img class="wcspg-thumbnail" src="' . esc_url($thumb) . '" data-large="' . esc_url($large) . '">';
				if (!empty($video)){ 
					echo '<img class="video_icon" src="' . esc_url($video_icon) . '" alt="">';			   
				}
				echo '</div>';
			}
			echo '</div>';
		}
	?>
	</div>
</div>
