<?php
/**
 * Renders the PeachPay settings homepage.
 *
 * @package PeachPay
 */

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

/**
 * Renders the settings homepage.
 */
function peachpay_render_settings_homepage() {
	?>
	<div class='dashboard-container'>
		<div class='dashboard'>
			<?php
			peachpay_homepage_convesio_banner();
			peachpay_homepage_section( 'payment_methods' );
			peachpay_homepage_section( 'add_ons' );
			?>
		</div>
		<div class="peachpay-notices-container"></div>
	</div>
	<?php
}

/**
 * Renders a settings homepage section that consists of a heading, 'View more' button, and content body.
 *
 * @param string $homepage_section The homepage section to render.
 */
function peachpay_homepage_section( $homepage_section ) {
	?>
	<div class='dashboard-section dashboard-section-<?php echo esc_attr( str_replace( '_', '-', $homepage_section ) ); ?> pp-flex-col pp-gap-24'>
		<?php
		peachpay_homepage_heading( $homepage_section );
		peachpay_homepage_section_content( $homepage_section );
		?>
	</div>
	<?php
}

/**
 * Renders a settings homepage section heading with the section title and the 'View more' button.
 *
 * @param string $homepage_section The homepage section for which to render the heading.
 */
function peachpay_homepage_heading( $homepage_section ) {
	?>
	<div class='pp-flex-row pp-jc-sb'>
		<div class='pp-flex-row dashboard-section-title'>
			<?php
			peachpay_homepage_section_title( $homepage_section );
			?>
		</div>
		<?php
		if ( 'add_ons' !== $homepage_section ) {
			peachpay_homepage_section_view_more( $homepage_section );
		}
		?>
	</div>
	<?php
}

/**
 * Renders a settings homepage section's content.
 *
 * @param string $homepage_section The homepage section for which to render the content body.
 */
function peachpay_homepage_section_content( $homepage_section ) {
	?>
	<div class='dashboard-section-content'>
		<?php
		switch ( $homepage_section ) {
			case 'payment_methods':
				peachpay_homepage_payment_methods();
				break;
			case 'add_ons':
				peachpay_homepage_add_ons();
				break;
		}
		?>
	</div>
	<?php
}

/**
 * Populates the payment methods section cards.
 */
