<?php
/**
 * Product Card
 *
 * @package Emoza
 */

/**
 * Hooks 
 */
function emoza_product_card_hooks() {
    $layout            = get_theme_mod( 'shop_archive_layout', 'product-grid' );    
	$button_layout     = get_theme_mod( 'shop_product_add_to_cart_layout', 'layout3' );
	$quick_view_layout = get_theme_mod( 'shop_product_quickview_layout', 'layout1' );
	$wishlist_layout   = get_theme_mod( 'shop_product_wishlist_layout', 'layout1' );
    $wishlist_enable   = Emoza_Modules::is_module_active( 'wishlist' );

    //Loop image wrapper extra class
	$loop_image_wrap_extra_class = 'emoza-add-to-cart-button-'. $button_layout;
	if( 'layout1' !== $quick_view_layout ) {
		$loop_image_wrap_extra_class .= ' emoza-quick-view-button-'. $quick_view_layout;
	}

	if( $wishlist_enable && 'layout1' !== $wishlist_layout ) {
		$loop_image_wrap_extra_class .= ' emoza-wishlist-button-'. $wishlist_layout;
	}

	//Archive layout
	if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) {
		if ( 'product-list' === $layout ) {

			// Products
			add_filter( 'single_product_archive_thumbnail_size', function(){ return 'emoza-big'; } );

			/**
			 * Hook 'emoza_wc_loop_image_wrap_extra_class'
			 *
			 * @since 1.0.0
			 */
			add_action( 'woocommerce_before_shop_loop_item', function() use ($loop_image_wrap_extra_class) { echo '<div class="row valign"><div class="col-md-4"><div class="loop-image-wrap '. esc_attr( apply_filters( 'emoza_wc_loop_image_wrap_extra_class', $loop_image_wrap_extra_class ) ) .'">'; }, 1 );
			add_action( 'woocommerce_before_shop_loop_item_title', function() { echo '</div></div><div class="col-md-8">'; }, 11 );
			add_action( 'woocommerce_after_shop_loop_item', function() { echo '</div>'; }, PHP_INT_MAX );

			// Categories
			add_filter( 'subcategory_archive_thumbnail_size', function(){ return 'emoza-big'; } );

			/**
			 * Hook 'emoza_wc_loop_image_wrap_extra_class'
			 *
			 * @since 1.0.0
			 */
			add_action( 'woocommerce_before_subcategory', function() use ($loop_image_wrap_extra_class) { echo '<div class="row valign"><div class="col-md-4"><div class="loop-image-wrap '. esc_attr( apply_filters( 'emoza_wc_loop_image_wrap_extra_class', $loop_image_wrap_extra_class ) ) .'">'; }, 1 );
			add_action( 'woocommerce_before_subcategory_title', function() { echo '</div></div><div class="col-md-8">'; }, 11 );
			add_action( 'woocommerce_after_subcategory', function() { echo '</div>'; }, PHP_INT_MAX );
		}

		if ( in_array( $layout, array( 'product-grid', 'product-masonry' ) ) ) {
			$shop_woocommerce_catalog_columns_desktop = get_theme_mod( 'shop_woocommerce_catalog_columns_desktop', 4 );
		
			if( $shop_woocommerce_catalog_columns_desktop === 2 ) {
				add_filter( 'single_product_archive_thumbnail_size', function(){ return 'emoza-large'; } );
				add_filter( 'subcategory_archive_thumbnail_size', function(){ return 'emoza-large'; } );
				add_filter( 'woocommerce_get_image_size_emoza-large', function( $size ){ $size[ 'width' ] = 920; return $size; } );
			}
		
			if( $shop_woocommerce_catalog_columns_desktop === 1 ) {
				add_filter( 'single_product_archive_thumbnail_size', function(){ return 'emoza-extra-large'; } );
				add_filter( 'subcategory_archive_thumbnail_size', function(){ return 'emoza-extra-large'; } );
				add_filter( 'woocommerce_get_image_size_emoza-extra-large', function( $size ){ $size[ 'width' ] = 1140; return $size; } );
			}
		}
	}

	/**
	 * Loop product structure
	 */

	//Move link close tag
	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
	add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 12 );

	//Wrap loop image
	if ( in_array( $layout, array( 'product-grid', 'product-masonry' ) ) || is_product() || ( $layout === 'product-list' && ! is_shop() && ! is_product_category() && ! is_product_tag() && ! is_product_taxonomy() ) ) {

		/**
		 * Hook 'emoza_wc_loop_image_wrap_extra_class'
		 *
		 * @since 1.0.0
		 */
		add_action( 'woocommerce_before_shop_loop_item_title', function() use ($loop_image_wrap_extra_class) { echo '<div class="loop-image-wrap '. esc_attr( apply_filters( 'emoza_wc_loop_image_wrap_extra_class', $loop_image_wrap_extra_class ) ) .'">'; }, 9 );
		add_action( 'woocommerce_before_shop_loop_item_title', function() { echo '</div>'; }, 11 );
	}

	if ( in_array( $layout, array( 'product-grid', 'product-masonry' ) ) ) {
		//Move button inside image wrap
		if ( 'layout4' === $button_layout && 'layout3' !== $quick_view_layout || 'layout3' === $button_layout && 'layout2' !== $quick_view_layout ) {
			remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
			add_action( 'woocommerce_before_shop_loop_item_title', function() { emoza_wrap_loop_button_start(); woocommerce_template_loop_add_to_cart(); echo '</div>'; } );
		}
	} elseif ( 'layout4' === $button_layout && 'layout3' !== $quick_view_layout || 'layout3' === $button_layout && 'layout2' !== $quick_view_layout ) {
		//Move button inside image wrap
		remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
		add_action( 'woocommerce_before_shop_loop_item_title', function() { emoza_wrap_loop_button_start(); woocommerce_template_loop_add_to_cart(); echo '</div>'; } );
	}

	//Remove product title, rating, price
	remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );
	remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
	remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price' );

	//Add elements from sortable option
	add_action( 'woocommerce_after_shop_loop_item', 'emoza_loop_product_structure', 9 );

	//Wrap loop button
	if ( 'layout4' !== $button_layout ) {
		add_action( 'woocommerce_after_shop_loop_item', 'emoza_wrap_loop_button_start', 9 );
		add_action( 'woocommerce_after_shop_loop_item', function() { echo '</div>'; }, 11 );
	}

	//Remove button
	if( 'layout1' === $button_layout ) {
		remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
	}

	//Quick view button & add to cart button
	if ( 
		( 'layout4' === $button_layout && 'layout3' === $quick_view_layout ) || 
		( 'layout3' === $button_layout && 'layout2' === $quick_view_layout ) 
	) {
		remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
		add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 10 );
	}

	//Quick view and wishlist buttons
	if ( is_shop() || is_product_category() || is_product_tag() || is_product() || emoza_page_has_woo_blocks() || emoza_page_has_woo_shortcode() || is_cart() || is_404() || is_product_taxonomy() ) {
		if( 'layout1' !== $quick_view_layout || ( $wishlist_enable && 'layout1' !== $wishlist_layout ) ) {
			remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open' );
			remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close' );
			add_action( 'woocommerce_before_shop_loop_item_title', 'emoza_woocommerce_template_loop_product_link_open', 9 );
			add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 11 );
		}

		if( 'layout1' !== $quick_view_layout ) {
			add_action( 'woocommerce_before_shop_loop_item_title', 'emoza_quick_view_button', 10 );
			
			//Quick view popup
			add_action( 'wp_body_open', 'emoza_quick_view_popup' );
			
			// Do not include on single product pages
			if ( current_theme_supports( 'wc-product-gallery-lightbox' ) && false === is_product() ) {
				add_action( 'emoza_footer_after', function(){
					$has_search_popular_products = get_theme_mod( 'shop_search_enable_popular_products', 0 ) ? true : false;

					if ( is_search() && ! $has_search_popular_products ) {
						return;
					}

					wc_get_template( 'single-product/photoswipe.php' );
				} );
			}
		}

		if( $wishlist_enable && 'layout1' !== $wishlist_layout ) {
			add_action( 'woocommerce_before_shop_loop_item_title', 'emoza_wishlist_button', 10 );
		}
	}

	$shop_cart_show_cross_sell = get_theme_mod( 'shop_cart_show_cross_sell', 1 );
	if( $shop_cart_show_cross_sell && 'layout1' !== $quick_view_layout ) {
		//Quick view popup
		add_action( 'wp_body_open', 'emoza_quick_view_popup' );
	}

	// Add to cart button text
	add_filter( 'woocommerce_product_add_to_cart_text', 'emoza_add_to_cart_text', 10, 2);
}
add_action( 'wp', 'emoza_product_card_hooks' );

