<?php
/**
 * Class ShipmentAssets
 *
 * @package DhlPolska\Shipment
 */

namespace DhlPolska\Shipment;

use DhlPolska\WooCommerceSettings\WooCommerceDHLPolskaSettingsFields;
use DhlPolskaVendor\WPDesk\FS\Shipment\Assets;
use DhlPolskaVendor\WPDesk\PluginBuilder\Plugin\Hookable;

/**
 * Add Assets
 */
class ShipmentAssets implements Hookable {
	public const HANDLE = 'dhl_polska_admin_order';

	/**
	 * @var string
	 */
	private $assets_url;

	/**
	 * @var string
	 */
	private $scripts_version;

	/**
	 * @var bool
	 */
	private $is_test_mode;

	/**
	 * IntegrationAssets constructor.
	 *
	 * @param string $assets_url      .
	 * @param string $scripts_version .
	 * @param bool   $is_test_mode     .
	 */
	public function __construct( $assets_url, $scripts_version, bool $is_test_mode = false ) {
		$this->assets_url      = $assets_url;
		$this->scripts_version = $scripts_version;
		$this->is_test_mode    = $is_test_mode;
	}

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

	/**
	 * @return void
	 */
	public function add_assets() {
		if ( ! wp_script_is( Assets::HANDLE ) ) {
			return;
		}

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

		wp_enqueue_script( 'thickbox' );
		wp_enqueue_style( 'thickbox' );

		wp_enqueue_script( self::HANDLE, $this->assets_url . 'js/admin-order' . $suffix . '.js', [ 'jquery', Assets::HANDLE, 'thickbox' ], $this->scripts_version, true );

			wp_localize_script(
				self::HANDLE,
				'__jsDHLPolskaVars',
				[
					'slug'             => WooCommerceDHLPolskaSettingsFields::SHIPPING_METHOD_ID,
					'ajax_url'         => admin_url( 'admin-ajax.php' ),
					'service_point'    => DhlServices::SERVICE_POINT,
					'map_url'          => $this->is_test_mode ? 'https://psm-sandbox.dhl24.com.pl' : 'https://parcelshop.dhl.pl',
					'point_type_label' => __( 'Typ punktu', 'dhl-polska' ),
				]
			);

		wp_enqueue_style( self::HANDLE, $this->assets_url . 'css/admin-order' . $suffix . '.css', [], $this->scripts_version );
	}
}