function peachpay_homepage_payment_methods() {
	//TODO(Followup): create filters for this function to promote loose coupling with different extensions.
	$data = array(
		array(
			'title'       => 'ConvesioPay',
			'heap_id'     => 'convesiopay',
			'hash'        => '#convesiopay',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/convesiopay/convesiopay.svg' ),
			'connection'  => (bool) PeachPay_ConvesioPay_Integration::connected(),
			'description' => 'The all-in-one payment solution to maximize revenue and streamline the payment process. Make test payments following these instructions.',
			'featured'    => true, // Make ConvesioPay a featured card (double width)
			'sub_logos'   => array(
				PeachPay::get_asset_url( '/img/marks/visa.svg' ),
				PeachPay::get_asset_url( '/img/marks/amex.svg' ),
				PeachPay::get_asset_url( '/img/marks/mastercard.svg' ),
				PeachPay::get_asset_url( '/img/marks/applepay-full.svg' ),
				PeachPay::get_asset_url( '/img/marks/google-pay.svg' ),
				PeachPay::get_asset_url( '/img/marks/bitcoin.svg' ),
				PeachPay::get_asset_url( '/img/marks/discover.svg' ),
			),
		),
		array(
			'title'       => 'NMI',
			'heap_id'     => 'nmi',
			'hash'        => '#nmi',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/nmi/nmi.png' ),
			'connection'  => (bool) peachpay_nmi_is_enabled(),
			'description' => 'NMI is a great fit for online merchants who want a flexible gateway with broad integration options and risk controls.',
			'featured'    => false,
			'sub_logos'   => array(
				PeachPay::get_asset_url( '/img/marks/cards.svg' ),
				PeachPay::get_asset_url( '/img/marks/applepay-full.svg' ),
			),
		),
		array(
			'title'       => 'Stripe',
			'heap_id'     => 'stripe',
			'hash'        => '#stripe',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/stripe.svg' ),
			'connection'  => (bool) PeachPay_Stripe_Integration::connected(),
			'description' => 'Stripe will give you the largest selection of global payment methods along with Apple Pay and Google Pay.',
			'sub_logos'   => array_map(
				function ( $gateway ) {
					return $gateway->get_icon_url( 'full' );
				},
				PeachPay_Stripe_Integration::get_payment_gateways()
			),
		),
		array(
			'title'       => 'PayPal',
			'heap_id'     => 'paypal',
			'hash'        => '#paypal',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/paypal.svg' ),
			'connection'  => (bool) PeachPay_PayPal_Integration::connected(),
			'description' => 'Accept payments from the largest number of countries with PayPal.',
			'sub_logos'   => array_map(
				function ( $gateway ) {
					return $gateway->get_icon_url( 'full' );
				},
				PeachPay_PayPal_Integration::get_payment_gateways()
			),
		),
		array(
			'title'       => 'Square',
			'heap_id'     => 'square',
			'hash'        => '#square',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/square.svg' ),
			'connection'  => peachpay_square_connected(),
			'description' => 'Connect your Square account to PeachPay to start accepting card payments, Apple Pay, and Google Pay instantly.',
			'sub_logos'   => array_map(
				function ( $gateway ) {
					return $gateway->get_icon_url( 'full' );
				},
				PeachPay_Square_Integration::get_payment_gateways()
			),
		),
		array(
			'title'       => 'Authorize.net',
			'heap_id'     => 'authnet',
			'hash'        => '#authnet',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/authnet.png' ),
			'connection'  => (bool) PeachPay_Authnet_Integration::connected(),
			'description' => 'Connect your Authorize.net account to PeachPay to start accepting card and US bank account payments.',
			'sub_logos'   => array_map(
				function ( $gateway ) {
					return $gateway->get_icon_url( 'full' );
				},
				PeachPay_Authnet_Integration::get_payment_gateways()
			),
		),
		array(
			'title'       => 'Purchase Order',
			'heap_id'     => 'purchase_order',
			'hash'        => '#peachpay',
			'main_logo'   => PeachPay::get_asset_url( '/img/marks/purchase_order.svg' ),
			'connection'  => 'yes' === array_filter(
				PeachPay_Payments_Integration::get_payment_gateways(),
				function ( $gateway ) {
					return 'peachpay_purchase_order' === $gateway->id;
				}
			)[0]->enabled,
			'description' => 'Accept purchase orders through PeachPay.',
			'sub_logos'   => null,
		),
	);
	foreach ( $data as $value ) {
		$featured_class = isset( $value['featured'] ) && $value['featured'] ? 'dashboard-section-card-featured' : '';
		?>
		<div class='dashboard-section-card <?php echo esc_attr( $featured_class ); ?>'>
			<?php if ( isset( $value['featured'] ) && $value['featured'] ) { ?>
				<!-- Featured card with subcard layout -->
				<div class='dashboard-section-card-subcards'>
					<!-- First subcard: Main payment method info -->
					<div class='dashboard-section-subcard dashboard-section-subcard-main'>
						<div class='image-container'>
							<img src='<?php echo esc_attr( $value['main_logo'] ); ?>'/>
						</div>
						<div class='title-button-row'>
							<div class='dashboard-section-card-title'>
								<?php echo esc_html( $value['title'] ); ?>
							</div>
							<a class='dashboard-card-button <?php echo esc_attr( $value['connection'] ? 'button-success-outlined-medium' : 'button-primary-filled-medium default-filled' ); ?>' href='<?php esc_url( PeachPay_Admin::admin_settings_url( 'peachpay', 'payment', '', $value['hash'] ) ); ?>'
								<?php
								if ( $value['connection'] ) {
									echo esc_attr( 'enabled' );
								}
								?>
								data-heap='<?php echo esc_html( $value['connection'] ? 'home_view_' : 'home_enable_' ) . esc_html( isset( $value['heap_id'] ) ? $value['heap_id'] : '' ); ?>'
							>
								<div></div>
								<div><?php echo esc_html( $value['connection'] ? 'Enabled' : 'Enable' ); ?></div>
								<div></div>
							</a>
						</div>
						<div class='dashboard-section-card-description'>
							<?php echo esc_html( $value['description'] ); ?>
						</div>
					</div>
					
					<!-- Second subcard: Offer wrapper -->
					<div class='dashboard-section-subcard dashboard-section-subcard-offer'>
						<div class="offerwrap">
							<div class="offer-tag">Featured</div>
						</div>
						<div class='offer-card-description'>
							<h3>Get paid faster with easily access to sales funds </h3>
							<p>Get started in as little as 10 minutes</p>
							<p>No minimum balance, no hidden fees</p>
							<p>100% online</p>
							<p>3.00% APY on savings</p>
						</div>
					</div>
				</div>
				
				<!-- Sub-logos in main card area -->
				<?php if ( $value['sub_logos'] ) { ?>
					<div class='sub-logos convesiopay-sub-logos'>
						<?php
						foreach ( $value['sub_logos'] as $src ) {
							?>
							<img src='<?php echo esc_attr( $src ); ?>'/>
							<?php
						}
						if ( 4 < count( $value['sub_logos'] ) ) {
							?>
							<div class='sub-logos-toggle'>See more</div>
							<?php
						}
						?>
					</div>
				<?php } ?>
			<?php } else { ?>
				<!-- Regular card layout -->
				<div class='image-container'>
					<img src='<?php echo esc_attr( $value['main_logo'] ); ?>'/>
				</div>
				<div class='title-button-row'>
					<div class='dashboard-section-card-title'>
						<?php echo esc_html( $value['title'] ); ?>
					</div>
					<a class='dashboard-card-button <?php echo esc_attr( $value['connection'] ? 'button-success-outlined-medium' : 'button-primary-filled-medium default-filled' ); ?>' href='<?php esc_url( PeachPay_Admin::admin_settings_url( 'peachpay', 'payment', '', $value['hash'] ) ); ?>'
						<?php
						if ( $value['connection'] ) {
							echo esc_attr( 'enabled' );
						}
						?>
						data-heap='<?php echo esc_html( $value['connection'] ? 'home_view_' : 'home_enable_' ) . esc_html( isset( $value['heap_id'] ) ? $value['heap_id'] : '' ); ?>'
					>
						<div></div>
						<div><?php echo esc_html( $value['connection'] ? 'Enabled' : 'Enable' ); ?></div>
						<div></div>
					</a>
				</div>
				<div class='dashboard-section-card-description'>
					<?php echo esc_html( $value['description'] ); ?>
				</div>
				<?php
				if ( $value['sub_logos'] ) {
					?>
					<div class='sub-logos'>
						<?php
						foreach ( $value['sub_logos'] as $src ) {
							?>
							<img src='<?php echo esc_attr( $src ); ?>'/>
							<?php
						}
						if ( 4 < count( $value['sub_logos'] ) ) {
							?>
							<div class='sub-logos-toggle'>See more</div>
							<?php
						}
						?>
					</div>
				<?php } ?>
			<?php } ?>
		</div>
		<?php
	}
}

