<?php

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

/**
 * Florbloom Cart Builder — Live Renderer
 *
 * Architecture:
 * Top          -> full width
 * Cart Details -> Cart Products left, remaining detail blocks right
 * Body         -> full width
 *
 * Mobile:
 * all regions collapse into one vertical flow.
 */


/* =========================================================
 * LOAD MODULAR CART RENDERERS
 * ====================================================== */

$florbloom_cart_heading_renderer =
	__DIR__ . '/cart-heading.php';

if (
	file_exists(
		$florbloom_cart_heading_renderer
	)
) {
	require_once
		$florbloom_cart_heading_renderer;
}


$florbloom_cart_products_renderer =
	__DIR__ . '/cart-products.php';

if (
	file_exists(
		$florbloom_cart_products_renderer
	)
) {
	require_once
		$florbloom_cart_products_renderer;
}


$florbloom_cart_delivery_details_renderer =
	__DIR__ . '/cart-delivery-details.php';

if (
	file_exists(
		$florbloom_cart_delivery_details_renderer
	)
) {
	require_once
		$florbloom_cart_delivery_details_renderer;
}


$florbloom_cart_total_price_renderer =
	__DIR__ . '/cart-total-price.php';

if (
	file_exists(
		$florbloom_cart_total_price_renderer
	)
) {
	require_once
		$florbloom_cart_total_price_renderer;
}


$florbloom_cart_coupon_renderer =
	__DIR__ . '/cart-coupon.php';

if (
	file_exists(
		$florbloom_cart_coupon_renderer
	)
) {
	require_once
		$florbloom_cart_coupon_renderer;
}


/* =========================================================
 * DEFAULT BLOCKS
 * ====================================================== */

if (
	! function_exists(
		'florbloom_cart_live_default_blocks'
	)
) {

	function florbloom_cart_live_default_blocks() {

		return array(

			array(
				'id'       => 'default_heading',
				'type'     => 'heading_subheading',
				'settings' => array(
					'enabled'    => 'yes',
					'region'     => 'top',
					'heading'    => 'Your Cart',
					'subheading' =>
						'Review your items before checkout.',
				),
			),

			array(
				'id'       => 'default_cart_products',
				'type'     => 'cart_products',
				'settings' => array(
					'enabled' => 'yes',
					'region'  => 'cart_details',
				),
			),

		);
	}
}


/* =========================================================
 * KNOWN BLOCK TYPES
 * ====================================================== */

if (
	! function_exists(
		'florbloom_cart_live_known_type'
	)
) {

	function florbloom_cart_live_known_type(
		$type
	) {

		return in_array(
			$type,
			array(
				'heading_subheading',
				'cart_products',
				'delivery_details',
				'total_price',
				'coupon',
			),
			true
		);
	}
}


/* =========================================================
 * REGION NORMALIZATION
 * ====================================================== */

if (
	! function_exists(
		'florbloom_cart_live_normalize_region'
	)
) {

	function florbloom_cart_live_normalize_region(
		$region
	) {

		$region =
			sanitize_key(
				(string) $region
			);

		return in_array(
			$region,
			array(
				'top',
				'cart_details',
				'body',
			),
			true
		)
			? $region
			: 'cart_details';
	}
}


/* =========================================================
 * DEVICE VISIBILITY
 * ====================================================== */

if (
	! function_exists(
		'florbloom_cart_live_visibility_attributes'
	)
) {

	function florbloom_cart_live_visibility_attributes(
		$settings
	) {

		$desktop =
			isset(
				$settings['show_desktop']
			)
				? $settings['show_desktop']
				: 'yes';

		$tablet =
			isset(
				$settings['show_tablet']
			)
				? $settings['show_tablet']
				: 'yes';

		$mobile =
			isset(
				$settings['show_mobile']
			)
				? $settings['show_mobile']
				: 'yes';

		return array(
			'desktop' =>
				'yes' === $desktop
					? 'yes'
					: 'no',

			'tablet' =>
				'yes' === $tablet
					? 'yes'
					: 'no',

			'mobile' =>
				'yes' === $mobile
					? 'yes'
					: 'no',
		);
	}
}


/* =========================================================
 * MAIN CART PAGE RENDERER
 * ====================================================== */

