<?php

/**
 * The core plugin class that defines internationalization, admin-specific hooks, and public-facing hooks.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 */

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

/**
 * NudgeSells
 *
 * Loads the plugin's dependencies and defines every hook used by the admin area and the public-facing storefront.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells
{

	protected $loader;

	/**
	 * Construct.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function __construct()
	{
		$this->load_dependencies();
		$this->loader = new NudgeSells_Loader();
	}

	/**
	 * Load dependencies.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function load_dependencies()
	{

		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-loader.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-activator.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-cpt.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-multilingual.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-currency.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-offer-repository.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-rules-repository.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-pricing-engine.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-order-compat.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-analytics-data.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-pro-loader.php';

		require_once NUDGESELLS_PLUGIN_DIR . 'admin/class-nudgesells-admin.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'admin/class-nudgesells-admin-offer.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'admin/class-nudgesells-admin-dashboard.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'admin/class-nudgesells-admin-analytics.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'admin/class-nudgesells-admin-settings.php';

		require_once NUDGESELLS_PLUGIN_DIR . 'public/class-nudgesells-public.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-cart-pricing.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-ajax.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-native-add-to-cart.php';
		require_once NUDGESELLS_PLUGIN_DIR . 'includes/class-nudgesells-order-tracking.php';
	}

	/**
	 * Define common hooks.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function define_common_hooks()
	{
		$cpt = new NudgeSells_CPT();
		$this->loader->add_action('init', $cpt, 'register');
		$this->loader->add_filter('manage_' . NudgeSells_CPT::POST_TYPE . '_posts_columns', $cpt, 'columns');
		$this->loader->add_action('manage_' . NudgeSells_CPT::POST_TYPE . '_posts_custom_column', $cpt, 'render_column', 10, 2);
		$this->loader->add_filter('post_row_actions', $cpt, 'row_actions', 10, 2);

		$multilingual = new NudgeSells_Multilingual();
		$this->loader->add_filter('pll_get_post_types', $multilingual, 'register_polylang_post_type', 10, 2);
	}

	/**
	 * Define admin hooks.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function define_admin_hooks()
	{
		$admin_menu = new NudgeSells_Admin();
		$this->loader->add_action('admin_menu', $admin_menu, 'register_menu');
		$this->loader->add_action('admin_enqueue_scripts', $admin_menu, 'enqueue_assets');
		$this->loader->add_filter('parent_file', $admin_menu, 'fix_parent_file');
		$this->loader->add_filter('submenu_file', $admin_menu, 'fix_submenu_file');
		$this->loader->add_action('admin_notices', $admin_menu, 'render_cpt_screen_header');
		$this->loader->add_action('admin_notices', $admin_menu, 'render_welcome_notice');
		$this->loader->add_filter('plugin_row_meta', $admin_menu, 'plugin_listing_links', 10, 4);
		$this->loader->add_filter('admin_footer_text', $admin_menu, 'edit_admin_footer_text', 11);
		$this->loader->add_filter('update_footer', $admin_menu, 'edit_admin_footer_version', 11);

		$admin_offer = new NudgeSells_Admin_Offer();
		$this->loader->add_action('add_meta_boxes', $admin_offer, 'add_metaboxes');
		$this->loader->add_action('save_post_' . NudgeSells_CPT::POST_TYPE, $admin_offer, 'save');
		$this->loader->add_action('admin_enqueue_scripts', $admin_offer, 'assets');
		$this->loader->add_filter('enter_title_here', $admin_offer, 'title_placeholder');
		$this->loader->add_action('wp_ajax_nudgesells_search_product_categories', $admin_offer, 'ajax_search_terms_categories');
		$this->loader->add_action('wp_ajax_nudgesells_search_product_tags', $admin_offer, 'ajax_search_terms_tags');
		$this->loader->add_action('admin_post_nudgesells_duplicate_offer', $admin_offer, 'duplicate_offer');
		$this->loader->add_action('admin_notices', $admin_offer, 'duplicated_notice');

		$admin_settings = new NudgeSells_Admin_Settings();
		$this->loader->add_action('admin_init', $admin_settings, 'register_settings');
	}

	/**
	 * Define public hooks.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	private function define_public_hooks()
	{
		$public = new NudgeSells_Public();
		$this->loader->add_action('wp_enqueue_scripts', $public, 'assets');
		$this->loader->add_action('woocommerce_before_add_to_cart_button', $public, 'render', 20);

		$cart_pricing = new NudgeSells_Cart_Pricing();
		$this->loader->add_action('woocommerce_before_calculate_totals', $cart_pricing, 'apply_prices', 20);
		$this->loader->add_filter('woocommerce_cart_item_price', $cart_pricing, 'filter_cart_item_price', 10, 3);
		$this->loader->add_filter('woocommerce_cart_item_name', $cart_pricing, 'flag_addon_name', 10, 3);
		$this->loader->add_filter('woocommerce_cart_item_class', $cart_pricing, 'cart_item_class', 10, 3);
		$this->loader->add_filter('woocommerce_get_item_data', $cart_pricing, 'add_savings_item_data', 10, 2);
		$this->loader->add_action('woocommerce_cart_item_removed', $cart_pricing, 'cascade_remove_orphans', 10, 2);

		$ajax = new NudgeSells_Ajax();
		$this->loader->add_action('wc_ajax_nudgesells_add_to_cart', $ajax, 'add_to_cart');
		$this->loader->add_action('wc_ajax_nopriv_nudgesells_add_to_cart', $ajax, 'add_to_cart');
		$this->loader->add_action('wc_ajax_nudgesells_price_preview', $ajax, 'price_preview');
		$this->loader->add_action('wc_ajax_nopriv_nudgesells_price_preview', $ajax, 'price_preview');
		$this->loader->add_action('wc_ajax_nudgesells_render_widget', $ajax, 'render_widget');
		$this->loader->add_action('wc_ajax_nopriv_nudgesells_render_widget', $ajax, 'render_widget');

		$native_add_to_cart = new NudgeSells_Native_Add_To_Cart();
		$this->loader->add_action('woocommerce_add_to_cart', $native_add_to_cart, 'handle', 10, 6);

		$order_tracking = new NudgeSells_Order_Tracking();
		$this->loader->add_action('woocommerce_checkout_create_order_line_item', $order_tracking, 'stamp_line_item', 10, 4);
		$this->loader->add_filter('woocommerce_order_item_name', $order_tracking, 'flag_order_item_name', 10, 2);
		$this->loader->add_filter('woocommerce_hidden_order_itemmeta', $order_tracking, 'hide_order_item_meta', 10, 1);
	}

	/**
	 * Woocommerce missing notice.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function woocommerce_missing_notice()
	{
		if (class_exists('WooCommerce')) {
			return;
		}
		echo '<div class="notice notice-error"><p>' .
			esc_html__('NudgeSells requires WooCommerce to be installed and active.', 'nudgesells') .
			'</p></div>';
	}

	/**
	 * Run.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function run()
	{
		$this->loader->add_action('admin_notices', $this, 'woocommerce_missing_notice');

		if (class_exists('WooCommerce')) {
			$this->define_common_hooks();
			$this->define_admin_hooks();
			$this->define_public_hooks();
			NudgeSells_Pro_Loader::load($this->loader);
		}

		$this->loader->run();
	}
}
