<?php
/**
 * PayPal Blocks support base class.
 *
 * @package PeachPay
 */

defined( 'PEACHPAY_ABSPATH' ) || exit;

/**
 * Base Blocks support for PayPal gateways.
 */
abstract class PeachPay_PayPal_Gateway_Blocks_Support extends \Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType {
	/**
	 * Payment method name/id/slug.
	 *
	 * @var string
	 */
	protected $name = '';

	/**
	 * Stored settings for the payment method.
	 *
	 * @var array
	 */
	protected $settings = array();

	/**
	 * Gateway instance.
	 *
	 * @var WC_Payment_Gateway|null
	 */
	protected $gateway;

	/**
	 * Initialize the payment method type.
	 */
	public function initialize() {
		$this->gateway  = peachpay_get_payment_gateway( $this->name );
		$this->settings = get_option( 'peachpay_' . $this->name . '_settings', array() );
	}

	/**
	 * Returns if this payment method should be active.
	 *
	 * @return bool
	 */
	public function is_active() {
		if ( ! $this->gateway || ! is_object( $this->gateway ) ) {
			return false;
		}

		return $this->gateway->is_available( true );
	}

	/**
	 * Returns an array of scripts/handles to be registered for this payment method.
	 *
	 * @return array
	 */
	public function get_payment_method_script_handles() {
		PeachPay::register_webpack_script(
			$this->name . '-blocks',
			'wordpress/' . $this->name . '-blocks',
			array(),
			true
		);

		return array( $this->name . '-blocks' );
	}

	/**
	 * Returns an array of key=>value pairs of data made available to the payment method script.
	 *
	 * @return array
	 */
	public function get_payment_method_data() {
		if ( ! $this->gateway || ! is_object( $this->gateway ) ) {
			return array();
		}

		return array(
			'paypal'            => array(
				'client_id'              => PeachPay_PayPal_Integration::client_id(),
				'merchant_id'            => PeachPay_PayPal_Integration::merchant_id(),
				'partner_attribution_id' => PeachPay_PayPal_Integration::partner_attribution_id(),
				'currency'               => get_woocommerce_currency(),
				'is_sandbox'             => peachpay_is_test_mode() || peachpay_is_local_development_site() || peachpay_is_staging_site(),
				'update_order_url'       => \WC_AJAX::get_endpoint( 'pp-paypal-update-order' ),
				'update_order_security'  => wp_create_nonce( 'peachpay-paypal-update-order' ),
				'approve_order_url'      => \WC_AJAX::get_endpoint( 'pp-paypal-approve-order' ),
			),
			'icon'              => $this->resolve_icon_url(),
			'title'             => $this->gateway->get_title(),
			'description'       => $this->gateway->description,
			'fallback_currency' => $this->gateway->get_fallback_currency(),
			'supports'          => array_filter( $this->gateway->supports, array( $this->gateway, 'supports' ) ),
			'button_style'      => array(
				'layout' => wc_string_to_bool( $this->gateway->get_option( 'paypal_card_tagline' ) ) ? 'vertical' : 'horizontal',
				'color'  => $this->gateway->get_option( 'paypal_button_color' ),
				'shape'  => $this->gateway->get_option( 'paypal_button_shape' ),
				'label'  => $this->gateway->get_option( 'paypal_button_label' ),
				'height' => intval( $this->gateway->get_option( 'paypal_button_height' ) ),
			),
		);
	}

	/**
	 * Resolve a usable icon url without assuming a specific background key.
	 *
	 * @return string
	 */
	protected function resolve_icon_url() {
		if ( empty( $this->gateway->icons ) || ! is_array( $this->gateway->icons ) ) {
			return '';
		}

		foreach ( array( 'small', 'full' ) as $size ) {
			if ( empty( $this->gateway->icons[ $size ] ) || ! is_array( $this->gateway->icons[ $size ] ) ) {
				continue;
			}

			$icon = reset( $this->gateway->icons[ $size ] );
			if ( is_string( $icon ) ) {
				return $icon;
			}
		}

		return '';
	}
}
