<?php

namespace SugarCalendarBookings\Calendar\Admin;

use SugarCalendarBookings\Calendar\CalendarHelpers;
use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Support\Helpers\Date;
use SugarCalendarBookings\Support\Helpers\Helpers;

/**
 * Class CalendarDayView.
 *
 * @since 1.1.0
 */
class CalendarDayView extends CalendarWeekView {

	/**
	 * View mode.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	const MODE = 'day';

	/**
	 * Get pagination first link URL.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_first_link_url() {

		return $this->query->get_url_with_date( $this->query->get_previous_week() );
	}

	/**
	 * Get pagination previous link URL.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_previous_link_url() {

		return $this->query->get_url_with_date( $this->query->get_previous_day() );
	}

	/**
	 * Get pagination next link URL.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_next_link_url() {

		return $this->query->get_url_with_date( $this->query->get_next_day() );
	}

	/**
	 * Get pagination last link URL.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_last_link_url() {

		return $this->query->get_url_with_date( $this->query->get_next_week() );
	}

	/**
	 * Get pagination first link text.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_first_link_text() {

		return esc_html__( 'Previous Week', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Get pagination previous link text.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_previous_link_text() {

		return esc_html__( 'Previous Day', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Get pagination next link text.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_next_link_text() {

		return esc_html__( 'Next Day', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Get pagination last link text.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected function pagination_get_last_link_text() {

		return esc_html__( 'Next Week', 'sugar-calendar-bookings-scheduling-appointments-lite' );
	}

	/**
	 * Get column options.
	 *
	 * @since 1.1.0
	 *
	 * @return array
	 */
	public function get_column_options() {

		return [];
	}

	/**
	 * Get header slots.
	 *
	 * @since 1.1.0
	 *
	 * @return array<BaseCalendarSlot>
	 */
	protected function get_header_slots() {

		$slots = $this->get_slots(
			$this->query->get_start_date(),
			$this->query->get_end_date()
		);

		// Insert week column.
		$week_slot = new WeekCalendarSlot( $slots[0]->get_start(), $slots[0]->get_end() );

		array_unshift( $slots, $week_slot );

		return $slots;
	}

	/**
	 * Get body slots.
	 *
	 * @since 1.1.0
	 *
	 * @return array<BaseCalendarSlot>
	 */
	protected function get_body_slots() {

		return $this->get_header_slots();
	}

	/**
	 * Get header cell class.
	 *
	 * @since 1.1.0
	 *
	 * @param BaseCalendarSlot $slot Calendar slot.
	 *
	 * @return array
	 */
	protected function get_header_cell_class( $slot ) {

		$class = parent::get_header_cell_class( $slot );

		if ( is_a( $slot, WeekCalendarSlot::class ) ) {
			$class[] = 'sugar-calendar-bookings-calendar__cell--week';
		} else {
			$class[] = 'sugar-calendar-bookings-calendar__cell--day';

			if ( $slot->is_same_day( $this->query->get_today() ) ) {
				$class[] = 'sugar-calendar-bookings-calendar__cell--today';
			}
		}

		return $class;
	}

	/**
	 * Get body cell class.
	 *
	 * @since 1.1.0
	 *
	 * @param BaseCalendarSlot $slot Calendar slot.
	 *
	 * @return array
	 */
	protected function get_body_cell_class( $slot ) {

		$class = parent::get_body_cell_class( $slot );

		if ( is_a( $slot, WeekCalendarSlot::class ) ) {
			$class[] = 'sugar-calendar-bookings-calendar__cell--week';
		} else {
			$class[] = 'sugar-calendar-bookings-calendar__cell--day';
		}

		return $class;
	}

	/**
	 * Display header cell.
	 *
	 * @since 1.1.0
	 *
	 * @param BaseCalendarSlot $slot Calendar slot.
	 *
	 * @return void
	 */
	protected function display_header_cell( $slot ) {

		if ( is_a( $slot, WeekCalendarSlot::class ) ) {
			$this->display_header_cell_week( $slot );
		} else {
			$this->display_header_cell_day( $slot );
		}
	}

	/**
	 * Display header cell for week.
	 *
	 * @since 1.3.0
	 *
	 * @param BaseCalendarSlot $slot Calendar slot.
	 */
	protected function display_header_cell_week( $slot ) {

		echo esc_html(
			sprintf(
			/* Translators: %1$s - ISO 8601 week number. */
				__( 'Week %1$s', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				$slot->get_start()->format( 'W' )
			)
		);
	}

	/**
	 * Display header cell for day.
	 *
	 * @since 1.3.0
	 *
	 * @param BaseCalendarSlot $slot Calendar slot.
	 */
	protected function display_header_cell_day( $slot ) {

		ob_start();
		?>

		<div class="sugar-calendar-bookings-calendar__day">
			<?php echo esc_html( wp_date( 'l, F j', $slot->get_start()->getTimestamp(), Date::get_default_timezone() ) ); ?>
		</div>
		<?php

		echo wp_kses(
			ob_get_clean(),
			[
				'div' => [
					'class' => [],
				],
			]
		);
	}

	/**
	 * Output dynamic styles.
	 *
	 * @since 1.1.0
	 * @since 1.4.0 Changed from static to instance method.
	 *
	 * @return void
	 */
	public function output_styles() {

		$hidden_columns        = Plugin::get( CalendarScreenOptions::class )->get( 'hidden_columns' );
		$hidden_columns        = $hidden_columns[ static::MODE ] ?? [];
		$day_is_visible        = ! in_array( 'day', $hidden_columns, true );
		$week_is_visible       = ! in_array( 'week', $hidden_columns, true );
		$bounds                = $this->get_display_bounds();
		$grid_template_rows    = CalendarHelpers::get_hour_grid_template_rows(
            $this->query->get_start_date(),
            [
				'start' => $bounds->start,
				'end'   => $bounds->end,
            ]
        );
		$available_hours       = $this->get_style_available_hours();
		$marker_visible        = $day_is_visible && $this->is_marker_visible();
		$grid_template_columns = [];

		if ( $week_is_visible ) {
			$column_width            = $day_is_visible ? '120px' : '1fr';
			$grid_template_columns[] = "[week] $column_width";
		}

		if ( $day_is_visible ) {
			$grid_template_columns[] = '[day] minmax(0, 1fr)';
		}

		Helpers::template(
			'calendar/day-view-styles.php',
			[
				'grid_template_rows'    => $grid_template_rows,
				'grid_template_columns' => $grid_template_columns,
				'available_hours'       => $available_hours,
				'week_is_visible'       => $week_is_visible,
				'day_is_visible'        => $day_is_visible,
				'hidden_columns'        => $hidden_columns,
				'marker_visible'        => $marker_visible,
			]
		);
	}
}