/**
 * Populates the cards in the add-ons section.
 */
function peachpay_homepage_add_ons() {

	$data = array(
		array(
			'title'       => __( 'Bot Protection', 'peachpay-for-woocommerce' ),
			'heap_id'     => 'bot_protection',
			'main_image'  => peachpay_url( '/core/admin/assets/img/add-ons/bot.svg' ),
			'connection'  => 'yes' === PeachPay_Bot_Protection_Settings::get_setting( 'enabled' ),
			'description' => __( 'Block bots from spamming purchases by enabling reCAPTCHA v3 technology.', 'peachpay-for-woocommerce' ),
			'url'         => PeachPay_Admin::admin_settings_url( 'peachpay', 'bot_protection', 'settings', '', false ),
		),
		array(
			'title'       => 'Express Checkout',
			'heap_id'     => 'express_checkout',
			'main_image'  => peachpay_url( '/core/admin/assets/img/add-ons/checkout_window.svg' ),
			'connection'  => PeachPay_Express_Checkout::enabled(),
			'description' => 'Allow shoppers to buy from anywhere, not just the checkout page!',
			'url'         => PeachPay_Admin::admin_settings_url( 'peachpay', 'express_checkout', 'branding', '', false ),
		),
		array(
			'title'       => __( 'Address Autocomplete', 'peachpay-for-woocommerce' ),
			'heap_id'     => 'address_autocomplete',
			'main_image'  => peachpay_url( '/core/admin/assets/img/add-ons/address-autocomplete.svg' ),
			'connection'  => 'yes' === PeachPay_Address_Autocomplete_Settings::get_setting( 'enabled' ),
			'description' => __( 'Make it easier for your shoppers to enter their address by enabling address autocomplete.', 'peachpay-for-woocommerce' ),
			'url'         => PeachPay_Admin::admin_settings_url( 'peachpay', 'settings', 'address_autocomplete', '', false ),
		),
		array(
			'title'       => 'Currency Switcher',
			'heap_id'     => 'currency_switcher',
			'main_image'  => peachpay_url( '/core/admin/assets/img/add-ons/currency_switcher.svg' ),
			'connection'  => peachpay_get_settings_option( 'peachpay_currency_options', 'enabled' ),
			'description' => 'Sell in over 135 currencies with a single click, or customize it to fit your needs.',
			'url'         => PeachPay_Admin::admin_settings_url( 'peachpay', 'currency', '', '', false ),
		),
		array(
			'title'       => 'Field Editor',
			'heap_id'     => 'field_editor',
			'main_image'  => peachpay_url( '/core/admin/assets/img/add-ons/field_editor.svg' ),
			'connection'  => 1,
			'description' => 'Add, edit, or remove checkout fields. Choose from all types of fields, including text, multiple choice, drop downs, and more.',
			'url'         => PeachPay_Admin::admin_settings_url( 'peachpay', 'field', 'billing', '', false ),
		),
		array(
			'title'       => 'Recommended Products',
			'heap_id'     => 'recommended_products',
			'main_image'  => peachpay_url( '/core/admin/assets/img/add-ons/recommended_products.svg' ),
			'connection'  => peachpay_get_settings_option( 'peachpay_related_products_options', 'peachpay_related_enable' ),
			'description' => 'Show upsells to boost your store’s revenue.',
			'url'         => PeachPay_Admin::admin_settings_url( 'peachpay', 'related_products', '', '', false ),
		),
	);
	foreach ( $data as $value ) {
		?>
		<div class='dashboard-section-card'>
			<div class='image-container'>
				<img src='<?php echo esc_attr( $value['main_image'] ); ?>'/>
			</div>
			<div class='pp-flex-row pp-jc-sb'>
				<div class='dashboard-section-card-title'>
					<?php echo esc_html( $value['title'] ); ?>
				</div>
				<a class='dashboard-card-button <?php echo esc_attr( $value['connection'] ? 'button-success-outlined-medium' : 'button-primary-filled-medium default-filled' ); ?>' href='<?php echo esc_url( $value['url'] ); ?>'
					<?php
					if ( $value['connection'] ) {
						echo esc_attr( 'enabled' );
					}
					?>
					data-heap='<?php echo esc_html( $value['connection'] ? 'home_view_' : 'home_enable_' ) . esc_html( isset( $value['heap_id'] ) ? $value['heap_id'] : '' ); ?>'
				>
					<div></div>
					<div><?php echo esc_html( $value['connection'] ? 'Enabled' : 'Enable' ); ?></div>
					<div></div>
				</a>
			</div>
			<div class='dashboard-section-card-description'>
				<?php echo esc_html( $value['description'] ); ?>
			</div>
		</div>
		<?php
	}
}

