<?php
namespace ApproveOrders\Compatibility;

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use ApproveOrders\Modules\PrePayGateway as AOFWC_PrePayGateway;

/**
 * PreOrder Blocks Support
 *
 * Adds support for WooCommerce Blocks Checkout
 */
final class PreOrderBlocksSupport extends AbstractPaymentMethodType {
	/**
	 * Payment method name defined by payment methods extending this class.
	 *
	 * @var string
	 */
	protected $name    = 'preordergateway-approve-orders';
	protected $gateway = null;

	/**
	 * Initializes the payment method type.
	 */
	public function initialize() {
		$this->settings = get_option( 'woocommerce_' . $this->name . '_settings', array() );

		foreach ( \WC_Payment_Gateways::instance()->payment_gateways() as $gateway ) {
			if ( $gateway instanceof AOFWC_PrePayGateway ) {
				$this->gateway = $gateway;
				break;
			}
		}
	}

	/**
	 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
	 *
	 * @return boolean
	 */
	public function is_active() {
		return true;
		// return ! empty( $this->settings['preorder-enabled'] ) && 'yes' === $this->settings['preorder-enabled'];
	}

	/**
	 * Returns an array of scripts/handles to be registered for this payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles() {

		wp_register_script(
			'aofwc-blocks-integration',
			AOFWC_URL . 'assets/js/aofwc-block.js',
			array(
				'wc-blocks-registry',
				'wc-settings',
				'wp-element',
				'wp-html-entities',
				'wp-i18n',
			),
			null,
			true
		);
		if ( function_exists( 'wp_set_script_translations' ) ) {
			wp_set_script_translations( 'aofwc-blocks-integration', 'approve-orders', AOFWC_DIR . 'languages/' );

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

	/**
	 * 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'        => ! empty( $this->settings['preorder-title'] ) ? $this->settings['preorder-title'] : __( 'Pre Order', 'approve-orders' ),
			'description'  => ! empty( $this->settings['preorder-description'] ) ? $this->settings['preorder-description'] : __( 'Place an order without making a payment. Payment will be processed later.', 'approve-orders' ),
			'instructions' => ! empty( $this->settings['preorder-instructions'] ) ? $this->settings['preorder-instructions'] : __( 'Thank you! Your order is pending approval. You will be notified once reviewed.', 'approve-orders' ),
			'supports'     => array(
				'products',
				'refunds',
			),
		);
	}
}
