<?php

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

/**
 * Bankful Payments Blocks integration
 *
 * @since 2.0.2
 */
final class WC_Bankful_WS_Gateway_Blocks_Support extends AbstractPaymentMethodType {


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

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

	/**
	 * Initializes the payment method type.
	 */
	public function initialize() {
		$this->settings = get_option('woocommerce_bankful_credit_card_settings', array());
		$this->gateway  = new WC_Bankful_WS_Gateway();
	}

	/**
	 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
	 *
	 * @return boolean
	 */
	public function is_active() {
		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() {
		$script_path       = '/assets/js-build/frontend-ws/blocks.js';
		$script_asset_path = plugin_abspath() . 'assets/js-build/frontend-ws/blocks.asset.php';

		$bf_cc_form_nonce = wp_create_nonce('bf_cc_form_nonce');

		$script_asset = file_exists($script_asset_path)
			? require $script_asset_path
			: array(
				'dependencies' => array(
					'wc-blocks-registry',
					'wc-settings',
					'wp-element',
					'wp-html-entities',
					'wp-i18n',
				),
				'version'      => '3.0.4',
			);
		$script_url = plugin_url() . $script_path;

		wp_register_script(
			'bankful_credit_card-blocks-integration',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		wp_localize_script('bankful_credit_card-blocks-integration', 'bankful_script_data', array( 'bf_cc_form_nonce' => $bf_cc_form_nonce, 'bankful_directory' => plugins_url() . '/bankful' ));

		// Enqueued script with localized data.
		wp_enqueue_script('bankful_credit_card-blocks-integration');

		if (function_exists('wp_set_script_translations')) {
			wp_set_script_translations('bankful_credit_card-blocks-integration');
		}

		return array( 'bankful_credit_card-blocks-integration' );
	}

	public function payment_fields() {
		return $this->gateway->payment_fields();
	}

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