<?php
/**
 * The Credorax_CC_Pay class.
 * This registers the Credorax payment gateway.
 *
 * If the Credorax plugin is updated, do the following:
 * 1. Copy the content of its assets and includes folder to their respective folders in /vendor-included/Credorax folder of this plugin.
 * 2. Copy the content of its /uninstall.php file to /vendor-included/Credorax/uninstall.php of this plugin.
 * 3. Copy ONLY the WC_Credorax class from the woocommerce-gateway-credorax.php file to /vendor-included/Credorax/WC_Credorax.php of this plugin.
 * 4. Make class WC_Credorax_Gateway implement Interface_Flo_WC_Payment_Gateway
 * 5. Set constant GATEWAY_ID from Shared Payment_Methods::NMC_CREDORAX in vendor-included/credorax/includes/Gateway.php.
 * 6. Update function getPostParam in WC_Credorax_Gateway to not use filter_var() for block checkout,
 * since it won't read payment data manually added to $_POST. Also make sure to keep the key update for new credit cards.
 * 7. Remove the requirement of 'g1' in response, and implement custom check in WC_Credorax_Model_Response_Payment_AuthTokenization and WC_Credorax_Model_Response_Payment_SaleTokenization. Method getRequiredResponseDataKeys() is removed, and if statement in processResponseData() is added.
 * 8. Make WC_Credorax methods __clone and __wakeup public and add wc_doing_it_wrong() instead, to prevent warnings.
 * 9. Add settings field Sub Merchant ID and include it in API requests if it is set.
 *     - Update vendor-included\credorax\includes\Source\Settings.php
 *          - Add fields for sub_merchant and sandbox_sub_merchant
 *     - Update vendor-included\credorax\assets\js\credorax-settings.js
 *          - Add selectors for new sub merchant fields in var CredoraxFieldSelectors
 *          - Update updateModeCredentialsFields() to show/hide new sub merchant fields
 *     - Add function to get sub merchant field in vendor-included\credorax\includes\Model\Config.php (look at getMerchantId())
 *     - update vendor-included\credorax\includes\Model\AbstractRequest.php to include sub merchant in request, in method getParams()
 * 10. Look for any updates in assets/js/credorax.js and update assets/src/gateway-blocks/Components/Credorax/CredoraxContent.js accordingly.
 * 11. Add Northmill branding in WC_Credorax_Gateway::payment_fields())
 * 12. Update translations
 *     - run `lando make-shift4-pot` to update pot file, and then update po files.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways;

use Flo_Checkout\Gateways;
use Flo_Checkout\Gateways\Helpers\Gateway_Helpers;
use Flo_Checkout\Gateways\Helpers\Gateway_Logger_Trait;
use Flo_Checkout\Gateways\Helpers\Interface_Flo_WC_Payment_Gateway;
use Flo_Checkout\Shared;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Data\Payment_Methods;
use WC_Credorax;
use WC_Credorax_Gateway;
use WC_Payment_Gateways;

/**
 * Class Credorax_CC_Pay
 *
 * The Credorax_CC_Pay class provides public functionality for the plugin.
 */
class Credorax_CC_Pay implements Interface_Flo_WC_Payment_Gateway {
	use Gateway_Logger_Trait;

	/**
	 * Get Gateway ID.
	 *
	 * @return string
	 */
	public static function get_id(): string {
		return WC_Credorax_Gateway::GATEWAY_ID;
	}

	/**
	 * Whether the gateway supports blocks.
	 *
	 * @return bool
	 */
	public static function has_block_support(): bool {
		return true;
	}
	/**
	 * Set up the public functionality of the plugin.
	 *
	 * This method is responsible for loading the plugin's text domain and initializing the main plugin instance.
	 *
	 * @return void
	 */
	public static function public_setup(): void {
		if ( ! class_exists( 'WC_Credorax' ) ) {
			define( 'WC_CREDORAX_PATH', Shared::instance()->package_info->paths->get_path() . 'vendor-included/credorax/' );
			define( 'WC_CREDORAX_ASSETS_URL', Shared::instance()->package_info->paths->get_url() . 'vendor-included/credorax/assets/' );

			add_action( 'wc_payment_gateways_initialized', array( self::class, 'customize_gateway_name' ) );
			add_filter( 'wc_credorax_source_settings', array( self::class, 'customize_settings_defaults' ) );
			add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );

			require_once WC_CREDORAX_PATH . 'WC_Credorax.php';
			WC_Credorax::get_instance();
		}
	}


	/**
	 * Customize the gateway name.
	 *
	 * This method is responsible for customizing the name and description of the "credorax" payment gateway.
	 *
	 * @param WC_Payment_Gateways $payment_gateways The WooCommerce payment gateways instance.
	 *
	 * @return void
	 */
	public static function customize_gateway_name( WC_Payment_Gateways $payment_gateways ): void {
		foreach ( $payment_gateways->payment_gateways as $gateway ) {
			if ( Payment_Methods::NMC_SHIFT4_CARD === $gateway->id ) {
				$gateway->method_title       = __( 'Northmill Checkout - Finaro Pay', 'flo-checkout-plugin' );
				$gateway->method_description = __( 'Accept card payments', 'flo-checkout-plugin' );
			}
		}
	}

	/**
	 * Customize the default settings fields.
	 *
	 * This method allows customization of the default settings fields by modifying the $settings_fields array.
	 * It updates the 'title' and 'description' fields to have the specified default values.
	 *
	 * @param array $settings_fields An array containing the default settings fields.
	 *
	 * @return array The modified $settings_fields array with the updated default values.
	 */
	public static function customize_settings_defaults( array $settings_fields ): array {
		$settings_fields['title']['default']       = __( 'Card Payment', 'flo-checkout-plugin' );
		$settings_fields['description']['default'] = __( 'Pay with your credit card', 'flo-checkout-plugin' );
		return $settings_fields;
	}

	/**
	 * Load credit card form scripts.
	 */
	public static function enqueue_scripts(): void {
		if ( is_checkout() ) {

			$gateway_classes = Gateways::get_gateway_classes();
			$gateway_class   = $gateway_classes['finaro'];

			if ( ! Gateway_Helpers::is_gateway_implementing_flo_interface( $gateway_class ) ) {
				return;
			}

			/**
			 * Instance of class implementing Interface_Flo_WC_Payment_Gateway.
			 *
			 * @var $gateway_class Interface_Flo_WC_Payment_Gateway
			 */

			$gateway_id = $gateway_class::get_id();

			$gateways = WC()->payment_gateways->payment_gateways();
			if ( isset( $gateways[ $gateway_id ] ) ) {
				$gateway = $gateways[ $gateway_id ];

				/**
				 * Instance of WC_Credorax_Gateway.
				 *
				 * @var $gateway WC_Credorax_Gateway
				 */

				if ( $gateway->is_available() ) {
					wp_enqueue_script( 'wc-credit-card-form' );

					if ( $gateway->supports( 'tokenization' ) && $gateway->save_cards ) {
						$gateway->tokenization_script();
					}
				}
			}
		}
	}
}
