<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

final class Flexilay_Woo_Admin {

	public static function init() : void {
		add_action( 'admin_menu', array( __CLASS__, 'menu' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
	}

	public static function menu() : void {
		$cap  = 'manage_woocommerce';
		$slug = 'flexilay';

		add_menu_page(
			__( 'FlexiLay', 'flexilay-gateway-for-woocommerce' ),
			__( 'FlexiLay', 'flexilay-gateway-for-woocommerce' ),
			$cap,
			$slug,
			array( __CLASS__, 'render_app' ),
			'dashicons-money-alt',
			56
		);

		add_submenu_page(
			$slug,
			__( 'Onboarding', 'flexilay-gateway-for-woocommerce' ),
			__( 'Onboarding', 'flexilay-gateway-for-woocommerce' ),
			$cap,
			'flexilay-onboarding',
			array( __CLASS__, 'render_app' )
		);

		add_submenu_page(
			$slug,
			__( 'Plans', 'flexilay-gateway-for-woocommerce' ),
			__( 'Plans', 'flexilay-gateway-for-woocommerce' ),
			$cap,
			'flexilay-plans',
			array( __CLASS__, 'render_app' )
		);

		add_submenu_page(
			$slug,
			__( 'Settings', 'flexilay-gateway-for-woocommerce' ),
			__( 'Settings', 'flexilay-gateway-for-woocommerce' ),
			$cap,
			'flexilay-settings',
			array( __CLASS__, 'render_app' )
		);
	}

	public static function render_app() : void {
		echo '<div class="wrap"><div id="flexilay-admin-app"><div class="flexilay-admin-shell"></div></div></div>';
	}

	public static function enqueue( string $hook ) : void {
		// Only load on our pages.
		if ( strpos( $hook, 'flexilay' ) === false ) {
			return;
		}

		wp_enqueue_style(
			'flexilay-admin',
			FLEXILAY_WOO_PLUGIN_URL . 'assets/css/admin.css',
			array(),
			filemtime( FLEXILAY_WOO_PLUGIN_DIR . 'assets/css/admin.css' )
		);

		wp_enqueue_script(
			'flexilay-admin-app',
			FLEXILAY_WOO_PLUGIN_URL . 'assets/js/admin-app.js',
			array( 'wp-api-fetch', 'wp-element', 'wp-components', 'wp-i18n' ),
			filemtime( FLEXILAY_WOO_PLUGIN_DIR . 'assets/js/admin-app.js' ),
			true
		);

		wp_localize_script( 'flexilay-admin-app', 'flexilayAdmin', array(
			'restRoot'            => esc_url_raw( rest_url( 'flexilay/v1' ) ),
			'pluginUrl'           => FLEXILAY_WOO_PLUGIN_URL,
			'nonce'               => wp_create_nonce( 'wp_rest' ),
			'apiBase'             => Flexilay_Woo_Options::get_api_base(),
			'shopDomain'          => Flexilay_Woo_Options::get_shop_domain(),
			'hasToken'            => Flexilay_Woo_Options::get_install_token() !== '',
			'hasApiKey'           => Flexilay_Woo_Options::get_api_key() !== '',
			'enableCrisp'         => Flexilay_Woo_Options::get_enable_crisp(),
			'crispWebsiteId'      => '85971abe-f421-422f-a712-a8c447380beb',
			'crispThemeColor'     => '#2563eb',
			'onboardingVideoUrl'  => '',
			'checkoutVideoUrl'    => '',
			'legalTosUrl'         => '',
			'legalMsaUrl'         => '',
			'legalPrivacyUrl'     => '',
		) );

		if ( Flexilay_Woo_Options::get_enable_crisp() ) {
			wp_enqueue_script(
				'flexilay-admin-crisp',
				FLEXILAY_WOO_PLUGIN_URL . 'assets/js/admin-crisp.js',
				array(),
				filemtime( FLEXILAY_WOO_PLUGIN_DIR . 'assets/js/admin-crisp.js' ),
				true
			);

			wp_localize_script( 'flexilay-admin-crisp', 'flexilayAdminCrisp', array(
				'websiteId'  => '85971abe-f421-422f-a712-a8c447380beb',
				'themeColor' => '#2563eb',
				'shopDomain' => Flexilay_Woo_Options::get_shop_domain(),
			) );
		}
	}
}
