<?php 

namespace Bookify\Controllers\Frontend;

use BookifyPro\Models\Bookify_Pro_Location_Models;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Bookify_Shortcodes' ) ) {

	class Bookify_Shortcodes {

		public function __construct() { 

			add_shortcode( 'bookify_bookings', array( $this, 'render_bookings' ) );
			add_shortcode( 'bookify_appointments', array( $this, 'render_appointments' ) );
		}

		public function render_appointments() {
			if ( is_user_logged_in() ) {

				wp_localize_script('bookify-appointments-script', 'wpbAptApp', array(
					'root' => esc_url_raw( rest_url() ),
					'nonce' => wp_create_nonce('wp_rest'),
					'wpTimezone' => wp_timezone_string(),
				));

				wp_enqueue_script( 'bookify-appointments-script' );

				return '<div id="bookify-appointments" style="max-width:100%;"></div>';

			} else {
				return '<div id="bookify-non-login">
					<span>Please login to see your appointments!</span>
				</div>';
			}
		}

		public function render_bookings( $atts ) {

			extract( shortcode_atts([
					'theme'				=> 'default',
					'locations' 		=> 'all',
					'hide_locations' 	=> 'false',
					'services' 			=> 'all',
					'hide_services' 	=> 'false',
					'staffs' 			=> 'all',
					'hide_staffs' 		=> 'false',
				], $atts, 'bookify_bookings')
			);

			$allowed_themes = apply_filters( 'bookify_allowed_themes', array( 'default', 'light' ) );
			$theme = isset( $theme ) ? sanitize_text_field( strtolower( $theme ) ) : 'default';
			$theme = in_array( $theme, $allowed_themes, true ) ? $theme : 'default';

			$locations 	= array_filter( array_map( 'trim', explode( ',', $locations ) ) );
			$services 	= array_filter( array_map( 'trim', explode( ',', $services ) ) );
			$staffs 	= array_filter( array_map( 'trim', explode( ',', $staffs ) ) );

			if ( empty( $locations ) || in_array( 'all', $locations ) || count( $locations ) > 1 ) {
				$hide_locations = 'false';
			} else {
				$hide_locations = $hide_locations ?? 'false';
			}

			if ( empty( $services ) || in_array( 'all', $services ) || count( $services ) > 1 ) {
			    $hide_services = 'false';
			} else {
			    $hide_services = $hide_services ?? 'false';
			}

			if ( empty( $staffs ) || in_array( 'all', $staffs ) || count( $staffs ) > 1 ) {
			    $hide_staffs = 'false';
			} else {
			    $hide_staffs = $hide_staffs ?? 'false';
			}

			wp_enqueue_style( 'bookify-frontend-' . $theme . '-style' );
			wp_enqueue_style( 'tailwind-css' );

			$customer_data = array(
				'id' => 0,
				'fname' => '',
				'lname' => '',
				'email' => '',
				'phone' => ''
			);

			$general_settings = get_option( 'bookify_general_settings' );

			if ( is_array( $general_settings ) && isset( $general_settings['usersCanBook'] ) && 'onlyRegistered' == $general_settings['usersCanBook'] ) {

				if ( is_user_logged_in() ) {

					$user = wp_get_current_user();

					if ( in_array( 'bookify-customer', (array) $user->roles ) || in_array( 'administrator', (array) $user->roles ) ) {

						$customer_data = array(
							'id' => $user->ID,
							'fname' => $user->user_firstname,
							'lname' => $user->user_lastname,
							'email' => $user->user_email,
							'phone' => get_user_meta( $user->ID, 'bookify_customer_phone', true )
						);

					} else {
						return '<div id="bookify-not-registered">
							<span>You\'re not logged in as a Customer. Please contact Admin for further details.</span>
						</div>';
					}
	
				} else {

					return '<div id="bookify-not-registered">
						<span>Please login as a Customer to book an appointment.</span>
					</div>';

				}
				
			} elseif ( is_user_logged_in() ) {

				$user = wp_get_current_user();

				if ( in_array( 'bookify-customer', (array) $user->roles ) || in_array( 'administrator', (array) $user->roles ) ) {

					$customer_data = array(
						'id' => $user->ID,
						'fname' => $user->user_firstname,
						'lname' => $user->user_lastname,
						'email' => $user->user_email,
						'phone' => get_user_meta( $user->ID, 'bookify_customer_phone', true )
					);

				}
			}

			$all_locations = array();

			if ( class_exists('\BookifyPro\Models\Bookify_Pro_Location_Models') && ( defined( 'BOOKIFY_PRO_LICENCE_ACTIVATED' ) || defined( 'BOOKIFY_PRO_LICENCE_ACTIVATED' ) == true ) ) {
				$all_locations = Bookify_Pro_Location_Models::bookify_get_all_locations();
			}
			
			wp_localize_script('bookify-frontend-script', 'wpbApp', array(
				'root' => esc_url_raw( rest_url() ),
				'nonce' => wp_create_nonce('wp_rest'),
				'customerData' => $customer_data,
				'wpTimezone' => wp_timezone_string(),
				'Services' => $services,
				'HideServices' => $hide_services,
				'Locations' => $locations,
				'HideLocations' => $hide_locations,
				'Staffs' => $staffs,
				'HideStaffs' => $hide_staffs,
				'locationsCount' => count( $all_locations ),
				'theme' => $theme,
			));

			wp_enqueue_script( 'bookify-frontend-script' );

			return '<div id="bookify-frontend" data-theme="' . esc_attr( $theme ) . '">	
				<div class="bookify-form-skeleton">
					<div class="bookify-skeleton-sidebar">
						<div class="bookify-skeleton-sidebar-item">
							<div class="bookify-skeleton-block bookify-skeleton-icon"></div>
							<div class="bookify-skeleton-block bookify-skeleton-text-sm"></div>
						</div>
						<div class="bookify-skeleton-sidebar-item">
							<div class="bookify-skeleton-block bookify-skeleton-icon"></div>
							<div class="bookify-skeleton-block bookify-skeleton-text-sm"></div>
						</div>
						<div class="bookify-skeleton-sidebar-item">
							<div class="bookify-skeleton-block bookify-skeleton-icon"></div>
							<div class="bookify-skeleton-block bookify-skeleton-text-sm"></div>
						</div>
						<div class="bookify-skeleton-sidebar-item">
							<div class="bookify-skeleton-block bookify-skeleton-icon"></div>
							<div class="bookify-skeleton-block bookify-skeleton-text-sm"></div>
						</div>
						<div class="bookify-skeleton-sidebar-item">
							<div class="bookify-skeleton-block bookify-skeleton-icon"></div>
							<div class="bookify-skeleton-block bookify-skeleton-text-sm"></div>
						</div>
						<div class="bookify-skeleton-sidebar-item">
							<div class="bookify-skeleton-block bookify-skeleton-icon"></div>
							<div class="bookify-skeleton-block bookify-skeleton-text-sm"></div>
						</div>
					</div>

					<div class="bookify-skeleton-main">
						<div class="bookify-skeleton-main-header">
							<div class="bookify-skeleton-block bookify-skeleton-title"></div>
						</div>

						<div class="bookify-skeleton-main-body">
							<div class="bookify-skeleton-block bookify-skeleton-text-md"></div>

							<div class="bookify-skeleton-services-row">
								<div class="bookify-skeleton-block bookify-skeleton-card"></div>
								<div class="bookify-skeleton-block bookify-skeleton-card"></div>
							</div>

							<div class="bookify-skeleton-block bookify-skeleton-text-md"></div>

							<div class="bookify-skeleton-services-row">
								<div class="bookify-skeleton-block bookify-skeleton-card"></div>
							</div>
						</div>

						<div class="bookify-skeleton-main-footer">
							<div class="bookify-skeleton-block bookify-skeleton-button-lg"></div>
						</div>
					</div>
				</div>
			</div>';
		}
	}

}
