<?php

namespace Codexpert\WC_Affiliate;

use Codexpert\Plugin\Widget;
use Codexpert\Plugin\Notice;
use Pluggable\Marketing\Survey;
use Pluggable\Marketing\Feature;
use Pluggable\Marketing\Deactivator;

/**
 * if accessed directly, exit.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main class for the plugin
 *
 * @package Plugin
 * @author codexpert <hello@codexpert.io>
 */
final class Plugin {

	public static $_instance;

	public $plugin;

	public function __construct() {
		$this->include();
		$this->define();
		$this->hook();
	}

	/**
	 * Includes files
	 */
	public function include() {
		require_once __DIR__ . '/vendor/autoload.php';
		require_once __DIR__ . '/inc/functions.php';
	}

	/**
	 * Define variables and constants
	 */
	public function define() {
		define( 'WCAFFILIATE', __FILE__ );
		define( 'WCAFFILIATE_DIR', dirname( WCAFFILIATE ) );
		define( 'WOOLEMENTOR_ASSETS', plugins_url( 'assets', WCAFFILIATE ) );
		define( 'WCAFFILIATE_DEBUG', apply_filters( 'wc-affiliate_debug', true ) );

		// plugin data.
		$this->plugin               = array();
		$this->plugin['basename']   = plugin_basename( WCAFFILIATE );
		$this->plugin['file']       = WCAFFILIATE;
		$this->plugin['TextDomain'] = 'wc-affiliate';
		$this->plugin['Name']       = 'WC Affiliate';
		$this->plugin['Version']    = '2.17';
		$this->plugin['server']     = apply_filters( 'wc-affiliate_server', 'https://my.pluggable.io' );
		$this->plugin['min_php']    = '7.4';
		$this->plugin['min_wp']     = '5.0';
		$this->plugin['doc_id']     = 11009;

		$platform = wc_get_platform();

		if ( $platform === 'easycommerce' ) {
			$this->plugin['depends'] = array( 'easycommerce/easycommerce.php' => 'Easycommerce' );
		} else {
			$this->plugin['depends'] = array( 'woocommerce/woocommerce.php' => 'WooCommerce' );
		}

		$this->plugin['hash_deactivator'] = '2b22ec78-14a5-4920-9a23-eba610850fe6';
		$this->plugin['hash_survey']      = '6baa92ba-2e82-47af-a5de-36681446267b';
		$this->plugin['hash_wizard']      = '5ae65e96-4523-4c70-9781-67fc7de918ba';
	}