if (
	! function_exists(
		'florbloom_render_cart_page_live'
	)
) {

	function florbloom_render_cart_page_live() {

		if ( ! function_exists( 'WC' ) ) {
			return '';
		}


		if (
			function_exists( 'wc_load_cart' ) &&
			! WC()->cart
		) {
			wc_load_cart();
		}


		if ( ! WC()->cart ) {
			return '';
		}


		$blocks =
			get_option(
				'_florbloom_cart_builder_blocks',
				array()
			);


		if (
			! is_array( $blocks ) ||
			empty( $blocks )
		) {
			$blocks =
				florbloom_cart_live_default_blocks();
		}


		$top_blocks =
			array();

		$cart_detail_blocks =
			array();

		$body_blocks =
			array();


		foreach (
			$blocks
			as $block
		) {

			if (
				! is_array( $block ) ||
				empty( $block['type'] )
			) {
				continue;
			}


			$type =
				sanitize_key(
					$block['type']
				);


			if (
				! florbloom_cart_live_known_type(
					$type
				)
			) {
				continue;
			}


			$settings =
				isset( $block['settings'] ) &&
				is_array(
					$block['settings']
				)
					? $block['settings']
					: array();


			if (
				isset(
					$settings['enabled']
				) &&
				'yes' !==
					$settings['enabled']
			) {
				continue;
			}


			$region =
				florbloom_cart_live_normalize_region(
					$settings['region']
						?? 'cart_details'
				);


			$block['settings'] =
				$settings;

			$block['settings']['region'] =
				$region;


			if ( 'top' === $region ) {

				$top_blocks[] =
					$block;

			} elseif ( 'body' === $region ) {

				$body_blocks[] =
					$block;

			} else {

				$cart_detail_blocks[] =
					$block;
			}
		}


		/*
		 * Cart Details desktop/tablet behavior:
		 * Cart Products -> left
		 * Remaining blocks -> right
		 */
		$product_blocks =
			array();

		$detail_info_blocks =
			array();


		foreach (
			$cart_detail_blocks
			as $block
		) {

			if (
				'cart_products' ===
				sanitize_key(
					$block['type']
				)
			) {

				$product_blocks[] =
					$block;

			} else {

				$detail_info_blocks[] =
					$block;
			}
		}


		$cart_is_empty =
			WC()->cart->is_empty();


		ob_start();

		?>

		<main
			class="florbloom-cart-builder-live"
			data-cart-empty="<?php
				echo $cart_is_empty
					? 'yes'
					: 'no';
			?>"
		>

			<div class="florbloom-cart-builder-live__container">


				<?php
				/*
				 * =================================================
				 * TOP REGION
				 * =================================================
				 */
				if ( ! empty( $top_blocks ) ) :
					?>

					<section
						class="florbloom-cart-builder-live__top"
						data-cart-region="top"
					>

						<?php

						foreach (
							$top_blocks
							as $block
						) {

							echo florbloom_render_cart_live_block(
								$block
							); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						}

						?>

					</section>

				<?php endif; ?>


				<?php if ( $cart_is_empty ) : ?>

					<?php
					/*
					 * Until an Empty Cart block is created,
					 * keep a safe premium fallback.
					 */
					?>

					<section class="florbloom-cart-builder-live__empty">

						<div class="florbloom-cart-builder-live__empty-card">

							<div class="florbloom-cart-builder-live__empty-icon">
								♡
							</div>

							<h2>
								Your cart is empty
							</h2>

							<p>
								Add something beautiful and come back
								when you are ready.
							</p>

							<a
								href="<?php
									echo esc_url(
										function_exists(
											'wc_get_page_permalink'
										)
											? wc_get_page_permalink(
												'shop'
											)
											: home_url(
												'/'
											)
									);
								?>"
							>
								Continue Shopping
							</a>

						</div>

					</section>

				<?php else : ?>


					<?php
					/*
					 * =================================================
					 * CART DETAILS REGION
					 * =================================================
					 */
					if (
						! empty(
							$product_blocks
						) ||
						! empty(
							$detail_info_blocks
						)
					) :
						?>

						<section
							class="florbloom-cart-builder-live__details<?php
								echo empty(
									$detail_info_blocks
								)
									? ' has-products-only'
									: '';
							?>"
							data-cart-region="cart_details"
						>


							<?php if (
								! empty(
									$product_blocks
								)
							) : ?>

								<div
									class="florbloom-cart-builder-live__products-column"
								>

									<?php

									foreach (
										$product_blocks
										as $block
									) {

										echo florbloom_render_cart_live_block(
											$block
										); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
									}

									?>

								</div>

							<?php endif; ?>


							<?php if (
								! empty(
									$detail_info_blocks
								)
							) : ?>

								<div
									class="florbloom-cart-builder-live__info-column"
								>

									<?php

									foreach (
										$detail_info_blocks
										as $block
									) {

										echo florbloom_render_cart_live_block(
											$block
										); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
									}

									?>

								</div>

							<?php endif; ?>


						</section>

					<?php endif; ?>

				<?php endif; ?>


				<?php
				/*
				 * =================================================
				 * BODY REGION
				 * =================================================
				 */
				if ( ! empty( $body_blocks ) ) :
					?>

					<section
						class="florbloom-cart-builder-live__body"
						data-cart-region="body"
					>

						<?php

						foreach (
							$body_blocks
							as $block
						) {

							echo florbloom_render_cart_live_block(
								$block
							); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						}

						?>

					</section>

				<?php endif; ?>


			</div>

		</main>


		<style>

			/*
			 * Temporary structural CSS for the modular Cart Builder.
			 * Later this will move into cart-live.css when all blocks
			 * are completed.
			 */

			.florbloom-cart-builder-live {

				width: 100%;

				background: #ffffff;
			}


			.florbloom-cart-builder-live,
			.florbloom-cart-builder-live * {

				box-sizing: border-box;
			}


			.florbloom-cart-builder-live__container {

				width: min(
					100% - 40px,
					1280px
				);

				margin: 0 auto;

				padding: 28px 0 48px;
			}


			.florbloom-cart-builder-live__top,
			.florbloom-cart-builder-live__body {

				width: 100%;
			}


			.florbloom-cart-builder-live__details {

				display: grid !important;
				grid-template-columns:
					minmax(0, 65fr)
					minmax(0, 35fr) !important;

				align-items: start !important;

				column-gap: 10px !important;
				row-gap: 0 !important;

				width: 100% !important;
			}


			.florbloom-cart-builder-live__products-column,
			.florbloom-cart-builder-live__info-column {

				width: auto !important;
				min-width: 0 !important;

				flex: none !important;

				margin: 0 !important;
				padding: 0 !important;

				align-self: start !important;
			}


			.florbloom-cart-builder-live__details.has-products-only
			.florbloom-cart-builder-live__products-column {

				flex: 1 1 100%;
			}


			.florbloom-cart-builder-live__products-column,
			.florbloom-cart-builder-live__info-column {

				width: 100%;
				min-width: 0;

				align-self: start;
			}


			.florbloom-cart-builder-live__products-column
			> .florbloom-cart-builder-live__block,

			.florbloom-cart-builder-live__info-column
			> .florbloom-cart-builder-live__block {

				width: 100% !important;
				max-width: none !important;
				min-width: 0 !important;
			}


			/*
			 * Prevent margin-collapsing / theme block spacing from
			 * pushing one card lower than the other on desktop/tablet.
			 */
			@media ( min-width: 768px ) {

				.florbloom-cart-builder-live__products-column,
				.florbloom-cart-builder-live__info-column {

					margin: 0 !important;
					padding: 0 !important;
					align-self: flex-start !important;
				}


				.florbloom-cart-builder-live__products-column
				> .florbloom-cart-builder-live__block,

				.florbloom-cart-builder-live__info-column
				> .florbloom-cart-builder-live__block {

					display: flex !important;
					flex-direction: column !important;

					width: 100% !important;

					margin: 0 !important;
					margin-block: 0 !important;

					padding: 0 !important;
					padding-block: 0 !important;

					align-self: flex-start !important;
				}


				.florbloom-cart-builder-live__products-column
				> .florbloom-cart-builder-live__block:first-child
				> .florbloom-cart-products,

				.florbloom-cart-builder-live__info-column
				> .florbloom-cart-builder-live__block:first-child
				> .florbloom-cart-delivery-details,

				.florbloom-cart-builder-live__info-column
				> .florbloom-cart-builder-live__block:first-child
				> .florbloom-cart-total-price,

				.florbloom-cart-builder-live__info-column
				> .florbloom-cart-builder-live__block:first-child
				> .florbloom-cart-coupon {

					margin-top: 0 !important;
					margin-block-start: 0 !important;
				}
			}


			.florbloom-cart-builder-live__body {

				margin-top: 32px;
			}


			/*
			 * Block device visibility
			 */

			@media (
				min-width: 1101px
			) {

				.florbloom-cart-builder-live__block[
					data-show-desktop="no"
				] {

					display: none !important;
				}
			}


			@media (
				min-width: 768px
			) and (
				max-width: 1100px
			) {

				.florbloom-cart-builder-live__container {

					width:
						min(
							100% - 32px,
							1100px
						);

					padding-top: 24px;
				}


				.florbloom-cart-builder-live__details {

					display: grid !important;
					grid-template-columns:
						minmax(0, 65fr)
						minmax(0, 35fr) !important;

					align-items: start !important;

					column-gap: 10px !important;
					row-gap: 0 !important;
				}


				.florbloom-cart-builder-live__products-column,
				.florbloom-cart-builder-live__info-column {

					width: auto !important;
					min-width: 0 !important;

					flex: none !important;
				}


				.florbloom-cart-builder-live__block[
					data-show-tablet="no"
				] {

					display: none !important;
				}
			}


			@media (
				max-width: 767px
			) {

				.florbloom-cart-builder-live__container {

					width:
						min(
							100% - 24px,
							767px
						);

					padding:
						18px 0 32px;
				}


				/*
				 * Mobile = one vertical flow.
				 */
				.florbloom-cart-builder-live__details {

					display: grid !important;
					grid-template-columns:
						minmax(0, 1fr) !important;

					gap: 0 !important;
				}


				.florbloom-cart-builder-live__products-column,
				.florbloom-cart-builder-live__info-column {

					width: 100% !important;
					min-width: 0 !important;

					flex: none !important;
				}


				.florbloom-cart-builder-live__body {

					margin-top: 22px;
				}


				.florbloom-cart-builder-live__block[
					data-show-mobile="no"
				] {

					display: none !important;
				}
			}


			/*
			 * Empty cart fallback
			 */

			.florbloom-cart-builder-live__empty-card {

				max-width: 620px;

				margin: 24px auto;

				padding: 42px 24px;

				text-align: center;

				background: #ffffff;

				border:
					1px solid
					#eee9f1;

				border-radius: 18px;

				box-shadow:
					0 8px 28px
					rgba(
						34,
						24,
						42,
						0.045
					);
			}


			.florbloom-cart-builder-live__empty-icon {

				margin-bottom: 12px;

				color: #9871d0;

				font-size: 34px;
			}


			.florbloom-cart-builder-live__empty-card h2 {

				margin: 0 0 8px;

				color: #27232b;

				font-size: 24px;
			}


			.florbloom-cart-builder-live__empty-card p {

				margin: 0 0 18px;

				color: #726c76;
			}


			.florbloom-cart-builder-live__empty-card a {

				display: inline-flex;

				align-items: center;

				justify-content: center;

				min-height: 44px;

				padding: 10px 20px;

				color: #ffffff;

				background: #9871d0;

				border-radius: 999px;

				font-weight: 600;

				text-decoration: none;
			}

		</style>

		<?php

		return ob_get_clean();
	}
}