/**
 * Loop add to cart
 */
function emoza_filter_loop_add_to_cart( $button, $product, $args ) {
	global $product;

	$button_layout  = get_theme_mod( 'shop_product_add_to_cart_layout', 'layout3' );

	$args['attributes'] = array(
		'title' => $product->add_to_cart_text(),
	);

	if ( 'layout4' !== $button_layout ) {
		$button = str_replace( 'href=', 'title="' . $product->add_to_cart_description() . '" href=', $button );

		return $button;
	}

	if ( $product->is_type( 'simple' ) ) {
		$text = '<i class="ws-svg-icon">' . emoza_get_svg_icon( 'icon-cart', false ) . '</i>';
	} else {
		$text = '<i class="ws-svg-icon">' . emoza_get_svg_icon( 'icon-eye', false ) . '</i>';
	}

	$button = sprintf(
		'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
		esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
		isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
		$text
	);

	return $button;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'emoza_filter_loop_add_to_cart', 10, 3 );

/**
 * Check if page has woocommece GB blocks
 */
function emoza_page_has_woo_blocks() {
	global $post;

	if( $post ) {
		if( isset( $post->post_content ) && strpos( $post->post_content, 'woocommerce/' ) ) {
            return true;
        }
	}

	return false;
}

/**
 * Check if page has woocommece shortcode
 */
