<?php
/**
 * Gateway Blocks Support
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Helpers;

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Flo_Checkout\Gateways;
use Flo_Checkout\REST_API;
use Flo_Checkout\Shared;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Helpers\Simple_Nonce;
use WC_Payment_Gateway;

/**
 * Implementation of Abstract Payments Blocks
 */
class Gateway_Blocks_Support extends AbstractPaymentMethodType {

	/**
	 * The gateway instance.
	 *
	 * @var WC_Payment_Gateway
	 */
	private WC_Payment_Gateway $gateway;

	/**
	 * Payment method name/id/slug.
	 *
	 * @var string
	 */
	protected $name;

	/**
	 * Payment method plugin key.
	 *
	 * @var string
	 */
	protected string $key;

	/**
	 * Constructor.
	 *
	 * @param string $name The name of the payment method.
	 * @param string $key  The plugin key of the payment method.
	 */
	public function __construct( string $name, string $key ) {
		$this->name = $name;
		$this->key  = $key;
	}

	/**
	 * Initializes the payment method type.
	 */
	public function initialize(): void {
		$this->settings = get_option( 'woocommerce_' . $this->name . '_settings', array() );
		$gateways       = WC()->payment_gateways->payment_gateways();
		$this->gateway  = $gateways[ $this->name ];
	}

	/**
	 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
	 *
	 * @return boolean
	 */
	public function is_active(): bool {
		return $this->gateway->is_available();
	}

	/**
	 * Returns an array of scripts/handles to be registered for this payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles(): array {
		$script_asset_path = Shared::instance()->assets->get_package_assets_path() . 'js/dist/' . $this->key . '.asset.php';
		$script_asset      = file_exists( $script_asset_path )
			? require $script_asset_path
			: array(
				'dependencies' => array(),
				'version'      => Shared::instance()->info->package_data->get_version(),
			);

		$plugin_dashed_key = str_replace( '_', '-', $this->key );
		$plugin_dashed_key = 'wc-' . $plugin_dashed_key . '-payments-blocks';
		wp_register_script(
			$plugin_dashed_key,
			Shared::instance()->assets->get_package_assets_url() . 'js/dist/gateway-blocks/' . $this->key . '.js',
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		wp_localize_script(
			$plugin_dashed_key,
			'wcGatewayData',
			array(
				'restNamespace' => REST_API::REST_NAMESPACE,
				'i18n'          => array(
					'invalidFields' => __( 'Check the invalid fields below.', 'flo-checkout-plugin' ),
				),
			),
		);

		wp_localize_script(
			$plugin_dashed_key,
			'wcGateways',
			array_map(
				function ( $gateway_name ) {
					if ( ! Gateway_Helpers::is_gateway_implementing_flo_interface( $gateway_name ) ) {
						return '';
					}
					/**
					 * Instance of class implementing Interface_Flo_WC_Payment_Gateway.
					 *
					 * @var $gateway_name Interface_Flo_WC_Payment_Gateway
					 */
					return $gateway_name::get_id();
				},
				Gateways::get_gateway_classes()
			),
		);

		if ( function_exists( 'wp_set_script_translations' ) ) {
			wp_set_script_translations( $plugin_dashed_key, 'flo-checkout-plugin', Shared::instance()->package_info->paths->get_path() . 'languages/' );
		}

		return array( $plugin_dashed_key );
	}

	/**
	 * Returns an array of key=>value pairs of data made available to the payment methods script.
	 *
	 * @return array
	 */
	public function get_payment_method_data(): array {
		return array(
			'title'       => $this->get_setting( 'title' ),
			'description' => $this->get_setting( 'description' ),
			'supports'    => array_filter( $this->gateway->supports, array( $this->gateway, 'supports' ) ),
		);
	}
}
