<?php
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://wondercode.pro/
 * @since      1.0.0
 *
 * @package    Awesome_Designs_For_Woocommerce
 * @subpackage Awesome_Designs_For_Woocommerce/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Awesome_Designs_For_Woocommerce
 * @subpackage Awesome_Designs_For_Woocommerce/public
 * @author     Mcurly <contact@wondercode.pro>
 */
class Awesome_Designs_For_Woocommerce_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * The version of the design.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of the design.
	 */
	private $adfw_design;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 * @param      integer   $adfp_design    The default design.
	 */
	public function __construct( $plugin_name, $version, $adfw_design ) {

		$this->plugin_name 	= $plugin_name;
		$this->version 		= $version;
		$this->adfw_design 	= $adfw_design;

		// Custom function if design is chosen
		if( $this->adfw_design ) {
			require_once( dirname( ( __FILE__ ) ) . '/partials/awesome-designs-for-woocommerce-public-display-'. intval($this->adfw_design) .'.php' );
		}

	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		// Check if it's a woocommerce page to include CSS & Fonts
		if( 
			is_woocommerce() || 
			is_cart() || 
			is_checkout() ||
			is_account_page() ||
			is_wc_endpoint_url() || 
			$this->contains_woocommerce_shortcode() 
		) {

			wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/' . $this->adfw_design . '/style.css', array(), $this->version, 'all' );

			// Preconnect to Google Fonts
			add_action('wp_head', function() {
				echo '<link rel="preconnect" href="https://fonts.googleapis.com">';
				echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>';
			}, 5 );

			// Includes Fonts
			switch ($this->adfw_design) {
				case 1:
					wp_enqueue_style('google-fonts-dm-sans', 'https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap', array(), $this->version, 'all');
					wp_enqueue_style('google-fonts-nunito-sans', 'https://fonts.googleapis.com/css2?family=Nunito+Sans:ital,opsz,wght@0,6..12,200..1000;1,6..12,200..1000&display=swap', array(), $this->version, 'all');
					break;
				case 2:
					wp_enqueue_style('google-fonts-lato', 'https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap', array(), $this->version, 'all');
					wp_enqueue_style('google-fonts-work-sans', 'https://fonts.googleapis.com/css2?family=Work+Sans:ital,wght@0,100..900;1,100..900&display=swap', array(), $this->version, 'all');
					break;
				case 3:
					wp_enqueue_style('google-fonts-fraunces', 'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,100..900;1,9..144,100..900&display=swap', array(), $this->version, 'all');
					wp_enqueue_style('google-fonts-inter', 'https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap', array(), $this->version, 'all');
					break;
				default:
					break;
			}

		}

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/awesome-designs-for-woocommerce-public.js', array( 'jquery' ), $this->version, false );

	}


	/**
         * Filter the woocommerce page template to use plugin template instead of the default
         *
	 * @since    1.0.0
	 */
	public function locate_template( $template, $template_name, $template_path ) {

		$basename = basename( $template );

		if( $basename == 'product-image.php' ) {
			$template = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/' . $this->adfw_design . '/product-image.php';
		} else {}

		return $template;
	}


	/**
         * Change number of products per line
         *
	 * @since    1.0.0
	 */
	public function change_loop_columns() {
		return 4;
	}

	/**
         * Add new classes into body tag
         *
	 * @since    1.0.0
	 */
	public function add_body_classes() {
		$classes[] = 'awesome-designs-woocommerce adfw-' . intval( $this->adfw_design );

    	return $classes;
	}

	/**
         * Check if WooCommerce shortcode into content
         *
	 * @since    1.0.0
	 */
	public function contains_woocommerce_shortcode() {
		if ( is_singular() ) {
			global $post;
			$shortcodes = array(
				'woocommerce_cart',
				'woocommerce_checkout',
				'woocommerce_my_account',
				'woocommerce_order_tracking',
				'products',
				'product_page',
				'product_category',
				'product_categories',
				'add_to_cart',
				'shop_messages',
				'related_products',
			);
	
			foreach ( $shortcodes as $shortcode ) {
				if ( has_shortcode( $post->post_content, $shortcode ) ) {
					return true;
				}
			}
		}

		return false;
	}
	
}