function emoza_page_has_woo_shortcode() {
	global $post;

	$shortcodes = array(
		'products',
		'product_page',
	);

	if( $post ) {
		if( isset( $post->post_content ) ) { 
			foreach( $shortcodes as $shortcode ) {
                if( has_shortcode( $post->post_content, $shortcode ) ) {
                    return true;
                }
            }
        }
	}

	return false;
}

/**
 * Wrap loop button
 */
function emoza_wrap_loop_button_start() {

	$loop_button_wrap_classes = array( 'loop-button-wrap' );
	$button_layout            = get_theme_mod( 'shop_product_add_to_cart_layout', 'layout3' );
	
	$loop_button_wrap_classes[] = 'button-' . $button_layout;
	$loop_button_wrap_classes[] = get_theme_mod( 'shop_product_add_to_cart_button_width', 'auto' ) === 'auto' ? 'button-width-auto' : 'button-width-full';

	/**
	 * Hook 'emoza_loop_button_wrap_classes'
	 *
	 * @since 1.0.0
	 */
	echo '<div class="'. esc_attr( implode( ' ', apply_filters( 'emoza_loop_button_wrap_classes', $loop_button_wrap_classes ) ) ) .'">';
}

/**
 * Loop product structure
 */
function emoza_loop_product_structure() {
	$elements   = get_theme_mod( 'shop_card_elements', emoza_get_default_shop_archive_card_elements() );
	$layout     = get_theme_mod( 'shop_product_card_layout', 'layout1' );

	/**
	 * Hook 'emoza_loop_product_elements'
	 *
	 * @since 1.0.0
	 */
	$elements = array_merge( $elements, apply_filters( 'emoza_loop_product_elements', array() ) );

	if ( 'layout1' === $layout ) {
		foreach ( $elements as $element ) {
			if( function_exists( $element ) ) {
				call_user_func( $element );
			}
		}
	} else {
		$left_elements = array_diff( $elements, array( 'woocommerce_template_loop_price', 'emoza_loop_product_description' ) );

		echo '<div class="row">';
			echo '<div class="col-md-7">';
			foreach ( $left_elements as $element ) {
				if( function_exists( $element ) ) {
					call_user_func( $element );
				}
			}       
			echo '</div>';
			echo '<div class="col-md-5 loop-price-inline text-sm-left">';
				if( in_array( 'woocommerce_template_loop_price', $elements ) ) {
					woocommerce_template_loop_price();
				}
			echo '</div>';
			echo '<div class="col-12 product-description-column">';
				if( in_array( 'emoza_loop_product_description', $elements ) ) {
					emoza_loop_product_description();
				}
			echo '</div>';
		echo '</div>';
	}
}

/**
 * Add to cart button text
 */
function emoza_add_to_cart_text( $text, $product ) {
	$out_of_stock_text = get_theme_mod( 'out_of_stock_text', '' );

	if( $out_of_stock_text && 'outofstock' === $product->get_stock_status() ) {
		$text = $out_of_stock_text;
	}

	return $text;
}

/**
 * Product Equal Height
 */
// Add class to emoza content class to flag the equal height in the product loop
function emoza_equal_height_content_class( $content_class ) {
	$layout             = get_theme_mod( 'shop_archive_layout', 'product-grid' );   
	$button_layout      = get_theme_mod( 'shop_product_add_to_cart_layout', 'layout3' );
	$equal_height       = get_theme_mod( 'shop_product_equal_height', 0 );
	$equal_height_class = ( ! empty( $equal_height ) && $button_layout === 'layout2' && $layout === 'product-grid' ) ? ' product-equal-height' : '';

	return $content_class . $equal_height_class;
}
add_action( 'wp', function() {
	if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() || emoza_post_content_has_woo_shortcode() || emoza_post_content_has_woo_blocks() ) {
		add_filter( 'emoza_content_class', 'emoza_equal_height_content_class' );
	}
} );

