<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

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

final class WC_Bobpay_Instant_EFT_Blocks_Support extends AbstractPaymentMethodType
{
	protected $name = 'bobpay_instant_eft';

	public function initialize()
	{
		$bobpay_settings = get_option('woocommerce_bobpay_settings');
		$instant_eft_settings = get_option('woocommerce_bobpay_instant_eft_settings', []);
		$this->settings = wp_parse_args($instant_eft_settings, $bobpay_settings);
	}

	public function is_active()
	{
		$payment_gateways_class = WC()->payment_gateways();
		$payment_gateways = $payment_gateways_class->payment_gateways();
		if (!isset($payment_gateways[$this->name])) {
			return false;
		}

		return $payment_gateways[$this->name]->is_available();
	}

	public function get_payment_method_data()
	{
		return [
			'title' => $this->get_setting('title'),
			'description' => '',
			'supports' => $this->get_supported_features(),
			'instant_eft_url' => WC_BOBPAY_PLUGIN_URL . '/assets/images/instant-eft-new.png',
		];
	}

	public function get_payment_method_script_handles()
	{
		$asset_path = WC_BOBPAY_PLUGIN_PATH . '/build/js/front-end/index.asset.php';
		$version = WC_BOBPAY_PLUGIN_VERSION;
		$dependencies = [];
		if (file_exists($asset_path)) {
			$asset = require $asset_path;
			$version = is_array($asset) && isset($asset['version']) ? $asset['version'] : $version;
			$dependencies = is_array($asset) && isset($asset['dependencies']) ? $asset['dependencies'] : $dependencies;
		}
		wp_register_script(
			'wc-bobpay-instant-eft-blocks-integration',
			WC_BOBPAY_PLUGIN_URL . '/build/js/frontend/index.js',
			$dependencies,
			$version,
			true
		);
		wp_set_script_translations(
			'wc-bobpay-instant-eft-blocks-integration',
			'bobpay-woo'
		);
		return ['wc-bobpay-instant-eft-blocks-integration'];
	}

	public function get_supported_features()
	{
		$payment_gateways = WC()->payment_gateways()->payment_gateways();
		if (!isset($payment_gateways[$this->name])) {
			return [];
		}

		return $payment_gateways[$this->name]->supports;
	}
}
