<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

// Don't load directly
if (!defined('ABSPATH')) { exit; }

// Require object
if (empty($ics_data)) { return false; }

global $R34ICS;

$days_of_week = $R34ICS->get_days_of_week('min');
$start_of_week = get_option('start_of_week', 0);

$date_format = r34ics_date_format($args['format'], true);

$today = r34ics_date('Ymd');
$this_ym = r34ics_date('Ym');
$todays_events_html = '';

$ics_calendar_classes = apply_filters('r34ics_calendar_classes', null, $args, true);

// Feed colors custom CSS
if (!empty($ics_data['colors']) || !empty($args['empty_day_bg_color']) && function_exists('r34ics_feed_colors_css')) {
	r34ics_feed_colors_css($ics_data, true, false);
}

// Prepare event details toggle lightbox
if ($args['toggle'] === 'lightbox') {
	r34ics_lightbox_container();
}

// Add empty day filler data
if (!empty($args['empty_day_title'])) {
	$no_events_html = '<ul class="events"><li class="event all-day filler"><span class="title has_desc">' . wp_kses_post($args['empty_day_title']) . '</span>';
	if (!empty($args['empty_day_description'])) {
		$no_events_html .= '<div class="descloc"><div class="eventdesc">' . wp_kses_post($args['empty_day_description']) . '</div></div>';
	}
	$no_events_html .= '</li></ul>';
}
else {
	$no_events_html = __('No events found.', 'ics-calendar-pro');
}

?>

