<?php
/**
 * Class InstanceSettingsAssets
 *
 * @package DhlPolska\ShippingMethod
 */

namespace DhlPolska\ShippingMethod;

use DhlPolska\Integration\FlexibleShipping\FlexibleShippingIntegration;
use DhlPolska\Shipment\DhlServices;
use DhlPolska\Shipment\ShipmentData;
use DhlPolska\ShippingMethod\LogoVariants;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * Adds scripts for DHL instance settings.
 */
class InstanceSettingsAssets implements Hookable {

	public const HANDLE = 'dhl_polska_shipping_settings';

	private string $assets_url;

	private string $scripts_version;

	public function __construct( string $assets_url, string $scripts_version ) {
		$this->assets_url      = $assets_url;
		$this->scripts_version = $scripts_version;
	}

	/**
	 * @return void
	 */
	public function hooks() {
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
	}

	/**
	 * @return void
	 */
	public function enqueue_assets() {
		if ( ! $this->is_shipping_settings_screen() ) {
			return;
		}

		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

		wp_register_script(
			self::HANDLE,
			$this->assets_url . 'js/admin-shipping-settings' . $suffix . '.js',
			[ 'jquery' ],
			$this->scripts_version,
			true
		);
		wp_add_inline_script(
			self::HANDLE,
			'window.dhlPolskaShippingSettingsConfig = ' . wp_json_encode( $this->get_script_config() ) . ';',
			'before'
		);
		wp_enqueue_script( self::HANDLE );
	}

	private function is_shipping_settings_screen(): bool {
		if ( ! function_exists( 'get_current_screen' ) ) {
			return false;
		}

		$screen = get_current_screen();
		if ( ! $screen instanceof \WP_Screen ) {
			return false;
		}

		if ( 'woocommerce_page_wc-settings' !== $screen->id ) {
			return false;
		}

		global $current_tab;

		return 'shipping' === (string) $current_tab;
	}

	/**
	 * @return array<string, mixed>
	 */
	private function get_script_config(): array {
		$integration_id = FlexibleShippingIntegration::INTEGRATION_ID;
		$logo_variants  = [];
		foreach ( LogoVariants::get_variant_file_map() as $logo_variant => $logo_file ) {
			$logo_variants[ $logo_variant ] = $this->assets_url . 'images/' . $logo_file;
		}

		return [
			'flexible_shipping'    => [
				'integration_selector'  => '#woocommerce_flexible_shipping_method_integration',
				'service_selector'      => '#woocommerce_flexible_shipping_' . $integration_id . '_' . ShipmentData::OPTION_SERVICE,
				'show_logo_selector'    => '#woocommerce_flexible_shipping_' . $integration_id . '_' . DhlInstanceSettings::OPTION_SHOW_LOGO,
				'logo_variant_selector' => '#woocommerce_flexible_shipping_' . $integration_id . '_' . DhlInstanceSettings::OPTION_LOGO_VARIANT,
				'point_type_selectors'  => [
					'#woocommerce_flexible_shipping_' . $integration_id . '_' . ShipmentData::OPTION_POINT_TYPE,
					'#woocommerce_flexible_shipping_' . $integration_id . '_' . ShipmentData::OPTION_POINT_TYPE_PL,
				],
				'field_selector'        => '.js--field-' . $integration_id,
				'integration_id'        => $integration_id,
				'service_point'         => DhlServices::SERVICE_POINT,
			],
			'instance_settings'    => [
				'service_selector'      => '#woocommerce_' . DhlShippingMethod::SHIPPING_METHOD_ID . '_' . DhlInstanceSettings::OPTION_SERVICE,
				'show_logo_selector'    => '#woocommerce_' . DhlShippingMethod::SHIPPING_METHOD_ID . '_' . DhlInstanceSettings::OPTION_SHOW_LOGO,
				'logo_variant_selector' => '#woocommerce_' . DhlShippingMethod::SHIPPING_METHOD_ID . '_' . DhlInstanceSettings::OPTION_LOGO_VARIANT,
				'point_type_selectors'  => [
					'#woocommerce_' . DhlShippingMethod::SHIPPING_METHOD_ID . '_' . DhlInstanceSettings::OPTION_POINT_TYPE,
					'#woocommerce_' . DhlShippingMethod::SHIPPING_METHOD_ID . '_' . DhlInstanceSettings::OPTION_POINT_TYPE_PL,
				],
				'settings_modal_id'     => 'wc-modal-shipping-method-settings',
			],
			'logo_variants'        => $logo_variants,
			'default_logo_variant' => LogoVariants::get_default_variant(),
			'logo_preview_class'   => 'dhl-polska-logo-variant-preview',
			'logo_alt'             => 'DHL Polska',
		];
	}
}
