<?php

/**
 * Shortcodes
 */

namespace RadiusTheme\RadiusBooking\Shortcodes;

defined( 'ABSPATH' ) || exit;

/**
 * Class Shortcodes
 *
 * @package RadiusBooking\Shortcodes
 * @since 1.0.0
 */
class Shortcodes {

	/**
	 * Init shortcodes
	 */
	public static function init() {
		$shortcodes = array(
			'radius_booking_form'    => __CLASS__ . '::form',
			'rtrb_my_booking'        => __CLASS__ . '::my_booking',
			'rtrb_service_list'      => __CLASS__ . '::service_list',
			'rtrb_employee_list'     => __CLASS__ . '::employee_list',
			'rtrb_meeting_list'      => __CLASS__ . '::meeting_list',
			'rtrb_meeting_booking'   => __CLASS__ . '::meeting_booking',
			'rtrb_event_list'        => __CLASS__ . '::event_list',
			'rtrb_event_registration' => __CLASS__ . '::event_registration',
		);
		foreach ( $shortcodes as $shortcode => $function ) {
			add_shortcode( apply_filters( "rtrb_{$shortcode}_shortcode_tag", $shortcode ), $function );
		}
	}


	/**
	 * Renders a shortcode with optional wrappers and returns the output as a string.
	 *
	 * @param callable $function The callback function to execute for generating the shortcode content.
	 * @param array $atts Optional. Array of attributes to pass to the callback function. Default is an empty array.
	 * @param array $wrapper Optional. Array containing 'class', 'before', and 'after' keys for customizing the wrapper.
	 *                           - 'class': CSS class name for the wrapper container. Default is 'radius-booking'.
	 *                           - 'before': Content to prepend before the output. Default is null.
	 *                           - 'after': Content to append after the output. Default is null.
	 *
	 * @return string The rendered content of the shortcode with the optional wrappers applied.
	 */
	public static function shortcode_wrapper(
		$function,
		$atts = array(),
		$wrapper = array(
			'class'  => 'radius-booking',
			'before' => null,
			'after'  => null,
		)
	) {
		ob_start();

		echo empty( $wrapper['before'] )
			? '<div class="' . esc_attr( $wrapper['class'] ) . '">'
			: wp_kses_post( $wrapper['before'] );

		call_user_func( $function, $atts );

		echo empty( $wrapper['after'] )
			? '</div>'
			: wp_kses_post( $wrapper['after'] );

		return ob_get_clean();
	}


	/**
	 * Render booking form shortcode
	 *
	 * @param array $atts Shortcode attributes
	 *
	 * @return string
	 */
	public static function form( $atts ) {
		return self::shortcode_wrapper( array( 'RadiusTheme\RadiusBooking\Shortcodes\BookingForm', 'output' ), $atts );
	}

	public static function my_booking( $atts ) {
		return self::shortcode_wrapper( array( 'RadiusTheme\RadiusBooking\Shortcodes\MyBooking', 'output' ), $atts );
	}

	public static function service_list( $atts ) {
		return self::shortcode_wrapper( array( 'RadiusTheme\RadiusBooking\Shortcodes\ServiceList', 'output' ), $atts );
	}

	public static function employee_list( $atts ) {
		return self::shortcode_wrapper( array( 'RadiusTheme\RadiusBooking\Shortcodes\EmployeeList', 'output' ), $atts );
	}

	/**
	 * Render meeting booking shortcode.
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string
	 */
	/**
	 * Render meeting type list shortcode.
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string
	 */
	public static function meeting_list( $atts ) {
		return MeetingTypeList::output( $atts );
	}

	public static function meeting_booking( $atts ) {
		return MeetingBooking::output( $atts );
	}

	/**
	 * Render event listing shortcode.
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string
	 */
	public static function event_list( $atts ) {
		return EventListing::output( $atts );
	}

	/**
	 * Render event registration shortcode.
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string
	 */
	public static function event_registration( $atts ) {
		return EventRegistration::output( $atts );
	}
}
