<?php

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

/**
 * PrecisionPay Payments Blocks integration
 * 
 * @since 4.0.0
 */
final class WC_Gateway_PrecisionPay_Blocks_Support extends AbstractPaymentMethodType {
  /**
   * The gateway instance.
   * 
   * @var WC_Gateway_PrecisionPay
   */
  private $gateway;

  /**
   * Payment method name/id/slug.
   * 
   * @var string
   */
  protected $name = PrecisionPay_Payments_For_WC::NAME;

  public function initialize() {
    $this->settings = get_option("woocommerce_{$this->name}_settings", []);
    $gateways       = WC()->payment_gateways->payment_gateways();
    $this->gateway  = $gateways[$this->name];
  }

  /**
   * Returns if this payment method should be active. If false, the scripts will not be enqueued.
   * 
   * @return boolean
   */
  public function is_active() {
    $available = $this->gateway->is_available();
    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       = '/build/blocks.js';
    $script_asset_path = WC_PrecisionPay_Payments::plugin_abspath() . 'build/blocks.asset.php';
    $script_asset      = file_exists($script_asset_path)
      ? require($script_asset_path)
      : [
        'dependencies' => [],
        'version' => PRCSNPY_VERSION
      ];
    $script_url        = WC_PrecisionPay_Payments::plugin_url() . $script_path;

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

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

  /**
   * Returns an arary of key=> value pairs of data made available to the payment methods script.
   * 
   * @return array
   */
  public function get_payment_method_data() {
    return [
      'id'          => $this->gateway->id,
      'title'       => $this->gateway->title,
      'description' => $this->gateway->description,
      'logo'        => $this->gateway->logo,
      'buttonTitle' => $this->gateway->button_title,
      'buttonLogo'  => $this->gateway->logo_white,
      'brandColor'  => PrecisionPay_Payments_For_WC::BRAND_COLOR,
      'pluginUrl'   => $this->gateway->plugin_url,
      'wpNonceName' => $this->gateway->wp_nonce_name,
      'wpNonce'     => $this->gateway->wp_nonce,
      'isTestMode'  => $this->gateway->enable_test_mode,
      'hasAPIKeys'  => $this->gateway->has_api_keys,
      'supports'    => array_filter($this->gateway->supports, [$this->gateway, 'supports'])
    ];
  }
}
