<?php
/**
 * Handles the BNPL waiting screen.
 */

namespace Flo_Checkout\Gateways\BNPL;

use Flo_Checkout\Shared;
use WC_Order;
use WP_Error;

/**
 * Class BNPL_Waiting_Screen
 */
class BNPL_Waiting_Screen {
	const WAITING_PAGE_SLUG = 'northmill_bnpl_waiting_page'; // Slug for the waiting page.

	/**
	 * Boolean variable indicating whether the current page is a waiting page or not.
	 *
	 * @var bool $is_waiting_page
	 */
	private static bool $is_waiting_page = false;

	/**
	 * Sets up the required actions for the waiting page functionality.
	 *
	 * @return void
	 */
	public static function setup(): void {
		if ( 'yes' === BNPL_Settings::get( 'enabled' ) ) {
			// Registers BNPL waiting page to the WooCommerce API.
			add_action( 'woocommerce_api_' . self::WAITING_PAGE_SLUG, array( self::class, 'display_waiting_page' ), 10, 0 );

			add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
			add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_styles' ), 9999 );
		}
	}

	/**
	 * Retrieve the WC_Order object for the waiting page.
	 *
	 * @return WC_Order The WC_Order object for the waiting page.
	 *
	 * @throws WP_Error If the order ID is invalid.
	 */
	public static function get_waiting_page_order(): WC_Order {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$order_id = isset( $_GET['order_id'] ) ? sanitize_text_field( $_GET['order_id'] ) : null;
		$order    = wc_get_order( $order_id );
		if ( $order ) {
			return $order;
		}

		wp_die( 'Invalid order ID', 'Invalid order ID', 404 );
	}


	/**
	 * Display the waiting page.
	 */
	public static function display_waiting_page(): never {
		self::$is_waiting_page = true;
		$order                 = self::get_waiting_page_order();

		$bnpl_url = $order->get_meta( 'northmill_bnpl_transaction_url' );
		$bnpl_qr  = $order->get_meta( 'northmill_bnpl_transaction_qr' );

		include Shared::instance()->package_info->paths->get_path() . 'views/bnpl/waiting_screen.php';
		exit;
	}

	/**
	 * Enqueues the necessary scripts for the waiting page functionality.
	 *
	 * This method is called on the `wp_enqueue_scripts` action hook to enqueue the JavaScript file `bnpl_waiting_screen.js`.
	 * It also localizes the script with the required data for AJAX requests.
	 *
	 * @return void
	 */
	public static function enqueue_scripts(): void {
		if ( self::$is_waiting_page ) {
			$order = self::get_waiting_page_order();

			wp_enqueue_script(
				'northmill-bnpl-waiting-screen-script',
				Shared::instance()->assets->get_package_assets_url() . 'assets/bnpl_waiting_screen.js',
				array( 'jquery' ),
				filemtime( Shared::instance()->assets->get_package_assets_path() . 'js/bnpl_waiting_screen.js' ),
				true
			);

			// Localize the script with ajax url and nonce.
			wp_localize_script(
				'northmill-bnpl-waiting-screen-script',
				'northmill_bnpl_waiting_screen',
				array(
					'ajax_url'           => admin_url( 'admin-ajax.php' ),
					'nonce'              => wp_create_nonce( 'northmill_bnpl_transaction' ),
					'cancel_url'         => $order->get_cancel_order_url(),
					'order_received_url' => $order->get_checkout_order_received_url(),
					'bnpl_link_text'     => __( 'Complete checkout', 'flo-checkout-plugin' ),
				)
			);
		}
	}

	/**
	 * Enqueues the required styles for the waiting screen.
	 *
	 * This method checks if the current page is a valid waiting page and enqueues the appropriate style.
	 *
	 * @return void
	 */
	public static function enqueue_styles(): void {
		if ( self::$is_waiting_page ) {
			wp_enqueue_style(
				'northmill-bnpl-waiting-screen-style',
				Shared::instance()->assets->get_package_assets_url() . 'css/bnpl-waiting-screen.css',
				array(),
				filemtime( Shared::instance()->assets->get_package_assets_path() . 'css/bnpl-waiting-screen.css' )
			);
		}
	}
}
