<?php

/**
 * WooCommerce Shop Class.
 *
 * @package Kubelio\WooCommerce
 * @since 1.0.0
 */

namespace Kubelio\WooCommerce;

if (! defined('ABSPATH')) {
	exit;
}

/**
 * Shop Class
 */
class Shop {

	/**
	 * Constructor.
	 */
	public function __construct() {
		// Custom shop thumbnail & title
		if (get_theme_mod('shop_style', 'button-overlay') === 'button-overlay') {
			remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
		}

		remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
		add_action('woocommerce_before_shop_loop_item_title', array($this, 'custom_product_thumbnail'), 10);

		remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
		remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 10);

		remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
		add_action('woocommerce_shop_loop_item_title', array($this, 'loop_product_title'), 10);

		add_action('woocommerce_before_shop_loop_item_title', array($this, 'open_product_body'), 20);
		add_action('woocommerce_after_shop_loop_item', array($this, 'close_product_body'), 999);

		add_filter('woocommerce_post_class', array($this, 'product_classes'), 10, 2);
		add_filter('loop_shop_per_page', array($this, 'products_per_page'), 20);
		add_filter('loop_shop_columns', array($this, 'products_columns'), 20);

		add_action('woocommerce_before_shop_loop', array($this, 'before_shop_loop'), 30);
		remove_action('woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
		remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);

		// Remove default archive description
		remove_action('woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10);
		remove_action('woocommerce_archive_description', 'woocommerce_product_archive_description', 10);

		// Add shop filter to footer
		add_action('wp_footer', array($this, 'shop_filters'));
	}

	/**
	 * Custom product thumbnail with overlay actions.
	 */
	public function custom_product_thumbnail() {
		global $product;

		echo '<div class="rt-product__thumbnail rounded-md overflow-hidden">';
		echo '<a href="' . esc_url(get_permalink($product->get_id())) . '" class="rt-product__image-link">';
		echo woocommerce_get_product_thumbnail(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</a>';

		if (get_theme_mod('shop_style', 'button-overlay') === 'button-overlay') {
			echo '<div class="rt-product__action">';
			woocommerce_template_loop_add_to_cart();
			echo '</div>';
		}
		echo '</div>';
	}

	/**
	 * Added link to product title
	 */
	public function loop_product_title() {
		global $product;

		echo '<h3 class="rt-product__title ' . esc_attr(apply_filters('woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title')) . '">';
		echo '<a href="' . esc_url(get_permalink($product->get_id())) . '" class="rt-product__title-link has-heading-color">';
		echo esc_html(get_the_title());
		echo '</a>';
		echo '</h3>';
	}

	/**
	 * Add custom class CSS to shop products.
	 *
	 * @param array $classes Post classes.
	 * @param \WC_Product $product WC Product object.
	 * @return array
	 */
	public function product_classes($classes, $product) {
		$classes[] = 'rt-product';

		if (get_theme_mod('shop_style', 'button-overlay') === 'button-overlay') {
			$classes[] = 'is-style-action-overlay';
		}

		return $classes;
	}

	/**
	 * Open product body wrapper.
	 */
	public function open_product_body() {
		echo '<div class="rt-product__body">';
	}

	/**
	 * Close product body wrapper.
	 */
	public function close_product_body() {
		echo '</div>';
	}

	/**
	 * Products per page
	 *
	 * @param int $per_page Products per page.
	 * @return int
	 */
	public function products_per_page($per_page) {
		$custom_per_page = get_theme_mod('shop_per_page', 12);
		return ($custom_per_page !== '') ? (int) $custom_per_page : $per_page;
	}

	/**
	 * Products columns
	 *
	 * @param int $columns Products columns.
	 * @return int
	 */
	public function products_columns($columns) {
		$custom_columns = get_theme_mod('shop_columns', 4);
		return ($custom_columns !== '') ? (int) $custom_columns : $columns;
	}

	/**
	 * Added header top shop page
	 */
	public function before_shop_loop() {
		get_template_part('woocommerce/shop-header');
	}

	/**
	 * Add shop filter to footer
	 */
	public function shop_filters() {
		if (kubelio_is_woocommerce('shop') && in_array(get_theme_mod('shop_layout', 'wide'), array('wide', 'sidebar'), true)) {
			get_template_part('woocommerce/shop-filter');
		}
	}
}
