<?php
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
final class TamiWooBlocks extends AbstractPaymentMethodType {
	private $localized = 0;
	private $gateway;
	protected $name = 'tami_woocommerce';

	public function initialize() {
		if ( ! class_exists( '\Tami_WooCommerce_GW' ) ) {
			include TAMI_PLUGIN_PATH . '/lib/Tami_WooCommerce_GW.php';
		}

		$this->settings = array();
		$this->gateway  = new \Tami_WooCommerce_GW();
	}

	public static function register() {
		add_action( 'woocommerce_blocks_payment_method_type_registration',
			function ( $registry ) {
				$registry->register( new static() );
			} );
	}

	public function is_active() {
		return true;
	}

	public function get_payment_method_script_handles() {
		$script_path  = 'block-assets/app/output.js';
		$script_asset_path = TAMI_PLUGIN_PATH . '/checkout/assets/blocks/output.asset.php';
		$script_asset      = file_exists( $script_asset_path )
			? require( $script_asset_path ) : array(
				'dependencies' => array(),
				'version'      => '1.0.0'
			);
		$script_url   = \Tami\WooCommerce\TamiCheckout::get_assets('blocks/output.js');

		wp_register_script(
			'wc-tami_woocommerce-payments-blocks',
			$script_url,
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		if ( function_exists( 'wp_set_script_translations' ) ) {
			wp_set_script_translations( 'wc-tami_woocommerce-payments-blocks', 'tami-payment', TAMI_PLUGIN_PATH . '/languages' );
		}

		return [ 'wc-tami_woocommerce-payments-blocks' ];
	}

	public function get_payment_method_data() {

		if ( ! class_exists( '\Tami_WooCommerce_GW' ) ) {
			include TAMI_PLUGIN_PATH . '/lib/Tami_WooCommerce_GW.php';
		}

		$tami_woo_commerce_GW = new \Tami_WooCommerce_GW();
		$paymentButton = __( 'Make Payment', 'tami-payment' );

		return array_merge( [
			'title'               => $this->get_setting( 'title' ),
			'description'         => $this->get_setting( 'description' ),
			'make_payment_button' => $paymentButton,
			'iconsrc'             => apply_filters( 'tami_payment_method_block_logo_url', \Tami\WooCommerce\TamiCheckout::get_assets( 'img/credits.png' ) ),
			'supports'            => array_filter( $this->gateway->supports, [ $this->gateway, 'supports' ] )
		], $tami_woo_commerce_GW->settings );
	}
}