<?php

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

class WC_Verifone_Site_Health_Info {
	private static $instance;

	/**
	 * Protected constructor to prevent creating a new instance of the
	 * *Singleton* via the `new` operator from outside of this class.
	 */
	protected function __construct() {
		add_filter( 'debug_information', [ $this, 'add_debug_info' ] );
		add_action( 'admin_footer', [ $this, 'inject_js' ] );
	}

	/**
	 * Returns the *Singleton* instance of this class.
	 *
	 * @return WC_Verifone_Site_Health Singleton The *Singleton* instance.
	 */
	public static function getInstance() {
		if (null === self::$instance) {
			self::$instance = new self();
		}

		return self::$instance;
    }

	public function add_debug_info( $info ) {
		$config = WC_Verifone_Config::getInstance();

		$style_code = $config->getStyleCode();
		$style_code = $style_code ? $style_code : esc_html__( 'Automatic', 'verifone-bluecommerce-for-woocommerce' );
		$style_code = $style_code !== 'webpay' ? $style_code : esc_html__( 'Automatic (new)', 'verifone-bluecommerce-for-woocommerce' );

		$info['wc-verifone'] = [
			'label'  => esc_html__( 'Verifone eCommerce', 'verifone-bluecommerce-for-woocommerce' ),
			'fields' => [
				'enabled' => [
					'label' => esc_html__( 'Enabled', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $this->is_enabled(),
				],
				'communication' => [
					'label' => esc_html__( 'Server communication', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $this->can_communicate() ? esc_html__( 'Working', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'Not working', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'environment' => [
					'label' => esc_html__( 'Environment', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->isLiveMode() ? esc_html__( 'Production', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'Test', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'merchant_agreement_code' => [
					'label' => esc_html__( 'EC ID (merchant code)', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getMerchantAgreement() ?: esc_html__( 'Not set', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'available_methods' => [
					'label' => esc_html__( 'Available payment methods', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => implode( ', ', array_keys( WC_Verifone_System::getPaymentMethodsOptions() ) ),
				],
				'display_method_logos' => [
					'label' => esc_html__( 'Display method logos', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->displayMethodLogos() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'skip_confirmation_page' => [
					'label' => esc_html__( 'Skip confirmation page', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->isSkipConfirmationPage() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'allow_to_save_cc' => [
					'label' => esc_html__( 'Allow to save credit cards', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->isAllowToSaveCC() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'payment_page_language' => [
					'label' => esc_html__( 'Payment page language', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getPaymentPageLanguage(),
				],
				'basket_item_sending' => [
					'label' => esc_html__( 'Basket item sending', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getBasketItemSending() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'combine_invoice_basket_items' => [
					'label' => esc_html__( 'Combine invoice basket items', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->isCombineInvoiceBasketItems() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'external_customer_id_field' => [
					'label' => esc_html__( 'External customer ID field', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getExternalCustomerIDField() ?: esc_html__( 'Not set', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'save_masked_pan_number' => [
					'label' => esc_html__( 'Save masked PAN number', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->isSaveMaskedPANNumber() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'style_code' => [
					'label' => esc_html__( 'Style code', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $style_code,
				],
				'validate_url' => [
					'label' => esc_html__( 'Validate URL', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->isValidateUrl() ? esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'pay_page_url_1' => [
					'label' => esc_html__( 'Pay page URL 1', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getData( 'pay_page_url_1' ) ?: esc_html__( 'Automatic', 'verifone-bluecommerce-for-woocommerce' ) . ' (' . $config->getPayPageUrl( 1 ) . ')',
				],
				'payment_delayed_url' => [
					'label' => esc_html__( 'Delayed success URL', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => WC_Verifone_Urls::getPaymentDelayedLink() ?: esc_html__( 'Not set', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'key_handling_mode' => [
					'label' => esc_html__( 'Key handling mode', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getKeyMode() ?: esc_html__( 'Automatic', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'public_key' => [
					'label' => esc_html__( 'Public key', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getShopPublicKey() ?: esc_html__( 'Not set', 'verifone-bluecommerce-for-woocommerce' ),
				],
				'private_key' => [
					'label' => esc_html__( 'Private key', 'verifone-bluecommerce-for-woocommerce' ),
					'value' => $config->getShopPrivateKey() ? esc_html__( 'Available', 'verifone-bluecommerce-for-woocommerce' ) : esc_html__( 'Not available', 'verifone-bluecommerce-for-woocommerce' ),
				],
			],
		];

		return $info;
	}

	public function inject_js() {
		// Only if site health info is current screen
		$screen = get_current_screen();
		if ( ! $screen || 'site-health' !== $screen->id ) {
			return;
		}

		/**
		 * Inject JS to format the public key field so that it wraps properly and is nicer to copy and paste.
		 */
		?>
		<script type="text/javascript">
			jQuery( document ).ready( function( $ ) {
				$('#health-check-accordion-block-wc-verifone td:contains("BEGIN PUBLIC KEY")').each(function () {
					var publicKey = $(this).text().trim();
					$(this).html('<pre style="white-space: pre-wrap; word-break: break-all;">' + publicKey + '</pre>');
				});
			} );
		</script>
		<?php
	}

	private function is_enabled() {
		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
		if ( isset( $available_gateways[ WC_VERIFONE_GATEWAY_ID ] ) ) {
			return esc_html__( 'Yes', 'verifone-bluecommerce-for-woocommerce' );
		}

		return esc_html__( 'No', 'verifone-bluecommerce-for-woocommerce' );
	}

	private function can_communicate() {
		/**
		 * If the site health check is not available, we assume communication is working
		 * for not bricking the functionalities of the plugin.
		 */
		if ( ! class_exists( 'WP_Site_Health' ) ) {
			return true;
		}

		$site_health = new WP_Site_Health();
		$tests = $site_health->get_tests();

		// Run the tests
		if ( isset( $tests['direct']['wc_verifone_server_communications'] ) ) {
			$test_live_mode_function = $tests['direct']['wc_verifone_server_communications']['test'];
			$test_live_mode_result = call_user_func( $test_live_mode_function );

			if ( 'good' === $test_live_mode_result['status'] ) {
				return true;
			}
		}

		return false;
	}
}

WC_Verifone_Site_Health_Info::getInstance();
