<?php

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

/**
 * GreenPay Blocks integration
 *
 * @since 1.0.3
 */
final class WC_GreenPay_Gateway_Blocks_Support extends AbstractPaymentMethodType
{
	/** @var WC_GreenPay_Gateway */
	private $gateway;

	/** @var array */
	protected $settings = [];

	/** @var string */
	protected $name = 'greenmoney';

	public function initialize()
	{
		$this->settings = get_option('gmpg_settings', []);
		$gateways       = WC()->payment_gateways->payment_gateways();
		$this->gateway  = isset($gateways[$this->name]) ? $gateways[$this->name] : null;
	}

	public function is_active()
	{
		if (!$this->gateway || !$this->gateway->is_available()) {
			return false;
		}

		return WC_GreenPay_Checkout_Assets::is_blocks_checkout_context();
	}

	private static function register_blocks_assets(
		$script_handle,
		$style_handle,
		$dev_url,
		$build_url,
		$script_path,
		$style_path,
		$asset_path
	) {
		$is_dev = get_site_url() === 'http://localhost:8881';

		if ($is_dev) {
			$script_url = $dev_url . $script_path;
			$style_url = $dev_url . $style_path;
			wp_script_add_data($script_handle, 'crossorigin', 'anonymous');
		} else {
			$script_url = $build_url . $script_path;
			$style_url = $build_url . $style_path;
		}

		$asset_fs_path = WC_GreenPay_Payments::plugin_abspath() . ltrim($asset_path, '/');
		$asset = file_exists($asset_fs_path)
			? require($asset_fs_path)
			: array(
				'dependencies' => array(
					'react',
					'react-dom',
					'wc-blocks-registry',
					'wc-settings',
					'wp-data',
					'wp-element',
					'wp-html-entities',
				),
				'version' => time(),
			);

		$deps = array_unique(array_merge(
			$asset['dependencies'],
			array('wc-blocks-registry', 'wc-settings')
		));

		wp_register_style($style_handle, $style_url, array(), $asset['version']);
		wp_register_script($script_handle, $script_url, $deps, $asset['version'], true);

		return $script_handle;
	}

	public function get_payment_method_script_handles()
	{
		if (!WC_GreenPay_Checkout_Assets::is_blocks_checkout_context()) {
			return [];
		}

		WC_GreenPay_Checkout_Assets::dequeue_classic_assets();

		$script_handle = 'wc-greenpay-blocks-script';
		$style_handle = 'wc-greenpay-blocks-style';

		self::register_blocks_assets(
			$script_handle,
			$style_handle,
			'http://localhost:3000',
			WC_GreenPay_Payments::plugin_url(),
			'/dist/greenpay-blocks.js',
			'/dist/greenpay-blocks.css',
			'/dist/greenpay-blocks.asset.php'
		);

		wp_enqueue_style($style_handle);

		if (function_exists('wp_set_script_translations')) {
			wp_set_script_translations(
				$script_handle,
				'woocommerce-greenpay-gateway',
				WC_GreenPay_Payments::plugin_abspath() . 'languages/'
			);
		}

		return array($script_handle);
	}

	public function get_payment_method_data()
	{
		$context = '';

		if (function_exists('is_checkout_pay_page') && is_checkout_pay_page()) {
			$context = WC_GreenPay_Checkout_Assets::CONTEXT_CLASSIC;
		} elseif (WC_GreenPay_Checkout_Assets::is_blocks_checkout_context()) {
			$context = WC_GreenPay_Checkout_Assets::CONTEXT_BLOCKS;
		}

		return array_merge(
			array(
				'clientId'     => $this->settings['gmpg_client_id'] ?? '',
				'plaidEnabled' => $this->settings['gmpg_plaid_enabled'] ?? false,
				'endpoint'     => $this->settings['gmpg_api_endpoint'] ?? '',
				'context'      => $context,
				'title'        => $this->settings['gmpg_title'] ?? 'GreenPay',
				'description'  => $this->settings['gmpg_gateway_description'] ?? '',
				'extraMessage' => $this->settings['gmpg_extra_message'] ?? '',
				'supports'     => ($this->gateway && isset($this->gateway->supports))
					? array_filter($this->gateway->supports, array($this->gateway, 'supports'))
					: array(),
			),
			function_exists('gmpg_get_bank_login_frontend_settings') ? gmpg_get_bank_login_frontend_settings() : array(),
			function_exists('gmpg_get_ribbit_frontend_settings') ? gmpg_get_ribbit_frontend_settings() : array()
		);
	}
}

add_filter('woocommerce_blocks_register_payment_method_type', function ($payment_methods) {
	$payment_methods[] = 'greenmoney';
	return $payment_methods;
});

add_filter('woocommerce_get_country_locale', function ($locale) {
	foreach ($locale as $key => $value) {
		$locale[$key]['phone'] = array(
			'required' => true,
			'hidden'   => false,
		);
	}

	return $locale;
});
