<?php
/**
 * Admin class file.
 *
 * @package WholesaleNode_For_WooCommerce
 */

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

/**
 * Handles admin menu, asset enqueuing, and page view callbacks.
 */
class WOOWSNFree_Admin {

	public function __construct() {
		add_action( 'admin_menu',            array( $this, 'register_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
	}

	/**
	 * Enqueue admin scripts.
	 *
	 * @param string $hook Current admin page hook suffix.
	 */
	public function enqueue_scripts( $hook ) {
		$screen = get_current_screen();
		if ( ! $screen || strpos( $screen->id, 'woowsn' ) === false ) {
			return;
		}

		$js_path = WOOWSN_FREE_DIR . 'assets/js/backend.js';
		if ( ! file_exists( $js_path ) ) {
			return;
		}

		global $wp_version;
		$script_args = true;
		if ( isset( $wp_version ) && version_compare( $wp_version, '6.3', '>=' ) ) {
			$script_args = array( 'in_footer' => true, 'strategy' => 'defer' );
		}

		wp_enqueue_script( 'wp-i18n' );

		wp_register_script(
			'woowsn-free-backend',
			WOOWSN_FREE_URL . 'assets/js/backend.js',
			array( 'jquery', 'wp-i18n' ),
			WOOWSN_FREE_VERSION,
			$script_args
		);
		wp_enqueue_script( 'woowsn-free-backend' );

		$localize = array(
			'api_nonce'        => wp_create_nonce( 'wp_rest' ),
			'root'             => esc_url_raw( rest_url( 'woowsn/v1/' ) ),
			'version'          => WOOWSN_FREE_VERSION,
			'pluginName'       => 'WholesaleNode for WooCommerce',
			'docsUrl'          => 'https://wholesalenode.com/documentation/',
			'pluginUrl'        => 'https://wholesalenode.com/',
			'supportEmail'     => 'info@wholesalenode.com',
			'adminUrl'         => admin_url( 'admin.php' ),
			'features'         => get_option( 'woowsn_free_features', array() ),
			'tools_url'        => admin_url( 'admin.php?page=woowsn_free_settings' ),
			'default_per_page' => 20,
			'is_free'          => true,
			'myaccount_url'    => wc_get_page_permalink( 'myaccount' ),
			'register_url'     => add_query_arg( 'action', 'register', wc_get_page_permalink( 'myaccount' ) ),
		);

		wp_localize_script( 'woowsn-free-backend', 'woowsn_object', $localize );

		wp_add_inline_script(
			'woowsn-free-backend',
			'window.woowsn_object = window.woowsn_object || ' . wp_json_encode( $localize ) . ';',
			'before'
		);

		wp_add_inline_script(
			'woowsn-free-backend',
			'(function(){var origFetch=window.fetch;window.fetch=function(url,opts){if(opts&&opts.method&&opts.method.toLowerCase()==="put"&&typeof url==="string"&&url.indexOf("/wp-json/")!==-1){opts.method="POST";}return origFetch.apply(this,arguments);};})();',
			'before'
		);

		if ( function_exists( 'wp_set_script_translations' ) ) {
			wp_set_script_translations( 'woowsn-free-backend', 'wholesalenode-for-woocommerce', WOOWSN_FREE_DIR . 'languages' );
		}
	}

	/**
	 * Enqueue admin styles.
	 *
	 * @param string $hook Current admin page hook suffix.
	 */
	public function enqueue_styles( $hook ) {
		$screen = get_current_screen();
		if ( ! $screen || strpos( $screen->id, 'woowsn' ) === false ) {
			return;
		}

		$css_path = WOOWSN_FREE_DIR . 'assets/css/backend.css';
		if ( ! file_exists( $css_path ) ) {
			return;
		}

		wp_enqueue_style(
			'woowsn-free-backend',
			WOOWSN_FREE_URL . 'assets/css/backend.css',
			array(),
			WOOWSN_FREE_VERSION
		);
	}

	/**
	 * Register admin menu and submenu pages.
	 */
	public function register_menu() {
		$cap = 'manage_woocommerce';

		add_menu_page(
			__( 'WholesaleNode', 'wholesalenode-for-woocommerce' ),
			__( 'WholesaleNode', 'wholesalenode-for-woocommerce' ),
			$cap,
			'woowsn_free_dashboard',
			array( $this, 'dashboard_ui' ),
			'dashicons-groups',
			56
		);

		$subpages = array(
			array( 'woowsn_free_dashboard',     __( 'Dashboard',      'wholesalenode-for-woocommerce' ), 'dashboard_ui' ),
			array( 'woowsn_free_registration',  __( 'Registration',   'wholesalenode-for-woocommerce' ), 'registration_ui' ),
			array( 'woowsn_free_user_groups',   __( 'User Groups',    'wholesalenode-for-woocommerce' ), 'user_groups_ui' ),
			array( 'woowsn_free_hide_price',    __( 'Hide Price',     'wholesalenode-for-woocommerce' ), 'hide_price_ui' ),
			array( 'woowsn_free_discount',      __( 'Discount Rules', 'wholesalenode-for-woocommerce' ), 'discount_ui' ),
			array( 'woowsn_free_product_lists', __( 'Product Lists',  'wholesalenode-for-woocommerce' ), 'product_lists_ui' ),
			array( 'woowsn_free_quote_entries', __( 'Quote Requests', 'wholesalenode-for-woocommerce' ), 'quote_entries_ui' ),
			array( 'woowsn_free_settings',      __( 'Settings',       'wholesalenode-for-woocommerce' ), 'settings_ui' ),
			array( 'woowsn_free_upgrade',       __( 'Upgrade to Pro', 'wholesalenode-for-woocommerce' ), 'upgrade_ui' ),
		);

		foreach ( $subpages as $page ) {
			add_submenu_page(
				'woowsn_free_dashboard',
				$page[1],
				$page[1],
				$cap,
				$page[0],
				array( $this, $page[2] )
			);
		}
	}

	/**
	 * Render a view file.
	 *
	 * @param string $view View filename without extension.
	 */
	private function render( $view ) {
		$file = WOOWSN_FREE_DIR . 'includes/views/' . $view . '.php';
		if ( file_exists( $file ) ) {
			include_once $file;
		}
	}

	public function dashboard_ui()     { $this->render( 'admin-dashboard' ); }
	public function registration_ui()  { $this->render( 'admin-registration' ); }
	public function user_groups_ui()   { $this->render( 'admin-user-groups' ); }
	public function hide_price_ui()    { $this->render( 'admin-hide-price' ); }
	public function discount_ui()      { $this->render( 'admin-discount' ); }
	public function product_lists_ui() { $this->render( 'admin-product-lists' ); }
	public function quote_entries_ui() { $this->render( 'admin-quote-entries' ); }
	public function settings_ui()      { $this->render( 'admin-settings' ); }
	public function upgrade_ui()       { $this->render( 'admin-upgrade' ); }
}