/**
 * Generates escaped and translated text for the homepage section title.
 * This is a workaround for esc_html_e only accepting string literals.
 *
 * @param string $homepage_section The homepage section.
 */
function peachpay_homepage_section_title( $homepage_section ) {
	switch ( $homepage_section ) {
		case 'payment_methods':
			echo esc_html_e( 'Payment methods', 'peachpay-for-woocommerce' );
			break;
		case 'add_ons':
			echo esc_html_e( 'Add-ons', 'peachpay-for-woocommerce' );
			break;
	}
}

/**
 * Renders the 'View more' button for a homepage section.
 *
 * @param string $homepage_section The homepage section.
 */
function peachpay_homepage_section_view_more( $homepage_section ) {
	$page    = '';
	$tab     = '';
	$section = '';
	switch ( $homepage_section ) {
		case 'payment_methods':
			$page = 'peachpay';
			$tab  = 'payment';
			break;
	}
	?>
	<a class='pp-homepage-view-more pp-flex-row pp-gap-4 pp-ai-center' data-heap='<?php echo 'home_view_' . esc_html( $homepage_section ); ?>' href='<?php esc_url( PeachPay_Admin::admin_settings_url( $page, $tab, $section ) ); ?>'>
		<div class='pp-flex-row'>View more</div>
		<div class='pp-flex-row pp-chevron-right'></div>
	</a>
	<?php
}

/**
 * Renders the ConvesioConvert promotional banner.
 */
function peachpay_homepage_convesio_banner() {
	?>
	<div class='dashboard-section dashboard-section-convesio-banner'>
		<a href='https://convert.convesio.com/register' target='_blank' rel='noopener noreferrer'>
			<img 
				src='<?php echo esc_url( PeachPay::get_asset_url( '/img/ConvesioConvert-banner-version03-big.png' ) ); ?>' 
				alt='<?php echo esc_attr__( 'ConvesioConvert - 2X your conversion rate by personalizing your website for every customer. Try ConvesioConvert for 3 months free. Use code PEACHPAY', 'peachpay-for-woocommerce' ); ?>'
				class='convesio-banner-image'
				style='max-width: 100%; height: auto; width: 100%; border-radius: 8px;'
			/>
		</a>
	</div>
	<?php
}