<?php
/**
 * Admin UI for Order Role Manager for WooCommerce
 **/

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

class WOSR_Admin {

	/**
	 * Admin page slug.
	 */
	const PAGE_SLUG = 'wosr-settings';

	/**
	 * Capability required to access the settings.
	 */
	const CAPABILITY = 'manage_options';

	/**
	 * Tabs (slug => label).
	 *
	 * @var array<string,string>
	 */
	private $tabs = array();

	/**
	 * Hook suffix for our admin page (set by add_submenu_page).
	 *
	 * @var string
	 */
	private $page_hook_suffix = '';

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->init_tabs();

		add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
	}

	/**
	 * Initialize tabs.
	 */
	public function init_tabs() {
		$this->tabs = array(
			'order_status'                     => __( 'Order Status Update Restriction', 'order-role-manager-for-woocommerce' ),
			'order_status_overview'            => __( 'Order Status Overview Restriction', 'order-role-manager-for-woocommerce' ),
			'delete_wosr_settings_on_uninstall'=> __( 'Delete Plugin\'s Data on Uninstall', 'order-role-manager-for-woocommerce' ),
			'upgrade'                           => __( 'Upgrade to Pro', 'order-role-manager-for-woocommerce' ),
		);
	}

	/**
	 * Register admin submenu page under WooCommerce.
	 */
	public function add_admin_page() {
		$this->page_hook_suffix = add_submenu_page(
			'woocommerce',
			esc_html__( 'Order Status Restriction', 'order-role-manager-for-woocommerce' ),
			esc_html__( 'Order Role Manager', 'order-role-manager-for-woocommerce' ),
			self::CAPABILITY,
			self::PAGE_SLUG,
			array( $this, 'render_admin_page' )
		);
	}

	/**
	 * Enqueue styles/scripts on our settings page only.
	 *
	 * @param string $hook Current admin page hook suffix.
	 */
	public function enqueue_admin_assets( $hook ) {

		// Only load on our settings screen.
		if ( $hook !== $this->page_hook_suffix ) {
			return;
		}

		/**
		 * Inline handles used with wp_add_inline_style/script elsewhere.
		 */
		wp_register_style(
			'wosr-admin-inline-style',
			false,
			array(),
			defined( 'WOSR_PLUGIN_VERSION' ) ? WOSR_PLUGIN_VERSION : '1.0.0'
		);
		wp_enqueue_style( 'wosr-admin-inline-style' );

		wp_register_script(
			'wosr-admin-inline-script',
			false,
			array( 'jquery' ),
			defined( 'WOSR_PLUGIN_VERSION' ) ? WOSR_PLUGIN_VERSION : '1.0.0',
			true
		);
		wp_enqueue_script( 'wosr-admin-inline-script' );

		// Plugin admin assets.
		wp_register_style(
			'wosr-admin-css',
			trailingslashit( WOSR_PLUGIN_URL ) . 'assets/admin.css',
			array( 'woocommerce_admin_styles' ),
			defined( 'WOSR_PLUGIN_VERSION' ) ? WOSR_PLUGIN_VERSION : '1.0.0'
		);

		wp_register_script(
			'wosr-admin-js',
			trailingslashit( WOSR_PLUGIN_URL ) . 'assets/admin.js',
			array( 'jquery', 'selectWoo' ),
			defined( 'WOSR_PLUGIN_VERSION' ) ? WOSR_PLUGIN_VERSION : '1.0.0',
			true
		);

		// Enqueue now that everything is registered.
		wp_enqueue_style( 'wosr-admin-css' );
		wp_enqueue_style( 'woocommerce_admin_styles' );
		wp_enqueue_script( 'selectWoo' );
		wp_enqueue_script( 'wosr-admin-js' );

		// Localize for AJAX usage in admin.js.
		wp_localize_script(
			'wosr-admin-js',
			'wosr_vars',
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'wosr_toggle_status' ),
			)
		);
	}

	/**
	 * Render the admin page.
	 */
	public function render_admin_page() {
		if ( ! current_user_can( self::CAPABILITY ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'order-role-manager-for-woocommerce' ) );
		}

		// Read-only: active tab from query string.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only
		$active_tab = isset( $_GET['tab'] )
			// Tabs are slugs; sanitize_key fits better than sanitize_text_field.
			? sanitize_key( wp_unslash( $_GET['tab'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only
			: 'order_status';

		// Fallback to default if invalid tab provided.
		if ( ! array_key_exists( $active_tab, $this->tabs ) ) {
			$active_tab = 'order_status';
		}

		echo '<div class="wrap">';
		echo '<h1>' . esc_html__( 'Order Role Manager for WooCommerce', 'order-role-manager-for-woocommerce' ) . '</h1>';

		// Tabs.
		echo '<h2 class="nav-tab-wrapper">';

		foreach ( $this->tabs as $tab => $label ) {
			$tab_url = add_query_arg(
				array(
					'page' => self::PAGE_SLUG,
					'tab'  => $tab,
				),
				admin_url( 'admin.php' )
			);

			$class = ( $active_tab === $tab ) ? ' nav-tab-active' : '';

			printf(
				'<a href="%1$s" class="nav-tab%2$s">%3$s</a>',
				esc_url( $tab_url ),
				esc_attr( $class ),
				esc_html( $label )
			);
		}

		echo '</h2>';

		/**
		 * Render the selected tab content.
		 *
		 * Action: wosr_render_tab_{tab_slug}
		 */
		do_action( "wosr_render_tab_{$active_tab}" );

		echo '</div>';
	}
}

new WOSR_Admin();
