<template>
  <div>
    <CalendarHeaderComponent v-model="selectedDate" />
    <div class="boat-schedule-table-component">
      <QCalendarDay
        ref="calendar"
        class="q-pa-xs"
        flat
        animated
        dense
        :disabled-before="disabledBefore"
        interval-height="24"
        interval-count="18"
        interval-start="06:00"
        :short-interval-label="true"
        v-model="selectedDate"
        :column-count="boats.length"
        @change="scrollToEvent()"
        v-touch-swipe.left.right="handleSwipe"
      >
        <template #head-day="{ scope }">
          <div style="text-align: center; font-weight: 800">
            {{ boats[scope.columnIndex].displayName }}
          </div>
        </template>

        <template #day-body="{ scope }">
          <div v-for="block in blocklist" :key="block.id">
            <div
              class="timeblock"
              :style="
                blockStyles(block, scope.timeStartPos, scope.timeDurationHeight)
              "
              @click="selectBlock($event, scope, block)"
            >
              Available
            </div>
          </div>
        </template>
      </QCalendarDay>
    </div>
  </div>
</template>

<script setup lang="ts">
import {
  QCalendarDay,
  Timestamp,
  diffTimestamp,
  today,
  parsed,
  parseTimestamp,
  addToDate,
} from '@quasar/quasar-ui-qcalendar';
import CalendarHeaderComponent from './CalendarHeaderComponent.vue';

import { ref, reactive, computed } from 'vue';
import { useBoatStore } from 'src/stores/boat';
import { Timeblock, useScheduleStore } from 'src/stores/schedule';
import { TouchSwipeValue } from 'quasar';

const scheduleStore = useScheduleStore();
const boatStore = useBoatStore();
const selectedBlock = ref<Timeblock | null>(null);
const selectedDate = ref(today());

const blocklist = reactive<Timeblock[]>(
  scheduleStore.getTimeblocksForDate(parsed(today()) as Timestamp)
);
const boats = boatStore.boats;

const calendar = ref<QCalendarDay | null>(null);

function handleSwipe(event: Event) {
  event.direction === 'right' ? calendar.value?.prev() : calendar.value?.next();
}
function blockStyles(
  block: Timeblock,
  timeStartPos: (t: string) => string,
  timeDurationHeight: (d: number) => string
) {
  const s = {
    top: '',
    height: '',
    opacity: '',
  };
  if (timeStartPos && timeDurationHeight) {
    s.top = timeStartPos(block.start.time) + 'px';
    s.height =
      timeDurationHeight(
        diffTimestamp(block.start, block.end, false) / 1000 / 60
      ) + 'px';
  }
  if (selectedBlock.value?.id === block.id) {
    s.opacity = '1.0';
  }
  return s;
}

interface DayBodyScope {
  columnIndex: number;
  timeDurationHeight: string;
  timeStartPos: (time: string, clamp: boolean) => string;
  timestamp: Timestamp;
}

function selectBlock(event: MouseEvent, scope: DayBodyScope, block: Timeblock) {
  // TODO: Disable blocks before today with updateDisabled and/or comparison
  selectedBlock.value = block;
}

function scrollToEvent() {
  setTimeout(() => calendar.value?.scrollToTime('09:00'), 10); // Should figure out why we need this setTimeout...
}

const disabledBefore = computed(() => {
  let ts = parseTimestamp(today());
  ts = addToDate(ts, { day: -1 });
  return ts.date;
});
</script>

<style lang="sass">
.boat-schedule-table-component
  display: flex
  height: 60vh
.timeblock
  display: flex
  position: absolute
  justify-content: center
  align-items: center
  width: 100%
  opacity: 0.25
  margin: 0 1px
  text-overflow: ellipsis
  overflow: hidden
  font-size: 0.8em
  cursor: pointer
  background: $primary
  color: white
  border: 2px solid black
.selected
  opacity: 1 !important
.q-calendar-day__interval--text
  font-size: 0.8em
</style>