<section class="<?php echo esc_attr($ics_calendar_classes); ?>" id="<?php echo esc_attr($ics_data['guid']); ?>" style="opacity: 0;">

	<?php
	// Title and description
	if (!empty($ics_data['title'])) {
		?>
		<<?php echo esc_attr($args['htmltagtitle']); ?> class="ics-calendar-title"><?php echo wp_kses_post($ics_data['title']); ?></<?php echo esc_attr($args['htmltagtitle']); ?>>
		<?php
	}
	if (!empty($ics_data['description'])) {
		?>
		<p class="ics-calendar-description"><?php echo wp_kses_post($ics_data['description']); ?></p>
		<?php
	}
	
	// Empty calendar message
	if (empty($ics_data['events'])) {
		?>
		<p class="ics-calendar-error"><?php esc_html_e('No events found.', 'ics-calendar-pro'); ?></p>
		<?php
	}
	
	// Display calendar
	else {

		// Actions before rendering calendar wrapper (can include additional template output)
		do_action('r34ics_display_calendar_before_wrapper', $view, $args, $ics_data);

		// Color code key
		if (empty($args['legendposition']) || $args['legendposition'] == 'above') {
			echo wp_kses(($R34ICS->color_key_html($args, $ics_data, true) ?: ''), r34ics_color_key_allowed());
		}
		?>		

		<div class="ics-calendar-widget">

			<div class="ics-calendar-overview" data-this-month="<?php echo esc_attr($this_ym); ?>">
				
				<?php
				// Build monthly calendars
				foreach (array_keys((array)$ics_data['events']) as $year) {
					for ($m = 1; $m <= 12; $m++) {
						$month = $m < 10 ? '0' . $m : '' . $m;
						$ym = $year . $month;
						// Is this month in range? If not, continue
						if (!r34ics_month_in_range($ym, $ics_data)) { continue; }
						$month_label = ucwords(r34ics_date($args['formatmonthyear'], $m.'/1/'.$year));
						$month_uid = $ics_data['guid'] . '-' . $ym;
				
						// Build month's calendar
						?>
						<div class="ics-calendar-widget-wrapper ics-calendar-month-wrapper" style="display: none;" data-year-month="<?php echo esc_attr($ym); ?>">
						
							<<?php echo esc_attr($args['htmltagmonth']); ?> class="ics-calendar-label">
								<span tabindex="0" class="ics-calendar-pagination prev" title="<?php esc_attr_e('Previous Month', 'ics-calendar-pro'); ?>" aria-label="<?php esc_attr_e('Previous Month', 'ics-calendar-pro'); ?>">&larr;</span>
								<span id="<?php echo esc_attr($month_uid); ?>"><?php echo wp_kses_post($month_label); ?></span>
								<span tabindex="0" class="ics-calendar-pagination next" title="<?php esc_attr_e('Next Month', 'ics-calendar-pro'); ?>">&rarr;</span>
							</<?php echo esc_attr($args['htmltagmonth']); ?>>
							
							<table class="ics-calendar-widget-grid" aria-labelledby="<?php echo esc_attr($month_uid); ?>">
								<thead>
									<tr>
										<?php
										foreach ((array)$days_of_week as $w => $dow) {
											?>
											<th data-dow="<?php echo esc_attr($w); ?>"><?php echo wp_kses_post($dow); ?></th>
											<?php
										}
										?>
									</tr>
								</thead>

								<tbody>
									<tr>
										<?php
										$first_dow = $R34ICS->first_dow($m.'/1/'.$year);
										if ($first_dow < $start_of_week) { $first_dow = $first_dow + 7; }
										for ($off_dow = $start_of_week; $off_dow < $first_dow; $off_dow++) {
											?>
											<td class="off" data-dow="<?php echo intval($off_dow); ?>"></td>
											<?php
										}
										for ($day = 1; $day <= r34ics_date('t', $m.'/1/'.$year); $day++) {
											$date = r34ics_date('Ymd', $m.'/'.$day.'/'.$year);
											$d = r34ics_date('d', $date);
											$dow = r34ics_date('w', $date);
											$day_events = isset($ics_data['events'][$year][$month][$d]) ? $ics_data['events'][$year][$month][$d] : null;
											$day_events_count = r34ics_day_events_count($day_events);
											$day_feed_keys = r34ics_day_events_feed_keys($day_events, '|');
											$day_uid = $ics_data['guid'] . '-' . $ym . $d;
											$day_classes = function_exists('r34ics_day_classes') ? r34ics_day_classes(array(
												'date' => $date,
												'today' => $today,
												'count' => $day_events_count,
												'filler' => !empty($day_events['all-day'][0]['filler'])
											)) : null;
											if ($dow == $start_of_week) {
												if ($day > 1) { echo '</tr><tr>'; }
											}
											?>
											<td data-dow="<?php echo intval($dow); ?>" data-date="<?php echo esc_attr($date); ?>" data-formatted-date="<?php echo esc_attr(r34ics_date($date_format, $date)); ?>" data-events-count="<?php echo intval($day_events_count); ?>" data-feed-keys="<?php echo esc_attr($day_feed_keys); ?>" class="<?php echo esc_attr($day_classes); ?>">
												<div tabindex="0" aria-haspopup="true" class="day" id="<?php echo esc_attr($day_uid); ?>"<?php
													if (!empty($ics_data['colors'][$day_feed_keys]['base'])) { echo ' data-feed-color="' . esc_attr($ics_data['colors'][$day_feed_keys]['base']) . '"'; }
												?>><?php echo wp_kses_post(r34ics_date('j', $date)); ?></div>
												<?php
												if (!empty($day_events)) {
													?>
													<ul class="events hidden" data-event-count="<?php echo esc_attr(r34ics_day_events_count($day_events)); ?>" aria-labelledby="<?php echo esc_attr($day_uid); ?>"><?php
														if ($date == $today) {
															ob_start();
														}
														foreach ((array)$day_events as $time => $events) {
															$all_day_indicator_shown = !empty($args['hidealldayindicator']);
															foreach ((array)$events as $event) {
																$has_desc = r34ics_has_desc($args, $event);
																if ($time == 'all-day') {
																	?>
																	<li class="<?php echo esc_attr(r34ics_event_css_classes($event, $time, $args)); ?>" data-feed-key="<?php echo intval($event['feed_key']); ?>"<?php
																		if (!empty($ics_data['colors'][$event['feed_key']]['base'])) { echo ' data-feed-color="' . esc_attr($ics_data['colors'][$event['feed_key']]['base']) . '"'; }
																		if (!empty($event['categories'])) { echo ' data-categories="' . esc_attr($event['categories']) . '"'; }
																		if (!empty($event['color'])) { echo ' data-color="' . esc_attr($event['color']) . '"'; }
																	?>>
																		<?php
																		if (!$all_day_indicator_shown) {
																			?>
																			<span class="all-day-indicator"><?php esc_html_e('All Day', 'ics-calendar-pro'); ?></span>
																			<?php
																			$all_day_indicator_shown = true;
																		}

																		// Event label (title)
																		echo wp_kses_post($R34ICS->event_label_html($args, $event, (!empty($has_desc) ? array('has_desc') : null)));

																		// Sub-label
																		echo wp_kses_post($R34ICS->event_sublabel_html($args, $event, null));

																		// Description/Location/Organizer
																		echo wp_kses_post($R34ICS->event_description_html($args, $event, array('sidebar_only'), $has_desc));
																		?>
																	</li>
																	<?php
																}
																else {
																	?>
																	<li class="<?php echo esc_attr(r34ics_event_css_classes($event, $time, $args)); ?>" data-feed-key="<?php echo intval($event['feed_key']); ?>"<?php
																		if (!empty($ics_data['colors'][$event['feed_key']]['base'])) { echo ' data-feed-color="' . esc_attr($ics_data['colors'][$event['feed_key']]['base']) . '"'; }
																		if (!empty($event['categories'])) { echo ' data-categories="' . esc_attr($event['categories']) . '"'; }
																		if (!empty($event['color'])) { echo ' data-color="' . esc_attr($event['color']) . '"'; }
																	?>>
																		<?php
																		if (!empty($event['start'])) {
																			?>
																			<span class="time"><?php
																			echo wp_kses_post($event['start']);
																			if (!empty($event['end']) && $event['end'] != $event['start']) {
																				if (empty($args['showendtimes'])) {
																					?>
																					<span class="show_on_hover">&#8211; <?php echo wp_kses_post($event['end']); ?></span>
																					<?php
																				}
																				else {
																					?>
																					&#8211; <?php echo wp_kses_post($event['end']); ?>
																					<?php
																				}
																			}
																			?></span>
																			<?php
																		}

																		// Event label (title)
																		echo wp_kses_post($R34ICS->event_label_html($args, $event, (!empty($has_desc) ? array('has_desc') : null)));

																		// Sub-label
																		echo wp_kses_post($R34ICS->event_sublabel_html($args, $event, null));

																		// Description/Location/Organizer
																		echo wp_kses_post($R34ICS->event_description_html($args, $event, array('sidebar_only'), $has_desc));
																		?>
																	</li>
																	<?php
																}
															}
														}
														if ($date == $today) {
															$todays_events_html = ob_get_clean();
															echo wp_kses_post($todays_events_html);
														}
													?></ul>
													<?php
												}
												?>
											</td>
											<?php
										}
										$calc_dow = ($start_of_week != 0 && $dow == 0) ? 7 : $dow;
										for ($off_dow = $calc_dow + 1; $off_dow < ($start_of_week + 7); $off_dow++) {
											?>
											<td class="off" data-dow="<?php echo intval($off_dow % 7); ?>"></td>
											<?php
										}
										?>
									</tr>
								</tbody>
							</table>
	
						</div>
						<?php
					}
				}
				?>
			
			</div><!-- .ics-calendar-overview -->
			
			<div class="ics-calendar-day-details" aria-hidden="true">
				<div class="ics-calendar-day-details-content">
					<<?php echo esc_attr($args['htmltagdate']); ?> class="ics-calendar-current-date"><?php echo wp_kses_post(r34ics_date($date_format)); ?></<?php echo esc_attr($args['htmltagdate']); ?>>
					<div class="ics-calendar-current-events" data-no-events-html="<?php echo esc_attr($no_events_html); ?>">
						<?php
						echo wp_kses_post(!empty($todays_events_html) ? '<ul class="events">' . $todays_events_html . '</ul>' : $no_events_html);
						?>
					</div>
				</div>
			</div><!-- .ics-calendar-day-details -->
			
		</div><!-- .ics-calendar-widget -->
		
		<?php
		// Color code key
		if (!empty($args['legendposition']) && $args['legendposition'] == 'below') {
			echo wp_kses(($R34ICS->color_key_html($args, $ics_data, true) ?: ''), r34ics_color_key_allowed());
		}

		// Actions after rendering calendar wrapper (can include additional template output)
		do_action('r34ics_display_calendar_after_wrapper', $view, $args, $ics_data);

	}
	?>

</section>
