<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://wondercode.pro/
 * @since      1.0.0
 *
 * @package    Awesome_Designs_For_Woocommerce
 * @subpackage Awesome_Designs_For_Woocommerce/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Awesome_Designs_For_Woocommerce
 * @subpackage Awesome_Designs_For_Woocommerce/includes
 * @author     Mcurly <contact@wondercode.pro>
 */


class Awesome_Designs_For_Woocommerce {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Awesome_Designs_For_Woocommerce_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

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

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

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {

		if ( defined( 'AWESOME_DESIGNS_FOR_WOOCOMMERCE_VERSION' ) ) {
			$this->version = AWESOME_DESIGNS_FOR_WOOCOMMERCE_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'awesome-designs-for-woocommerce';

		$options_settings  = get_option( 'Awesome_designs_for_woocommerce_main_settings' );

		if( $options_settings ) {
			$this->adfw_design = (int)$options_settings['design'];
		} else {
			$this->adfw_design = false;
		}
		

		$this->load_dependencies();
		$this->set_locale();

		$this->define_admin_hooks();
		$this->define_public_hooks();
		$this->define_common_hooks();

	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Awesome_Designs_For_Woocommerce_Loader. Orchestrates the hooks of the plugin.
	 * - Awesome_Designs_For_Woocommerce_i18n. Defines internationalization functionality.
	 * - Awesome_Designs_For_Woocommerce_Admin. Defines all hooks for the admin area.
	 * - Awesome_Designs_For_Woocommerce_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-awesome-designs-for-woocommerce-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-awesome-designs-for-woocommerce-i18n.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-awesome-designs-for-woocommerce-admin.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-awesome-designs-for-woocommerce-public.php';

		$this->loader = new Awesome_Designs_For_Woocommerce_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Awesome_Designs_For_Woocommerce_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {

		$plugin_i18n = new Awesome_Designs_For_Woocommerce_i18n();

		$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );

	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {

		$plugin_admin = new Awesome_Designs_For_Woocommerce_Admin( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
		
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_menu' );

	}

	/**
	 * Register all of the hooks common to public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_public_hooks() {

		$plugin_public = new Awesome_Designs_For_Woocommerce_Public( $this->get_plugin_name(), $this->get_version(), $this->get_adfw_design() );

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

		// Change Woocommerce template 
		$this->loader->add_filter( 'woocommerce_locate_template', $plugin_public, 'locate_template', 10, 3 );

		// Changer number of products per line
		$this->loader->add_filter( 'loop_shop_columns', $plugin_public, 'change_loop_columns', 10, 3 );

		// Add new classes into body
		// Changer number of products per line
		$this->loader->add_filter( 'body_class', $plugin_public, 'add_body_classes', 10, 3 );

	}

	
	/**
	 * Register all of the hooks related to the public-facing & admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_common_hooks() {

		if( (int)$this->adfw_design == '2' ) {
			$this->loader->add_action( 'after_setup_theme', $this, 'adfw_image_sizes' );
			$this->loader->add_filter( 'image_downsize', $this, 'adfw_generate_custom_image_size_on_the_fly', 10, 3 );
		}

	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Awesome_Designs_For_Woocommerce_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}

	/**
	 * Retrieve the default design
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the design.
	 */
	public function get_adfw_design() {
		return $this->adfw_design;
	}

	/**
	 * Add custom image sizes
	 *
	 * @since     1.0.0
	 * @return    void
	 */
	public function adfw_image_sizes() {
		add_image_size('adfw-vertical-1', 342, 445, true);
	}

	/**
	 * Generate custom image size on the fly
	 *
	 * @since     1.0.0
	 * @param     mixed  $out   False if no image found, array of image URL and dimensions otherwise.
	 * @param     int    $id    Attachment ID.
	 * @param     mixed  $size  Requested size.
	 * @return    mixed         False if no image found, array of image URL and dimensions otherwise.
	 */
	public function adfw_generate_custom_image_size_on_the_fly($out, $id, $size) {
		// Define custom sizes here
		$custom_sizes = array(
			'adfw-vertical-1' => array(342, 445, true),
		);

		// Check if the size is a string and if it is in the custom sizes
		if (!is_string($size) || !isset($custom_sizes[$size])) {
			return false;
		}

		// Retrieve image metadata
		$meta = wp_get_attachment_metadata($id);
		if (!$meta || !is_array($meta)) {
			return false;
		}

		// Check if the size already exists
		if (isset($meta['sizes'][$size])) {
			$file = $meta['sizes'][$size]['file'];
			$url = wp_get_attachment_url($id);
			$url = str_replace(basename($url), $file, $url);
			$out = array($url, $custom_sizes[$size][0], $custom_sizes[$size][1], true);
			return $out;
		}

		// Generate the image if it does not exist
		$file_path = get_attached_file($id);
		$resized = image_make_intermediate_size($file_path, $custom_sizes[$size][0], $custom_sizes[$size][1], $custom_sizes[$size][2]);

		if (!$resized) {
			return false;
		}

		// Update image metadata
		$meta['sizes'][$size] = $resized;
		wp_update_attachment_metadata($id, $meta);

		$url = wp_get_attachment_url($id);
		$url = str_replace(basename($url), $resized['file'], $url);
		$out = array($url, $resized['width'], $resized['height'], true);

		return $out;
	}

}