<?php

namespace Simplyin\Simplyin_W_Plugin\Hooks;

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

use Simplyin\Simplyin_W_Plugin\Controller\Ajax\Widget\Widget_Controller;
use Simplyin\Simplyin_W_Plugin\Hooks\Shop_Frontend\Shop_Frontend_Hooks;
use Simplyin\Simplyin_W_Plugin\Shipping_Mapping\Mapping;
use WC_Order;

class Hooks {

	/**
	 * @var array<int, true>
	 */
	private static $simplyin_checkout_order_create_handled = [];

	public function init() {
		add_action( 'woocommerce_checkout_order_created',
			[ $this, 'woocommerce_checkout_order_created' ],
			10,
			1 );

		add_action( 'woocommerce_store_api_checkout_order_processed',
			[ $this, 'woocommerce_checkout_order_created_in_blocks' ],
			10,
			1 );

		add_action( 'woocommerce_review_order_after_order_total',
			[ $this, 'classic_checkout_init' ],
			10,
			0 );

		add_action( 'template_redirect',
			[ $this, 'block_checkout_init' ],
			10 );

//		add_action( 'woocommerce_store_api_cart_update_order_from_request',
//			[ $this, 'woocommerce_checkout_init' ],
//			10,
//			0 );


		add_action( 'flexible_shipping_shipment_confirmed',
			[ $this, 'flexible_shipping_shipment_confirmed' ],
			10,
			1 );

		( new Shop_Frontend_Hooks() )->init();

		$this->init_checkout_hooks();

	}

	private function init_checkout_hooks(): void {
		add_filter( 'woocommerce_checkout_fields', 'simplyInCustomOverrideCheckoutFields', 100, 1 );
		add_action( 'wp_enqueue_scripts', 'simplyin_load_react_app_for_checkout', 50 );
		add_action( 'woocommerce_checkout_after_order_review', 'SimplyInAddCheckboxFlagField' );
		add_action( 'woocommerce_checkout_update_order_meta', 'SimplyInSaveFlagCheckboxValue' );
		add_filter( 'woocommerce_checkout_fields', 'SimplyInAddTaxIdToBilling', 9999 );
		add_action( 'woocommerce_checkout_update_order_meta', 'SimplyInSaveTaxIdToOrder' );
		add_action( 'woocommerce_store_api_checkout_update_order_from_request', 'SimplyInBlockCheckoutSaveTaxIdToOrder', 10, 2 );
		add_action( 'wp_enqueue_scripts', 'SimplyInAddjQueryToFrontend' );
		add_action( 'woocommerce_checkout_update_order_meta', 'SimplyInSaveCustomCheckoutField' );
		add_action( 'woocommerce_admin_order_data_after_billing_address', 'SimplyInDisplayCustomOrderData' );
	}

	public function block_checkout_init(): void {
		if ( is_checkout() && has_block('woocommerce/checkout') ) {
			$this->run_handle_checkout();
		}
	}

	public function classic_checkout_init(): void {
		if ( ! has_block('woocommerce/checkout') ) {
			$this->run_handle_checkout();
		}
	}

	private function run_handle_checkout(): void {
		( new Widget_Controller() )->maybe_resync_checkout_from_simplyin_session();
		$shipping_mapping = new Mapping();
		$shipping_mapping->handle_checkout();
	}

	public function woocommerce_checkout_order_created( WC_Order $order ) {
		$this->run_simplyin_on_order_create_once_per_request( $order );
	}

	public function woocommerce_checkout_order_created_in_blocks(
		WC_Order $order
	) {
		$this->run_simplyin_on_order_create_once_per_request( $order );
	}

	/**
	 * Classic and block checkout can both fire for the same order; run SimplyIN API once.
	 */
	private function run_simplyin_on_order_create_once_per_request( WC_Order $order ): void {
		$order_id = $order->get_id();
		if ( isset( self::$simplyin_checkout_order_create_handled[ $order_id ] ) ) {
			return;
		}
		self::$simplyin_checkout_order_create_handled[ $order_id ] = true;

		( new simplyInOnOrderCreate() )->run( $order );
	}

	public function flexible_shipping_shipment_confirmed(
		$flexible_shipping_shipment
	) {

		( new Fs_Shipment() )->handle_shipment_confirmed( $flexible_shipping_shipment );

	}


}
