import { defineStore } from 'pinia';
import { ref } from 'vue';
import { Boat } from './boat';
import {
  Timestamp,
  parseDate,
  parsed,
  compareDate,
} from '@quasar/quasar-ui-qcalendar';

import { Reservation, Timeblock } from './schedule.types';
import {
  getSampleReservations,
  getSampleTimeBlocks,
} from './sampledata/schedule';

export const useScheduleStore = defineStore('schedule', () => {
  // TODO: Implement functions to dynamically pull this data.
  const reservations = ref<Reservation[]>(getSampleReservations());
  const timeblocks = ref<Timeblock[]>(getSampleTimeBlocks());

  const getTimeblocksForDate = (date: string): Timeblock[] => {
    return timeblocks.value.filter((b) =>
      compareDate(parsed(b.start) as Timestamp, parsed(date) as Timestamp)
    );
  };

  const getBoatReservations = (
    searchDate: Timestamp,
    boat?: string
  ): Reservation[] => {
    return reservations.value.filter((x) => {
      return (
        ((parseDate(x.start)?.date == searchDate.date ||
          parseDate(x.end)?.date == searchDate.date) && // Part of reservation falls on day
          x.resource != undefined && // A boat is defined
          !boat) ||
        x.resource.$id == boat // A specific boat has been passed, and matches
      );
    });
  };

  // const getConflicts = (timeblock: Timeblock, boat: Boat) => {
  //   const start = date.buildDate({
  //     hour: timeblock.start.hour,
  //     minute: timeblock.start.minute,
  //     second: 0,
  //     millisecond: 0,
  //   });
  //   const end = date.buildDate({
  //     hour: timeblock.end.hour,
  //     minute: timeblock.end.minute,
  //     second: 0,
  //     millisecond: 0,
  //   });
  //   return scheduleStore.getConflictingReservations(boat, start, end);
  // };
  const getConflictingReservations = (
    resource: Boat,
    start: Date,
    end: Date
  ): Reservation[] => {
    const overlapped = reservations.value.filter(
      (entry: Reservation) =>
        entry.resource.$id == resource.$id &&
        entry.start < end &&
        entry.end > start
    );
    return overlapped;
  };

  const isResourceTimeOverlapped = (
    resource: Boat,
    start: Date,
    end: Date
  ): boolean => {
    return getConflictingReservations(resource, start, end).length > 0;
  };

  const isReservationOverlapped = (res: Reservation): boolean => {
    return isResourceTimeOverlapped(res.resource, res.start, res.end);
  };

  const getNewId = (): string => {
    return [...Array(20)]
      .map(() => Math.floor(Math.random() * 16).toString(16))
      .join('');
    // Trivial placeholder
    //return Math.max(...reservations.value.map((item) => item.id)) + 1;
  };

  const addOrCreateReservation = (reservation: Reservation) => {
    const index = reservations.value.findIndex(
      (res) => res.id == reservation.id
    );
    index != -1
      ? (reservations.value[index] = reservation)
      : reservations.value.push(reservation);
  };

  return {
    reservations,
    getBoatReservations,
    getConflictingReservations,
    getTimeblocksForDate,
    getNewId,
    addOrCreateReservation,
    isReservationOverlapped,
    isResourceTimeOverlapped,
  };
});
