<?php

/**
 * PeachPay gateway table.
 *
 * @var array $gateway_list A list of gateway instances to render.
 *
 * @package PeachPay
 */

defined('PEACHPAY_ABSPATH') || exit;

$convesiopay_dashboard_url                = function_exists( 'peachpay_convesiopay_dashboard_url' ) ? peachpay_convesiopay_dashboard_url() : 'https://convesiopay.com';
$convesiopay_payment_methods_url          = $convesiopay_dashboard_url . '/advanced-settings/payment-methods';

$convesiopay_apple_standard_cert_zip_url      = function_exists( 'peachpay_convesiopay_apple_pay_certificate_url' ) ? peachpay_convesiopay_apple_pay_certificate_url( false ) : '';
$convesiopay_apple_orchestration_cert_zip_url = function_exists( 'peachpay_convesiopay_apple_pay_certificate_url' ) ? peachpay_convesiopay_apple_pay_certificate_url( true ) : '';

foreach ($gateway_list as $gateway) : ?>
	<div class="gateway">
		<div class="info flex-col gap-12 w-100">
			<div class="list-item-heading gap-12">
				<?php
				$icon_url = $gateway->get_icon_url('full', 'color');
				if ($icon_url) {
					echo '<span style="gap:0.2rem;margin-left:0.4rem;align-items:center"><img style="max-height:25px;" data-gateway="' . esc_attr($gateway->id) . '" src="' . esc_url($icon_url) . '" alt="' . esc_attr($gateway->title) . '" /></span>';
				}
				?>
				<h4><?php echo esc_html($gateway->title); ?></h4>
				<div class="general-status <?php echo esc_attr(('yes' === $gateway->enabled && ! $gateway->needs_setup()) ? 'active' : 'inactive'); ?> flex-row gap-8">
					<div class="active">Active</div>
					<div class="inactive">Inactive</div>
				</div>
			</div>
			<div class="location-status <?php echo esc_attr(('yes' === $gateway->enabled && ! $gateway->needs_setup()) ? '' : 'hide'); ?>">
				<?php if ($gateway->id == 'peachpay_convesiopay_card' || $gateway->id == 'peachpay_convesiopay_btcpay' || $gateway->id == 'peachpay_convesiopay_applepay' || $gateway->id == 'peachpay_convesiopay_googlepay') { ?>
					<div class="location <?php echo 'express_checkout_only' !== $gateway->get_option('active_locations') ? 'active' : ''; ?>">
						Checkout page
					</div>
				<?php } else { ?>
					<div class="location <?php echo 'checkout_page_only' !== $gateway->get_option('active_locations') ? 'active' : ''; ?>">
						Express Checkout
					</div>
					<div class="location <?php echo 'express_checkout_only' !== $gateway->get_option('active_locations') ? 'active' : ''; ?>">
						Checkout page
					</div>
				<?php } ?>
			</div>
			<div class="description">
				<?php if ($gateway->needs_setup()) { ?>
					<span>
						<?php echo esc_html($gateway->method_description); ?>
					</span>
				<?php } else { ?>
					<div class="details">
						<?php
						$supported_currencies = $gateway->get_supported_currencies();
						$supported_countries  = $gateway->get_supported_countries();
						$columns              = array(
							array(
								'label'          => 'Currency availability',
								'key'            => $gateway->id . '_currency',
								'data'           => is_array($supported_currencies) ? implode(', ', $supported_currencies) : '',
								'no-data'        => is_null($supported_currencies) || 0 === count($supported_currencies),
								'no-restriction' => ! is_array($supported_currencies),
							),
							array(
								'label'          => 'Country availability',
								'key'            => $gateway->id . '_country',
								'data'           => is_array($supported_countries) ? implode(', ', $supported_countries) : '',
								'no-data'        => is_null($supported_countries) || 0 === count($supported_countries),
								'no-restriction' => ! is_array($supported_countries),
							),
							array(
								'label'          => 'Minimum charge',
								'key'            => $gateway->id . '_min',
								'data'           => '$' . $gateway->get_minimum_charge(),
								'no-data'        => false,
								'no-restriction' => ! is_numeric($gateway->get_minimum_charge()),
							),
							array(
								'label'          => 'Maximum charge',
								'key'            => $gateway->id . '_max',
								'data'           => $gateway->get_maximum_charge(),
								'no-data'        => false,
								'no-restriction' => INF === $gateway->get_maximum_charge(),
							),
						);
						?>
						<?php foreach ($columns as $column) { ?>
							<div class="flex-col gap-4">
								<h4><?php echo esc_html($column['label']); ?></h4>
								<div class="see-more-wrap">
									<input type="checkbox" class="see-more-state hide" id="<?php echo esc_attr($column['key']); ?>-list" />
									<div class="flex-col gap-4" style="flex-direction: column-reverse;">
										<label for="<?php echo esc_attr($column['key']); ?>-list" class="see-more-trigger hide"></label>
										<div class="see-more-target" style="max-height: 50px;">
											<p>
												<?php
												if ($column['no-restriction']) {
													echo esc_html_e('Not restricted', 'peachpay-for-woocommerce');
												} elseif ($column['no-data']) {
													echo esc_html_e('Not available', 'peachpay-for-woocommerce');
												} else {
													echo esc_html($column['data']);
												}
												?>
											</p>
											<div class="fade-bottom"></div>
										</div>
									</div>
								</div>
							</div>
						<?php } ?>
					</div>
				<?php } ?>
			</div>
			<?php
			if ('peachpay_convesiopay_applepay' === $gateway->id) { ?>
				<div class="gateway-setup-card applepay">
					<p><strong>Step 1:</strong> Download the ZIP for <strong>your account type</strong>.</p>
					<ul class="gateway-setup-list gateway-setup-list-nested">
						<li>
							<p><strong>Standard ConvesioPay merchant (non-OL)</strong> — <a class="gateway-setup-link" href="<?php echo esc_url( $convesiopay_apple_standard_cert_zip_url ); ?>">download ZIP</a></p>
						</li>
						<li>
							<p><strong>Orchestration (OL) account</strong> — Orchestration Accounts, please use this certificate: <a class="gateway-setup-link" href="<?php echo esc_url( $convesiopay_apple_orchestration_cert_zip_url ); ?>">download ZIP</a></p>
						</li>
					</ul>
					<p><strong>Step 2:</strong> Put the domain association file on each domain and subdomain where you accept Apple Pay. <strong>The file name must be exactly</strong> <code>apple-developer-merchantid-domain-association</code> <strong>— Apple will not validate if you rename it</strong> (no <code>.txt</code> or other extension).</p>
					<div class="gateway-setup-applepay-path">
						<p class="gateway-setup-applepay-path-label">Use this path on your site (filename matches the last segment):</p>
						<p class="gateway-setup-code gateway-setup-code-emphasis">/.well-known/apple-developer-merchantid-domain-association</p>
						<p class="gateway-setup-note">Example full URL: <code>https://yourdomain.com/.well-known/apple-developer-merchantid-domain-association</code> — replace <code>yourdomain.com</code> with yours (use the same for each subdomain you need).</p>
					</div>
					<p><strong>The file must:</strong></p>
					<ul class="gateway-setup-list">
						<li>Be externally accessible</li>
						<li>Not be password protected</li>
						<li>Not be behind a proxy or redirect</li>
					</ul>
					<p><a class="gateway-setup-link" href="https://eu.adyen.link/.well-known/apple-developer-merchantid-domain-association">See an example of a working certificate file</a></p>
					<ul>
						<li><a class="gateway-setup-link" href="https://convesiopay.com/integrations/woo-commerce">Add ApplePay domains in the ConvesioPay Dashboard WooCommerce Integration Settings.</a> You must register all top-level domains and subdomains you want to take ApplePay payments for with this extension.</li>
					</ul>
					<hr class="gateway-setup-divider">
					<p class="gateway-setup-support"><em>💡 If you need assistance setting this up, please reach out to <a href="mailto:pay@convesio.com"><strong>ConvesioPay Support</strong></a></em></p>
				</div>
			<?php }
			if ('peachpay_convesiopay_googlepay' === $gateway->id) { ?>
				<div class="gateway-setup-card googlepay">
					<p><strong>Step 1:</strong> Go to <a class="gateway-setup-link" href="<?php echo esc_url($convesiopay_payment_methods_url); ?>"><strong>Advanced Settings -> Payment Methods</strong></a> in the ConvesioPay Dashboard.</p>
					<p><strong>Step 2:</strong> Enable <strong>Google Pay</strong>, open the dropdown, and select a Google Pay Merchant ID.</p>
					<p><strong>Step 3:</strong> If you do not have a Merchant ID yet, click <strong>Create</strong> and wait for it to become active.</p>
					<p class="gateway-setup-pending">Google Pay merchant IDs can take some time to activate. If your Google Pay merchant ID is listed as <strong>Pending</strong>, check back soon and make sure it is listed as <strong>Active</strong> before using it to process payments.</p>
					<p><strong>Google Pay domains</strong></p>
					<p>You must register all main domains and subdomains where you want to accept payments with Google Pay through the WooCommerce extension.</p>
					<p class="gateway-setup-code">https://abc.com</p>
					<p>Add your domain and click <strong>Register a Google Pay domain</strong>. Once the domain is registered successfully, Google Pay will be available to shoppers on that domain.</p>
					<hr class="gateway-setup-divider">
					<p class="gateway-setup-support"><em>💡 If you need assistance setting this up, please reach out to <a href="mailto:pay@convesio.com"><strong>ConvesioPay Support</strong></a></em></p>
				</div>
			<?php }
			// for btcpay
			if (('Crypto' === $gateway->title) && ! $gateway->needs_setup()) {
				$url = 'https://convesiopay.com/integrations';
			?>
				<div class="gateway-info-row">
					<div class="pp-icon-info"></div>
					<span>
						<span>
							<?php
							// PHPCS:ignore
							echo sprintf(__('%s enabled but not showing? Ensure you configured it on', 'peachpay-for-woocommerce'), $gateway->title);
							?>
						</span>
						<a class="gateway-info-link"
							href="<?php echo esc_url($url); ?>"
							target="_blank">
							<?php
							// PHPCS:ignore
							echo sprintf(__('Convesiopay Integrations', 'peachpay-for-woocommerce'));
							?>
						</a>
					</span>
				</div>
			<?php
			}
			?>

		</div>
		<div class="buttons-container flex-col <?php echo esc_attr($gateway->needs_setup() ? 'needs-setup' : (('yes' === $gateway->enabled) ? 'enabled' : 'disabled')); ?>">
			<a class="setup-button button-primary-outlined-medium" data-heap="<?php echo 'setup_' . esc_html($gateway->id); ?>" href="<?php echo esc_url($gateway->get_settings_url()); ?>">
				<?php
				esc_html_e('Set up', 'peachpay-for-woocommerce');
				?>
				<span class="arrow-top-right"></span>
			</a>
			<button type="button" class="activate-button button-primary-outlined-medium" data-heap="<?php echo 'activate_' . esc_html($gateway->id); ?>" data-id="<?php echo esc_html($gateway->id); ?>" tabindex="0">
				<?php
				esc_html_e('Activate', 'peachpay-for-woocommerce');
				?>
				<span class="spinner"></span>
			</button>
			<a class="manage-button <?php echo esc_attr(('yes' === $gateway->enabled) ? 'button-primary-filled-medium default-filled' : 'button-primary-text-medium default-text'); ?>" data-heap="<?php echo 'manage_' . esc_html($gateway->id); ?>" href="<?php echo esc_url($gateway->get_settings_url()); ?>">
				<?php
				esc_html_e('Manage', 'peachpay-for-woocommerce');
				?>
				<span class="arrow-top-right"></span>
			</a>
			<button type="button" class="deactivate-button button-warning-text-medium default-text" data-heap="<?php echo 'deactivate_' . esc_html($gateway->id); ?>" data-id="<?php echo esc_html($gateway->id); ?>" tabindex="0">
				<?php
				esc_html_e('Deactivate', 'peachpay-for-woocommerce');
				?>
				<span class="spinner"></span>
			</button>
		</div>
	</div>
<?php endforeach; ?>