<?php
/**
 * FoodOrd Mobile Cart Class
 *
 * @package FoodOrd
 */

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

/**
 * Class Foodord_Mobile_Cart
 */
class Foodord_Mobile_Cart {

	/**
	 * Initialize the mobile cart.
	 */
	public function __construct() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_filter( 'body_class', array( $this, 'add_body_class' ) );
	}

	/**
	 * Check if current page should display mobile cart.
	 *
	 * @return bool True if mobile cart should be displayed.
	 */
	private function should_display_mobile_cart() {
		global $post;

		// Don't show on admin pages.
		if ( is_admin() ) {
			return false;
		}

		// Don't show on checkout page.
		if ( function_exists( 'is_checkout' ) && is_checkout() ) {
			return false;
		}

		// Check shortcode.
		if ( $post && has_shortcode( $post->post_content, 'foodord_products' ) ) {
			return true;
		}

		// Check FoodOrd blocks.
		if ( $post && function_exists( 'has_block' ) ) {
			if ( has_block( 'foodord/card', $post ) || has_block( 'foodord/products', $post ) ) {
				return true;
			}
		}

		if ( function_exists( 'has_shortcode' ) && has_shortcode( $post ? $post->post_content : '', 'foodord_card' ) ) {
			return true;
		}

		if ( ! empty( $GLOBALS['foodord_has_card_block'] ) || ! empty( $GLOBALS['foodord_has_card_shortcode'] ) || ! empty( $GLOBALS['foodord_has_products_block'] ) || ! empty( $GLOBALS['foodord_has_products_shortcode'] ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Enqueue mobile cart scripts and styles.
	 */
	public function enqueue_scripts() {
		if ( ! $this->should_display_mobile_cart() ) {
			return;
		}

		if ( function_exists( 'foodord_enqueue_touch_styles' ) ) {
			foodord_enqueue_touch_styles();
		}

		// Enqueue mobile cart CSS.
		wp_enqueue_style(
			'foodord-mobile-cart',
			FOODORD_URL . 'public/css/foodord-mobile-cart.css',
			array( 'foodord-touch' ),
			FOODORD_VERSION,
			'all'
		);

		// Enqueue mobile popover CSS.
		wp_enqueue_style(
			'mobile-popover',
			FOODORD_URL . 'public/css/mobile-popover.css',
			array( 'foodord-mobile-cart' ),
			FOODORD_VERSION,
			'all'
		);

		wp_enqueue_script(
			'foodord-cart-notes',
			FOODORD_URL . 'public/js/foodord-cart-notes.js',
			array(),
			FOODORD_VERSION,
			true
		);

		wp_localize_script(
			'foodord-cart-notes',
			'foodordCartNotesLabels',
			function_exists( 'foodord_get_cart_note_labels' ) ? foodord_get_cart_note_labels() : array()
		);

		wp_enqueue_script(
			'foodord-upsell-skeleton',
			FOODORD_URL . 'public/js/foodord-upsell-skeleton.js',
			array(),
			FOODORD_VERSION,
			true
		);

		// Enqueue mobile cart JavaScript.
		wp_enqueue_script(
			'foodord-swipe-dismiss',
			FOODORD_URL . 'public/js/foodord-swipe-dismiss.js',
			array(),
			FOODORD_VERSION,
			true
		);

		wp_enqueue_script(
			'foodord-mobile-cart',
			FOODORD_URL . 'public/js/foodord-mobile-cart.js',
			array( 'foodord-cart-notes', 'foodord-upsell-skeleton', 'foodord-swipe-dismiss' ),
			FOODORD_VERSION,
			true
		);

		wp_localize_script(
			'foodord-mobile-cart',
			'foodordMobileCartI18n',
			array(
				'checkout'         => __( 'Checkout', 'foodord' ),
				'viewCart'         => __( 'View Cart', 'foodord' ),
				'yourCart'         => __( 'Your order', 'foodord' ),
				'subtotal'         => __( 'Subtotal', 'foodord' ),
				'total'            => __( 'Total', 'foodord' ),
				'deliveryFee'      => __( 'Delivery Fee', 'foodord' ),
				'emptyCartText'    => __( 'Your cart is empty', 'foodord' ),
				'emptyCartSubtext' => __( 'Add items to get started', 'foodord' ),
				'closeCart'        => __( 'Close cart', 'foodord' ),
				'itemsInCart'      => __( 'Items in cart', 'foodord' ),
				/* translators: %s: Product name */
				'editItem'         => __( 'Edit %s', 'foodord' ),
				/* translators: %1$s: remaining amount. */
				'minOrderNotice'   => __( 'Add %1$s more to reach the minimum order.', 'foodord' ),
				'upsellsEnabled'   => 'yes' === get_option( 'foodord_enable_modal_upsells', 'no' ),
			)
		);

		// Enqueue mobile popover JavaScript.
		wp_enqueue_script(
			'mobile-popover',
			FOODORD_URL . 'public/js/mobile-popover.js',
			array( 'jquery', 'foodord-mobile-cart' ),
			FOODORD_VERSION,
			true
		);
	}

	/**
	 * Add body class when mobile cart should be available.
	 *
	 * @param array $classes Body classes.
	 * @return array Modified body classes.
	 */
	public function add_body_class( $classes ) {
		if ( $this->should_display_mobile_cart() ) {
			$classes[] = 'foodord-mobile-cart-enabled';
		}
		return $classes;
	}
}
