<?php
/**
 * PeachPay Square payment integration.
 *
 * @package PeachPay
 */

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

/**
 * PeachPay Square payment integration.
 */
final class PeachPay_Square_Integration {
	use PeachPay_Payment_Integration;

	/**
	 * Should the extension load?
	 */
	public static function should_load() {
		return true;
	}

	/**
	 * Is the extension enabled?
	 */
	public static function enabled() {
		return true;
	}

	/**
	 * Loads required files for the Square integration.
	 *
	 * @param boolean $enabled If the extension is enabled.
	 */
	private function includes( $enabled ) {
		require_once PEACHPAY_ABSPATH . 'core/payments/square/class-peachpay-square-credentials.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/hooks.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/functions.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/routes/square-webhook.php';

		if ( is_admin() ) {
			require_once PEACHPAY_ABSPATH . 'core/payments/square/admin/class-peachpay-admin-square-integration.php';
		}
	}

	/**
	 * Runs code after all plugins are loaded. Before WC init.
	 *
	 * @param boolean $enabled If the extension is enabled.
	 */
	private function plugins_loaded( $enabled ) {
		// https://github.com/woocommerce/woocommerce/wiki/Payment-Token-API
		require_once PEACHPAY_ABSPATH . 'core/payments/square/tokens/class-wc-payment-token-peachpay-square-card.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/utils/class-peachpay-square-error.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/utils/class-peachpay-square-rest-client.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/utils/class-peachpay-square.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/utils/class-peachpay-square-order-data.php';
	}

	/**
	 * Loads Square payment gateways after WooCommerce is initialized.
	 *
	 * @param boolean $enabled If the extension is enabled.
	 */
	protected function woocommerce_init( $enabled ) {
		require_once PEACHPAY_ABSPATH . 'core/payments/square/abstract/class-peachpay-square-payment-gateway.php';

		require_once PEACHPAY_ABSPATH . 'core/payments/square/gateways/class-peachpay-square-card-gateway.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/gateways/class-peachpay-square-applepay-gateway.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/gateways/class-peachpay-square-googlepay-gateway.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/gateways/class-peachpay-square-ach-gateway.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/gateways/class-peachpay-square-afterpay-gateway.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/gateways/class-peachpay-square-cashapp-gateway.php';

		$this->payment_gateways[] = 'PeachPay_Square_Card_Gateway';
		$this->payment_gateways[] = 'PeachPay_Square_ApplePay_Gateway';
		$this->payment_gateways[] = 'PeachPay_Square_GooglePay_Gateway';
		$this->payment_gateways[] = 'PeachPay_Square_ACH_Gateway';
		$this->payment_gateways[] = 'PeachPay_Square_Afterpay_Gateway';
		$this->payment_gateways[] = 'PeachPay_Square_Cashapp_Gateway';
	}

	/**
	 * Callback for registering PeachPay Square payment method support for WooCommerce blocks.
	 */
	protected function woocommerce_blocks_loaded() {
		require_once PEACHPAY_ABSPATH . 'core/payments/square/blocks/class-peachpay-square-afterpay-gateway-blocks-support.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/blocks/class-peachpay-square-applepay-gateway-blocks-support.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/blocks/class-peachpay-square-card-gateway-blocks-support.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/blocks/class-peachpay-square-googlepay-gateway-blocks-support.php';
		require_once PEACHPAY_ABSPATH . 'core/payments/square/blocks/class-peachpay-square-ach-gateway-blocks-support.php';

		add_action(
			'woocommerce_blocks_payment_method_type_registration',
			function ( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
				$payment_method_registry->register( new PeachPay_Square_Afterpay_Gateway_Blocks_Support() );
				$payment_method_registry->register( new PeachPay_Square_ApplePay_Gateway_Blocks_Support() );
				$payment_method_registry->register( new PeachPay_Square_Card_Gateway_Blocks_Support() );
				$payment_method_registry->register( new PeachPay_Square_GooglePay_Gateway_Blocks_Support() );
				$payment_method_registry->register( new PeachPay_Square_ACH_Gateway_Blocks_Support() );
			}
		);
	}

	/**
	 * Determines whether Square is connected (credentials configured).
	 *
	 * @return array|false Account data array if connected, false otherwise.
	 */
	public static function connected() {
		if ( ! PeachPay_Square_Credentials::is_configured() ) {
			return false;
		}

		$account = PeachPay_Square_Credentials::get_account();
		if ( empty( $account ) ) {
			return false;
		}

		return array(
			'connect_id'  => isset( $account['id'] ) ? $account['id'] : '',
			'location_id' => isset( $account['location_id'] ) ? $account['location_id'] : '',
			'country'     => isset( $account['country'] ) ? $account['country'] : '',
			'currency'    => isset( $account['currency'] ) ? $account['currency'] : '',
		);
	}

	/**
	 * Gets Square config for frontend usage.
	 *
	 * @return array Config array with public_key (Application ID).
	 */
	public static function config() {
		return array(
			'public_key' => PeachPay_Square_Credentials::get_publishable_key(),
		);
	}

	/**
	 * Gets Square account id (merchant_id).
	 *
	 * @return string The Square merchant ID.
	 */
	public static function connect_id() {
		return PeachPay_Square_Credentials::get_account_id();
	}

	/**
	 * Gets Square public key (Application ID).
	 *
	 * @return string The Application ID.
	 */
	public static function public_key() {
		return PeachPay_Square_Credentials::get_publishable_key();
	}

	/**
	 * Gets Square location ID (required by Web Payments SDK).
	 *
	 * @return string The location ID.
	 */
	public static function location_id() {
		return PeachPay_Square_Credentials::get_location_id();
	}

	/**
	 * Gets Square account country.
	 *
	 * @return string The country code.
	 */
	public static function connect_country() {
		return PeachPay_Square_Credentials::get_account_country();
	}

	/**
	 * Registers Square REST API endpoints.
	 *
	 * @param boolean $enabled If the extension is enabled.
	 */
	private function rest_api_init( $enabled ) {
		if ( ! $enabled ) {
			return;
		}

		register_rest_route(
			PEACHPAY_ROUTE_BASE,
			'/square/webhook',
			array(
				'methods'             => 'POST',
				'callback'            => 'peachpay_rest_api_square_webhook',
				'permission_callback' => '__return_true',
			)
		);
	}

	/**
	 * Used to detect if a gateway is a PeachPay Square gateway.
	 *
	 * @param string $id Payment gateway id.
	 */
	public static function is_payment_gateway( $id ) {
		return peachpay_starts_with( $id, 'peachpay_square_' );
	}

	/**
	 * Gets the PeachPay square test or live mode status.
	 *
	 * @param boolean|null $mode If the mode should override the global test mode. If not null a truthy value will indicate live mode where a falsy value will indicate test mode.
	 */
	public static function mode( $mode = 'detect' ) {
		if ( 'detect' === $mode ) {
			return ( peachpay_is_test_mode() || peachpay_is_local_development_site() || peachpay_is_staging_site() ) ? 'test' : 'live';
		}

		if ( 'live' === $mode ) {
			return 'live';
		} else {
			return 'test';
		}
	}
}
PeachPay_Square_Integration::instance();
