import { DateOptions, date } from 'quasar';
import { Boat, useBoatStore } from '../boat';
import {
  parseTimestamp,
  today,
  Timestamp,
  addToDate,
} from '@quasar/quasar-ui-qcalendar';

import type {
  StatusTypes,
  Reservation,
  IntervalTemplate,
  Interval,
  TimeTuple,
} from '../schedule.types';

export const templateA: IntervalTemplate = {
  id: '1',
  name: 'WeekdayBlocks',
  timeTuples: [
    ['08:00', '12:00'],
    ['12:00', '16:00'],
    ['17:00', '21:00'],
  ],
};

export const templateB: IntervalTemplate = {
  id: '2',
  name: 'WeekendBlocks',
  timeTuples: [
    ['07:00', '10:00'],
    ['10:00', '13:00'],
    ['13:00', '16:00'],
    ['16:00', '19:00'],
  ],
};

export function getSampleIntervals(): Interval[] {
  // Hard-code 30 days worth of blocks, for now.  Make them random templates
  const boats = useBoatStore().boats;
  const result: Interval[] = [];
  const tsToday: Timestamp = parseTimestamp(today()) as Timestamp;

  for (let i = 0; i <= 30; i++) {
    const template = templateB;
    result.push(
      ...boats
        .map((b): Interval[] => {
          return template.blocks.map((t: TimeTuple): Interval => {
            return {
              $id: 'id' + Math.random().toString(32).slice(2),
              resource: b.$id,
              start: addToDate(tsToday, { day: i }).date + ' ' + t[0],
              end: addToDate(tsToday, { day: i }).date + ' ' + t[1],
            };
          });
        })
        .flat(2)
    );
  }
  return result;
}

export function getSampleReservations(): Reservation[] {
  const sampleData = [
    {
      id: '1',
      user: 'John Smith',
      start: '7:00',
      end: '10:00',
      boat: '66359729003825946ae1',
      status: 'confirmed',
      reason: 'Open Sail',
    },
    {
      id: '2',
      user: 'Bob Barker',
      start: '16:00',
      end: '19:00',
      boat: '66359729003825946ae1',
      status: 'confirmed',
      reason: 'Open Sail',
    },
    {
      id: '3',
      user: 'Peter Parker',
      start: '7:00',
      end: '13:00',
      boat: '663597030029b71c7a9b',
      status: 'tentative',
      reason: 'Open Sail',
    },
    {
      id: '4',
      user: 'Vince McMahon',
      start: '10:00',
      end: '13:00',
      boat: '663597030029b71c7a9b',
      status: 'pending',
      reason: 'Open Sail',
    },
    {
      id: '5',
      user: 'Heather Graham',
      start: '13:00',
      end: '19:00',
      boat: '663596b9000235ffea55',
      status: 'confirmed',
      reason: 'Private Sail',
    },
    {
      id: '6',
      user: 'Lawrence Fishburne',
      start: '13:00',
      end: '16:00',
      boat: '663596b9000235ffea55',
      reason: 'Open Sail',
    },
  ];
  const boatStore = useBoatStore();
  const now = new Date();
  const splitTime = (x: string): string[] => {
    return x.split(':');
  };
  const makeOpts = (x: string[]): DateOptions => {
    return {
      hour: parseInt(x[0]),
      minute: parseInt(x[1]),
      seconds: 0,
      milliseconds: 0,
    };
  };

  return sampleData.map((entry): Reservation => {
    const boat = <Boat>boatStore.boats.find((b) => b.$id == entry.boat);
    return {
      id: entry.id,
      user: entry.user,
      start: date
        .adjustDate(now, makeOpts(splitTime(entry.start)))
        .toISOString(),
      end: date.adjustDate(now, makeOpts(splitTime(entry.end))).toISOString(),
      resource: boat.$id,
      reservationDate: now,
      reason: entry.reason,
      status: entry.status as StatusTypes,
      comment: '',
    };
  });
}
