<?php
/**
 * Payment Gateway for ZainPay WooCommerce Blocks Support
 *
 * @package WooCommerce_PGZW_ZainPay
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Automattic\WooCommerce\StoreApi\Payments\PaymentContext;
use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;

final class PGZW_Blocks_Support extends AbstractPaymentMethodType {

	protected $name = 'pgzw_zainpay';

	public function initialize() {
		$this->settings = get_option( 'woocommerce_pgzw_zainpay_settings', [] );
		add_action( 'woocommerce_rest_checkout_process_payment_with_context', [ $this, 'failed_payment_notice' ], 8, 2 );
	}

	public function is_active() {
		$payment_gateways_class = WC()->payment_gateways();
		if ( ! $payment_gateways_class ) {
			return false;
		}
		$payment_gateways = $payment_gateways_class->payment_gateways();
		if ( empty( $payment_gateways['pgzw_zainpay'] ) ) {
			return false;
		}
		return $payment_gateways['pgzw_zainpay']->is_available();
	}

	public function get_payment_method_script_handles() {
		$script_path       = '/assets/js/blocks/pgzw-zainpay-blocks.js';
		$script_asset_path = plugin_dir_path( PGZW_MAIN_FILE ) . 'assets/js/blocks/pgzw-zainpay-blocks.asset.php';
		$script_asset      = file_exists( $script_asset_path )
			? require( $script_asset_path )
			: array(
				'dependencies' => array(),
				'version'      => PGZW_VERSION,
			);
		$script_url        = PGZW_URL . $script_path;

		wp_register_script(
			'pgzw-zainpay-blocks',
			$script_url,
			array_merge(
				$script_asset['dependencies'],
				array(
					'wc-blocks-registry',
					'wc-settings',
					'wp-element',
					'wp-html-entities',
					'wp-i18n',
				)
			),
			$script_asset['version'],
			true
		);

		if ( function_exists( 'wp_set_script_translations' ) ) {
			wp_set_script_translations( 'pgzw-zainpay-blocks', 'payment-gateway-for-zainpay-for-woocommerce', plugin_dir_path( PGZW_MAIN_FILE ) . 'languages' );
		}

		return [ 'pgzw-zainpay-blocks' ];
	}

	public function get_payment_method_data() {
		$payment_gateways_class = WC()->payment_gateways();
		$gateway = null;
		if ( $payment_gateways_class ) {
			$payment_gateways = $payment_gateways_class->payment_gateways();
			$gateway = isset( $payment_gateways['pgzw_zainpay'] ) ? $payment_gateways['pgzw_zainpay'] : null;
		}

		return [
			'title'             => $this->get_setting( 'title' ),
			'description'       => $this->get_setting( 'description' ),
			'supports'          => $gateway ? array_filter( $gateway->supports, [ $gateway, 'supports' ] ) : [],
			'allow_saved_cards' => ( $gateway && ! empty( $gateway->saved_cards ) && is_user_logged_in() ),
			'logo_url'          => PGZW_URL . '/assets/images/zainpay.png',
		];
	}

	public function failed_payment_notice( PaymentContext $context, PaymentResult &$result ) {
		if ( 'pgzw_zainpay' === $context->payment_method ) {
			add_action(
				'pgzw_process_payment_error',
				function( $failed_notice ) use ( &$result ) {
					$payment_details                 = $result->payment_details;
					$payment_details['errorMessage'] = wp_strip_all_tags( $failed_notice );
					$result->set_payment_details( $payment_details );
				}
			);
		}
	}
}