<?php 

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

if ( function_exists( 'WC' ) ) {

	function woo_shrk_slides_image_grid_enable() {
		remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );

		if ( shrk_get_theme_option( 'woo_slides_image_grid' ) == '1' ) {
			remove_action( 'woocommerce_before_shop_loop_item_title', 'woo_shrk_template_loop_product_thumbnail_hover', 10 );
			add_action(    'woocommerce_before_shop_loop_item_title', 'woo_shrk_template_loop_product_thumbnail_slider', 10 );
		} else {
			remove_action( 'woocommerce_before_shop_loop_item_title', 'woo_shrk_template_loop_product_thumbnail_slider', 10 );
			add_action(    'woocommerce_before_shop_loop_item_title', 'woo_shrk_template_loop_product_thumbnail_hover', 10 );
		}
	}

	function woo_shrk_slides_image_grid_disable() {
		remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
		remove_action( 'woocommerce_before_shop_loop_item_title', 'woo_shrk_template_loop_product_thumbnail_slider', 10 );
		add_action(    'woocommerce_before_shop_loop_item_title', 'woo_shrk_template_loop_product_thumbnail_hover', 10 );
	}

	add_action( 'init', 'woo_shrk_slides_image_grid_enable' );

}






function woo_shrk_template_loop_product_thumbnail_slider() {

	global $post, $woocommerce, $product;
	$attachment_ids = woo_shrk_get_gallery_image_ids( $product );

	if ( !$attachment_ids ) { //No need for swiper, call default fn & return.
		woocommerce_template_loop_product_thumbnail();
		return;
	}

	?>
	<div class="product-grid-images-slider small-arrows">
		<div class="swiper-wrapper">
		<?php
			if ( has_post_thumbnail() ) {
				
				$image_alt   = trim( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) );
				$image_obj   = wp_get_attachment_image_src( get_post_thumbnail_id(), 'shop_catalog' );
				$image_link  = $image_obj[0];
				$image_ratio = shrk_get_image_ratio( get_post_thumbnail_id(), 'shop_catalog' );
				$srcset      = wp_get_attachment_image_srcset( get_post_thumbnail_id(), 'shop_catalog' );
				$sizes       = wp_get_attachment_image_sizes( get_post_thumbnail_id(), 'shop_catalog' );
				$image       = '<img src="'. esc_url( get_template_directory_uri() . '/img/transp1px.png' ) . '" data-src="' . esc_url($image_link) . '" srcset="' . esc_attr( $srcset ) . '" sizes="' . esc_attr( $sizes ) . '" class="swiper-lazy" alt="' . esc_attr( $image_alt ) . '">';

				printf( '<div class="swiper-slide swiper-lazy-not-loaded"><div class="image-wrap" style="padding-bottom: %s">%s</div></div>', intval( 100/$image_ratio ) . '%', $image );

			} else {

				printf( '<div class="swiper-slide"><div class="image-wrap"><img src="%s" alt="%s" /></div></div>', wc_placeholder_img_src(), esc_html__( 'Placeholder', 'ux-shop' ) );

			}

			if ( $attachment_ids ) {
		
				foreach ( $attachment_ids as $attachment_id ) {
					$image_alt   = trim( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) );
					$image_obj   = wp_get_attachment_image_src( $attachment_id, 'shop_catalog' );
					$image_link  = $image_obj[0];
					$image_ratio = shrk_get_image_ratio( $attachment_id, 'shop_catalog' );
					$srcset      = wp_get_attachment_image_srcset( $attachment_id, 'shop_catalog' );
					$sizes       = wp_get_attachment_image_sizes( $attachment_id, 'shop_catalog' );
					$image       = '<img src="'. esc_url( get_template_directory_uri() . '/img/transp1px.png' ) . '" data-src="' . esc_url($image_link) . '" srcset="' . esc_attr( $srcset ) . '" sizes="' . esc_attr( $sizes ) . '" class="swiper-lazy" alt="' . esc_attr( $image_alt ) . '">';
					
					printf( '<div class="swiper-slide swiper-lazy-not-loaded"><div class="image-wrap" style="padding-bottom: %s">%s</div></div>', intval( 100/$image_ratio ) . '%', $image );
					
				}
			
			}
		?>
		</div>
		<div class="prev_swipe"><i class="icon-ws-arrow-left"></i></div>
		<div class="next_swipe"><i class="icon-ws-arrow-right"></i></div>
		<div class="pagination-wrapper"><div class="pagination"></div></div>
	</div>
	<?php
}

function woo_shrk_template_loop_product_thumbnail_hover() {

	global $post, $woocommerce, $product;
	$attachment_ids = woo_shrk_get_gallery_image_ids( $product );
	$classes = '';

	if ( empty( $attachment_ids ) ) {
		$classes .= 'one-image ';
	}

	if ( shrk_get_theme_option( 'woo_transparent_product_images' ) ) {
		$classes .= 'transp ';
	}

?>
	<div class="product-images-hover <?php echo esc_attr( $classes ); ?>">
		<?php
			if ( has_post_thumbnail() ) {
				
				$image_alt   = trim( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) );
				$image_obj   = wp_get_attachment_image_src( get_post_thumbnail_id(), 'shop_catalog' );
				$srcset      = wp_get_attachment_image_srcset( get_post_thumbnail_id(), 'shop_catalog' );
				$sizes       = wp_get_attachment_image_sizes( get_post_thumbnail_id(), 'shop_catalog' );
				$image_link  = $image_obj[0];
				echo '<img src="' . esc_url( $image_link ) . '" srcset="' . esc_attr( $srcset ) . '" sizes="' . esc_attr( $sizes ) . '" class="img-a" alt="' . esc_attr( $image_alt ) . '">';

			} else {

				printf( '<img src="%s" alt="%s" class="img-a"/>', wc_placeholder_img_src(), esc_html__( 'Placeholder', 'ux-shop' ) );

			}

			if ( !empty( $attachment_ids ) ) {
		
				$image_alt   = trim( get_post_meta( $attachment_ids[0], '_wp_attachment_image_alt', true ) );
				$image_obj  = wp_get_attachment_image_src( $attachment_ids[0], 'shop_catalog' );
				$srcset     = wp_get_attachment_image_srcset( $attachment_ids[0], 'shop_catalog' );
				$sizes      = wp_get_attachment_image_sizes( $attachment_ids[0], 'shop_catalog' );
				$image_link = $image_obj[0];
				echo '<img src="' . esc_url( $image_link ) . '" srcset="' . esc_attr( $srcset ) . '" sizes="' . esc_attr( $sizes ) . '" class="img-b" alt="' . esc_attr( $image_alt ) . '">';
				
			}
		?>
	</div>
	<?php
}

?>