<?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');
$todays_events_html = '';

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

// Feed colors custom CSS
if (function_exists('r34ics_feed_colors_css')) {
	r34ics_feed_colors_css($ics_data, true, false);
}
?>

<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 (only if colors are set)
		if (!empty(trim(@$args['color'], '|')) && empty($args['legendposition']) || $args['legendposition'] == 'above') {
			echo wp_kses(($R34ICS->color_key_html($args, $ics_data, true) ?: ''), r34ics_color_key_allowed());
		}

		if (count($ics_data['events']) > 1) {
			?>
			<label class="screen-reader-text" for="<?php echo esc_attr($ics_data['guid']); ?>-select"><?php esc_html_e('Year selection', 'ics-calendar-pro'); ?></label>
			<?php
			if (empty($ics_data['no_year_dropdown'])) {
				?>
				<select role="listbox" aria-label="<?php esc_attr_e('Year selection', 'ics-calendar-pro'); ?>" id="<?php echo esc_attr($ics_data['guid']); ?>-select" class="ics-calendar-select" style="display: none;" autocomplete="off">
					<?php
					// Build list from earliest to latest year
					foreach (array_keys((array)$ics_data['events']) as $year) {
						?>
						<option value="<?php echo esc_attr($year); ?>"<?php if ($year == r34ics_date('Y')) { echo ' selected="selected"'; } ?>><?php echo wp_kses_post($year); ?></option>
						<?php
					}
					?>
				</select>
				<?php
			}
		}
	
		// Print
		if (!empty($args['print'])) {
			echo wp_kses($R34ICS->print_button_html($args), r34ics_color_key_allowed());
		}
	
		// Build yearly/monthly calendars
		foreach (array_keys((array)$ics_data['events']) as $year) {
			?>

			<div class="ics-calendar-year-wrapper" data-year="<?php echo esc_attr($year); ?>"<?php if (count($ics_data['events']) > 1 && empty($ics_data['no_year_dropdown'])) { echo ' style="display: none;"'; } ?>>

				<<?php echo esc_attr($args['htmltagmonth']); ?> class="ics-calendar-label"><?php echo wp_kses_post($year); ?></<?php echo esc_attr($args['htmltagmonth']); ?>>
		
				<?php
				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('F', $m.'/1/'.$year));
					$month_uid = $ics_data['guid'] . '-' . $ym;
					
					// Skip month if in the past and we're starting on the current date
					if (empty($args['startdate']) && empty($args['pastdays']) && ($year < r34ics_date('Y') || ($year == r34ics_date('Y') && $m < r34ics_date('n')))) { continue; }
	
					// Build month's calendar
					?>
					<div class="ics-calendar-year-month" data-year="<?php echo esc_attr($year); ?>" data-month="<?php echo esc_attr($month); ?>">
			
						<<?php echo esc_attr($args['htmltagmonth']); ?> class="ics-calendar-label" id="<?php echo esc_attr($month_uid); ?>"><?php echo wp_kses_post($month_label); ?></<?php echo esc_attr($args['htmltagmonth']); ?>>
				
						<table class="ics-calendar-year-availability-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_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;
										foreach ((array)$day_events as $time => $events) {
											foreach ((array)$events as $event) {
												$day_classes .= ' ' . r34ics_event_css_classes($event, $time, $args);
											}
										}
										if (!empty($args['combined_availability']) && r34icspro_day_has_availability($day_events, $ics_data['urls'])) {
											$day_classes = str_replace('has_events', 'empty', $day_classes);
										}
										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 class="day"<?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>
										</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><!-- .ics-calendar-year-month -->
					<?php
				}
				?>
			</div><!-- .ics-calendar-year-wrapper -->

			<?php
		}

		// Color code key (only if colors are set)
		if (!empty(trim(@$args['color'], '|')) && !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>