/**
 * Pass through all the legacy WooCommerce shortcodes to handle attributes
 */
$emoza_legacy_woo_shortcodes = array(
	'products',
	'recent_products',
	'sale_products',
	'best_selling_products',
	'top_rated_products',
	'featured_products',
	'related_products',
);

foreach( $emoza_legacy_woo_shortcodes as $emoza_legacy_woo_shortcode ) {
	add_filter( "shortcode_atts_{$emoza_legacy_woo_shortcode}", function( $atts ){

		// Always product grid layout
		$atts[ 'class' ] = 'product-grid';

		// Proudct Equal Height
		$shop_product_equal_height = get_theme_mod( 'shop_product_equal_height', 0 );
		if( $shop_product_equal_height ) {
			$atts[ 'class' ] = 'product-equal-height';
		}
		
		return $atts;
	} );
}

/**
 * Change products cards heading tag from h2 (default) to h3
 * To have a better SEO structure
 * 
 */
function emoza_product_card_title_output( $title, $loop_post ) {
	if( ! is_singular( 'product' ) && ! is_404() ) {
		return $title;
	}

	$early = true;

	// 404
	if( is_404() ) {
		$early = false;
	}

	// Related, Upsell and Recently Viewed Products
	if( ( get_theme_mod( 'single_related_products', 1 ) || get_theme_mod( 'single_upsell_products', 1 ) || get_theme_mod( 'single_recently_viewed_products', 0 ) ) && ! is_404() ) {
		$early = false;
	}

	// Return early
	if( $early ) {
		return $title;
	}

	ob_start();
	the_title( '<h3 class="woocommerce-loop-product__title"><a class="emoza-wc-loop-product__title" href="'. esc_url( get_the_permalink( $loop_post->ID ) ) .'">', '</a></h3>' );
	$the_title = ob_get_clean();

	return $the_title;
}
add_filter( 'emoza_shop_loop_product_title', 'emoza_product_card_title_output', 10, 2 );

/**
 * Add rating count to product grid.
 * 
 * @param string $html
 * @param float $rating
 * 
 * @return string
 */
function emoza_products_grid_rating_html( $html, $rating, $count ) {
	global $product;

	if ( ! $product ) {
		return $html;
	}

	$display_rating_count = get_theme_mod( 'shop_product_display_reviews_count', 0 );
	if ( ! $display_rating_count ) {
		return $html;
	}

	$is_inside_product_grid = did_action( 'woocommerce_after_single_product_summary' );
	if ( is_singular( 'product' ) && ! $is_inside_product_grid ) {
		return $html;
	}

	$review_count = $product->get_review_count();
	if ( $review_count === 0 ) {
		return $html;
	}
	
	ob_start();
	?>
	
	<a href="<?php echo esc_url( get_permalink( $product->get_id() ) ); ?>#reviews" class="woocommerce-review-link" rel="nofollow">(<span class="count"><?php echo esc_html( $review_count ); ?></span>)</a>

	<?php
	$html .= ob_get_clean();

	return '<div class="woocommerce-product-rating emoza-wc-product-rating-with-count">' . $html . '</div>';
}
add_filter( 'woocommerce_product_get_rating_html', 'emoza_products_grid_rating_html', 10, 3 );

/**
 * Add rating count to product grid on WooCommerce Blocks.
 * 
 * @param string $html
 * @param object $data
 * @param object $product
 * 
 * @return string
 */
function emoza_wc_blocks_products_grid_rating_html( $html, $data, $product ) {
	$review_count = $product->get_review_count();
	if ( $review_count === 0 ) {
		return $html;
	}

	ob_start();
	?>
	
	<a href="<?php echo esc_url( get_permalink( $product->get_id() ) ); ?>#reviews" class="woocommerce-review-link" rel="nofollow">(<span class="count"><?php echo esc_html( $review_count ); ?></span>)</a>

	<?php
	$rating_count_html= ob_get_clean();

	$data->rating = str_replace( 
		array( '"wc-block-grid__product-rating"', '</div></div>' ), 
		array( '"wc-block-grid__product-rating emoza-wc-product-rating-with-count"', '</div>' . $rating_count_html . '</div>' ), 
		$data->rating 
	);

	return $html;
}
add_filter( 'woocommerce_blocks_product_grid_item_html', 'emoza_wc_blocks_products_grid_rating_html', 10, 3 );
