<?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       #
 * @since      1.0.0
 *
 * @package    Esraw
 * @subpackage Esraw/includes
 */

namespace EasyShippingRate;

use EasyShippingRate\Admin\Admin;
use EasyShippingRate\Frontend\Frontend;

/**
 * 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    Esraw
 * @subpackage Esraw/includes
 */
class Plugin {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      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;

	/**
	 * 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( 'ESRAW_VERSION' ) ) {
			$this->version = ESRAW_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'esraw';

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Loader. Orchestrates the hooks of the plugin.
	 * - I18n. Defines internationalization functionality.
	 * - Admin\Admin. Defines all hooks for the admin area.
	 * - Frontend\Frontend. Defines all hooks for the public side of the site.
	 *
	 * Classes are loaded through Composer's PSR-4 autoloader, so no manual
	 * require_once calls are needed here.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		$this->loader = new Loader();
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the 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 I18n();

		$this->loader->add_action( 'init', $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 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( 'woocommerce_shipping_init', $plugin_admin, 'init_shipping_method' );
		$this->loader->add_filter( 'woocommerce_shipping_methods', $plugin_admin, 'add_easy_rate_shipping_method' );
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_admin_menu' );
		$this->loader->add_action( 'woocommerce_shipping_zone_method_deleted', $plugin_admin, 'delete_shipping_conditions', 10, 1 );
		$this->loader->add_action( 'admin_notices', $plugin_admin, 'maybe_show_delete_blocked_notice' );
		// On the plugin's own screens, notices from other plugins are hidden so only ours show.
		$this->loader->add_action( 'in_admin_header', $plugin_admin, 'remove_foreign_admin_notices', 100 );

		// Review prompt shown on every plugin screen until snoozed via "No thanks".
		$this->loader->add_action( 'admin_init', $plugin_admin, 'handle_review_prompt_actions' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_notice_styles' );
		$this->loader->add_action( 'admin_notices', $plugin_admin, 'maybe_show_review_notice' );
	}

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

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

		$this->loader->add_action( 'woocommerce_after_shipping_rate', $plugin_public, 'display_shipping_description_on_cart', 10, 2 );
		$this->loader->add_filter( 'woocommerce_package_rates', $plugin_public, 'hide_shipping_when_free_is_available', 10, 2 );
		$this->loader->add_filter( 'woocommerce_shipping_chosen_method', $plugin_public, 'set_default_shipping_method', 99, 2 );
		// Classic cart/checkout templates.
		$this->loader->add_action( 'woocommerce_before_cart_table', $plugin_public, 'display_free_shipping_notification' );
		$this->loader->add_action( 'woocommerce_before_checkout_form', $plugin_public, 'display_free_shipping_notification' );
		// Gutenberg WooCommerce Cart block — classic hooks never fire there.
		$this->loader->add_filter( 'render_block', $plugin_public, 'inject_notification_in_cart_block', 10, 2 );

		// Per-method debug report (administrators only).
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_debug_assets' );
		$this->loader->add_filter( 'woocommerce_cart_shipping_packages', $plugin_public, 'refresh_shipping_for_debug' );
		$this->loader->add_action( 'woocommerce_before_cart_table', $plugin_public, 'display_debug_report' );
		$this->loader->add_action( 'woocommerce_before_checkout_form', $plugin_public, 'display_debug_report' );
		$this->loader->add_filter( 'render_block', $plugin_public, 'inject_debug_in_cart_block', 10, 2 );
	}

	/**
	 * 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    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;
	}
}