/* =========================================================
 * SINGLE BLOCK RENDERER
 * ====================================================== */

if (
	! function_exists(
		'florbloom_render_cart_live_block'
	)
) {

	function florbloom_render_cart_live_block(
		$block
	) {

		if (
			! is_array( $block ) ||
			empty( $block['type'] )
		) {
			return '';
		}


		$type =
			sanitize_key(
				$block['type']
			);


		if (
			! florbloom_cart_live_known_type(
				$type
			)
		) {
			return '';
		}


		$settings =
			isset( $block['settings'] ) &&
			is_array(
				$block['settings']
			)
				? $block['settings']
				: array();


		if (
			isset(
				$settings['enabled']
			) &&
			'yes' !==
				$settings['enabled']
		) {
			return '';
		}


		$visibility =
			florbloom_cart_live_visibility_attributes(
				$settings
			);


		ob_start();

		?>

		<section
			class="florbloom-cart-builder-live__block florbloom-cart-builder-live__block--<?php
				echo esc_attr(
					$type
				);
			?>"
			data-cart-block="<?php
				echo esc_attr(
					$type
				);
			?>"
			data-show-desktop="<?php
				echo esc_attr(
					$visibility[
						'desktop'
					]
				);
			?>"
			data-show-tablet="<?php
				echo esc_attr(
					$visibility[
						'tablet'
					]
				);
			?>"
			data-show-mobile="<?php
				echo esc_attr(
					$visibility[
						'mobile'
					]
				);
			?>"
		>

			<?php

			switch ( $type ) {

				case 'heading_subheading':

					if (
						function_exists(
							'florbloom_render_cart_heading'
						)
					) {

						florbloom_render_cart_heading(
							$settings
						);
					}

					break;


				case 'cart_products':

					if (
						function_exists(
							'florbloom_render_cart_products'
						)
					) {

						florbloom_render_cart_products(
							$settings
						);
					}

					break;


				case 'delivery_details':

					if (
						function_exists(
							'florbloom_render_cart_delivery_details'
						)
					) {

						florbloom_render_cart_delivery_details(
							$settings
						);
					}

					break;


				case 'total_price':

					if (
						function_exists(
							'florbloom_render_cart_total_price'
						)
					) {

						florbloom_render_cart_total_price(
							$settings
						);
					}

					break;


				case 'coupon':

					if (
						function_exists(
							'florbloom_render_cart_coupon'
						)
					) {

						florbloom_render_cart_coupon(
							$settings
						);
					}

					break;
			}

			?>

		</section>

		<?php

		return ob_get_clean();
	}
}
