<?php
/**
 * Plugin's shortcodes.
 *
 * @package HBFWC/Shortcodes.
 */

namespace HBFWC;

defined( 'ABSPATH' ) || exit;

use function HBFWC\HBF_WC;

/**
 * Class Shortcode.
 */
class Shortcodes {
	/**
	 * Hook in method.
	 */
	public static function init() {
		$shortcodes = array(
			'hbfwc_availability' => __CLASS__ . '::availability',
			'hbfwc_search_form'  => __CLASS__ . '::search_form',
		);

		foreach ( $shortcodes as $hbfwc_shortcode => $function ) {
			add_shortcode( apply_filters( "{$hbfwc_shortcode}_shortcode_tag", $hbfwc_shortcode ), $function );
		}
	}

	/**
	 * Availability lists & filter.
	 */
	public static function availability() {
		if ( is_admin() && ! wp_doing_ajax() && ! defined('REST_REQUEST') ) {
			return '';
		}

		$query_args = self::set_query_args();

		ob_start();

		wc_get_template(
			'hbfwc/availability-check.php',
			array(
				'query_args' => $query_args,
			)
		);

		return ob_get_clean();
	}

	/**
	 * The search form.
	 *
	 * @param array $atts The shortcode atts.
	 */
	public static function search_form( $atts ) {
		$attrs = shortcode_atts(
			array(
				'page' => false,
			),
			$atts
		);

		if ( is_admin() && ! wp_doing_ajax() && ! defined('REST_REQUEST') ) {
			return '';
		}

		if ( ! $attrs['page'] ) {
			return '<div class="woocommerce-notices-wrapper"><ul class="woocommerce-error" role="alert" tabindex="-1"><li>' . esc_html__( 'Please provide the page ID or page path in the "Search results page" settings.', 'hotel-booking-for-woocommerce' ) . '</li></ul></div>';
		}

		$page = $attrs['page'];
		if ( ! is_numeric( $page ) ) {
			$page = get_page_by_path( $page );
			if ( ! $page ) {
				return '<div class="woocommerce-notices-wrapper"><ul class="woocommerce-error" role="alert" tabindex="-1"><li>' . esc_html__( 'Please provide the page ID or page path in the "Search results page" settings.', 'hotel-booking-for-woocommerce' ) . '</li></ul></div>';
			}
		} else {
			$page = absint( $page );
		}

		$link     = get_permalink( $page );
		$checkin  = wp_date( 'm/d/Y', hbfwc_default_checkin_date() );
		$checkout = wp_date( 'm/d/Y', hbfwc_default_checkin_date() + DAY_IN_SECONDS );
		$ci_date  = new \DateTime( wp_date( 'Y-m-d', hbfwc_default_checkin_date() ) );
		$co_date  = new \DateTime( wp_date( 'Y-m-d', hbfwc_default_checkin_date() + DAY_IN_SECONDS ) );

		$query_args = array(
			'checkin'       => $checkin,
			'checkout'      => $checkout,
			'rooms'         => 1,
			'checkin_date'  => $ci_date,
			'checkout_date' => $co_date,
		);

		ob_start();

		wc_get_template(
			'hbfwc/availability-search.php',
			array(
				'link'       => $link,
				'query_args' => $query_args,
			)
		);

		return ob_get_clean();
	}

	/**
	 * Set query args from URL query params.
	 *
	 * @return array
	 */
	private static function set_query_args(): array {
		if ( is_admin() ) {
			return array();
		}

		$timezone = wp_timezone();
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
		$checkin  = isset( $_GET['checkin'] ) ? sanitize_text_field( wp_unslash( $_GET['checkin'] ) ) : wp_date( 'm/d/Y', hbfwc_default_checkin_date() );
		$checkout = isset( $_GET['checkout'] ) ? sanitize_text_field( wp_unslash( $_GET['checkout'] ) ) : wp_date( 'm/d/Y', hbfwc_default_checkin_date() + DAY_IN_SECONDS );
		$adults   = isset( $_GET['adults'] ) ? absint( wp_unslash( $_GET['adults'] ) ) : '1';
		$children = isset( $_GET['children'] ) ? absint( wp_unslash( $_GET['children'] ) ) : '0';
		$ci_date  = new \DateTime( wp_date( 'Y-m-d', strtotime($checkin) ) );
		$co_date  = new \DateTime( wp_date( 'Y-m-d', strtotime($checkout) ) );
		$stay     = intval( $co_date->diff( $ci_date )->format( '%a' ) );

		$child_ages = isset( $_GET['children_age'] ) && is_array( $_GET['children_age'] ) ? array_map( 'absint', wp_unslash( $_GET['children_age'] ) ) : array();
		$rooms      = hbfwc_show_rooms_field() && isset( $_GET['rooms'] ) ? max( 1, absint( wp_unslash( $_GET['rooms'] ) ) ) : 1;
		// phpcs:enable

		return array(
			'checkin'       => $checkin,
			'checkout'      => $checkout,
			'adults'        => $adults,
			'children'      => $children,
			'rooms'         => $rooms,
			'checkin_date'  => $ci_date,
			'checkout_date' => $co_date,
			'stay'          => $stay,
			'child_ages'    => $child_ages,
		);
	}
}