	/**
	 * Hooks
	 */
	public function hook() {

		if ( is_admin() ) :

			/**
			 * Setup wizard facing hooks
			 *
			 * To add an action, use $admin->action()
			 * To apply a filter, use $admin->filter()
			 */
			$wizard = new Wizard( $this->plugin );
			$wizard->filter( "plugin_action_links_{$this->plugin['basename']}", 'action_links' );
			$wizard->action( 'init', 'render' );
			$wizard->action( 'admin_print_styles', 'enqueue_style' );

			/**
			 * Admin facing hooks
			 *
			 * To add an action, use $admin->action()
			 * To apply a filter, use $admin->filter()
			 */
			$admin = new Admin( $this->plugin );
			$admin->activate( 'install' );
			$admin->action( 'wc_affiliate_daily', 'daily' );
			$admin->action( 'wp_dashboard_setup', 'dashboard_widget', 99 );
			$admin->action( 'admin_enqueue_scripts', 'enqueue_scripts' );
			$admin->action( 'init', 'i18n' );
			$admin->filter( "plugin_action_links_{$this->plugin['basename']}", 'action_links' );
			$admin->filter( 'plugin_row_meta', 'plugin_row_meta', 10, 2 );
			$admin->action( 'admin_menu', 'add_menu' );
			$admin->filter( 'wc-affiliate-admin_charts', 'generate_charts_data' );
			$admin->action( 'edit_user_profile', 'show_commission_fields' );
			$admin->action( 'edit_user_profile_update', 'save_commission_fields' );
			$admin->action( 'show_user_profile', 'personal_show_commission_fields' );
			$admin->action( 'personal_options_update', 'personal_save_commission_fields' );
			$admin->action( 'woocommerce_product_options_general_product_data', 'show_product_comission_fields' );
			$admin->action( 'woocommerce_process_product_meta', 'save_product_comission_fields' );
			$admin->action( 'woocommerce_variation_options_pricing', 'show_variation_comission_fields', 10, 3 );
			$admin->action( 'woocommerce_save_product_variation', 'save_variation_comission_fields', 10, 2 );
			$admin->action( 'cx-plugin_tablenav', 'custom_table_data_filter', 10, 2 );
			$admin->action( 'cx-settings-after-title', 'custom_button' );
			$admin->action( 'admin_init', 'bulk_actions' );
			$admin->action( 'woocommerce_order_status_changed', 'update_referral_status', 10, 4 );
			$admin->action( 'wc-affiliate-after_chart', 'add_overlay', 10, 2 );
			$admin->action( 'woocommerce_subscription_renewal_payment_complete', 'reorder_subscription', 10, 2 );
			$admin->filter( 'wp_kses_allowed_html', 'allow_extra_html_tags' );
			// $admin->action( 'init', 'wc_affiliate_sale_notice', 9 );
			$admin->action( 'admin_init', 'restrict_admin_access_by_affiliates' );

			/**
			 * Settings related hooks
			 *
			 * To add an action, use $settings->action()
			 * To apply a filter, use $settings->filter()
			 */
			$settings = new Settings( $this->plugin );
			$settings->action( 'init', 'init_menu', -999 );
			$settings->action( 'admin_bar_menu', 'add_admin_bar', 70 );
			$settings->action( 'admin_init', 'redirect_get_pro' );

			// // Product related classes
			$widget      = new Widget( $this->plugin );
			$survey      = new Survey( $this->plugin );
			$notice      = new Notice( $this->plugin );
			// $feature	 = new Feature( $this->plugin, [ 'reserved' => [] ] );
			$deactivator = new Deactivator( $this->plugin );

		else : // !is_admin() ?

			/**
			 * Front facing hooks
			 *
			 * To add an action, use $front->action()
			 * To apply a filter, use $front->filter()
			 */
			$front = new Front( $this->plugin );
			$front->action( 'wp_head', 'head' );
			$front->action( 'wp_enqueue_scripts', 'enqueue_scripts' );
			$front->action( 'init', 'set_token' );
			$front->action( 'init', 'verify_email' );
			$front->action( 'woocommerce_thankyou', 'add_credit' );
			$front->action( 'easycommerce_after_order', 'add_credit_ec', 10, 3 );
			$front->action( 'pre_get_posts', 'alter_query_vars' );
			$front->filter( 'wc-affiliate-front_charts', 'generate_charts_data' );
			$front->filter( 'get_avatar_url', 'avatar_url', 10, 3 );
			$front->action( 'woocommerce_cart_totals_before_order_total', 'use_credit_to_pay' );
			$front->action( 'woocommerce_cart_calculate_fees', 'apply_credit_pay' );
			// $front->action( 'woocommerce_thankyou', 'pay_with_credit_transaction' );
			$front->filter( 'wc-affiliate-dashboard_navigation', 'remove_banner_tab' );
			$front->action( 'wp_footer', 'loader_html' );
			$front->action( 'init', 'hide_admin_bar_for_affiliates' );

			/**
			 * Shortcode hooks
			 *
			 * To enable a shortcode, use $shortcode->register()
			 */
			$shortcode = new Shortcode( $this->plugin );
			$shortcode->register( 'wc-affiliate-dashboard', 'dashboard' );
			$shortcode->register( 'wc-affiliate-application-form', 'application_form' );

		endif;

		/**
		 * Template hooks
		 *
		 * To add an action, use $template->action()
		 * To apply a filter, use $template->filter()
		 */
		$template = new Template( $this->plugin );
		$template->action( 'wc-affiliate-dashboard', 'dashboard' );
		$template->action( 'wc-affiliate-dashboard-profile', 'profile' );
		$template->action( 'wc-affiliate-dashboard-navigation', 'navigation' );
		$template->action( 'wc-affiliate-dashboard-content', 'tab_content' );
		$template->action( 'wc-affiliate-dashboard-report', 'report' );
		$template->action( 'wc-affiliate-review-user', 'review_user' );
		$template->action( 'wc-affiliate-transaction-form', 'transaction_form' );
		$template->action( 'wc-affiliate-new-affiliate', 'new_affiliate_form' );

		/**
		 * AJAX facing hooks
		 *
		 * To add a hook for logged in users, use $ajax->priv()
		 * To add a hook for non-logged in users, use $ajax->nopriv()
		 */
		$ajax = new AJAX( $this->plugin );
		if ( ! Helper::has_pro() ) :
			$ajax->all( 'wf-register-user', 'apply' );
		endif;
		$ajax->priv( 'wf-remove-affiliate', 'remove_affiliate' );
		$ajax->priv( 'wf-update-user', 'update_user' );
		$ajax->priv( 'wf-request-payout', 'request_payout' );
		$ajax->priv( 'wf-transaction-form-action', 'transaction_form_action' );
		$ajax->priv( 'wf-review-action', 'review_action' );
		$ajax->priv( 'wf-url-generator', 'generate_url' );
		$ajax->priv( 'wf-export-report', 'export_csv_report' );
		$ajax->priv( 'wf-payout', 'payout' );
		$ajax->priv( 'wf-export-table-report', 'export_table_report' );
		// $ajax->priv( 'wf-pay-with-credit', 'pay_with_credit' );
		$ajax->priv( 'wca-resend-varify-url', 'resend_varify_url' );
		$ajax->priv( 'wf-export-all', 'export_all_data' );
		$ajax->priv( 'wca-import-data', 'import_data' );
		$ajax->all( 'wc_affiliate_dismiss_notice', 'wc_affiliate_dismiss_callback' );
		$ajax->all( 'assign_affiliate', 'handle_affiliation_assignment' );

		/**
		 * Email hooks
		 *
		 * To add an action, use $email->action()
		 * To apply a filter, use $email->filter()
		 */
		$email = new Email( $this->plugin );
		$email->action( 'wc-affiliate-affiliate_applied', 'affiliate_applied_affiliate' );
		$email->action( 'wc-affiliate-affiliate_applied', 'affiliate_applied_admin' );
		$email->action( 'wc-affiliate-account_reviewed', 'account_review_affiliate', 10, 3 );
		$email->action( 'wc-affiliate-request_payout', 'request_payout_affiliate', 10, 2 );
		$email->action( 'wc-affiliate-request_payout', 'request_payout_admin', 10, 2 );
		$email->action( 'wc-affiliate-add_credit', 'add_credit_affiliate', 10, 2 );
		$email->action( 'wc-affiliate-add_credit', 'add_credit_admin', 10, 2 );
		$email->action( 'wc-affiliate-marked_paid', 'payout_processed' );
		$email->filter( 'wc-affiliate-resend_varify_email', 'resend_varify_url', 10, 2 );

		/**
		 * Common hooks
		 *
		 * Executes on both the admin area and front area
		 */
		$common = new Common( $this->plugin );
		$common->action( 'ajax_query_attachments_args', 'show_current_user_attachments' );
	}

	/**
	 * Cloning is forbidden.
	 */
	public function __clone() { }

	/**
	 * Unserializing instances of this class is forbidden.
	 */
	public function __wakeup() { }

	/**
	 * Instantiate the plugin
	 */
	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
}

Plugin::instance();
