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

import type {
  StatusTypes,
  Reservation,
  TimeBlockTemplate,
  Timeblock,
} from '../schedule.types';

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

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

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

  for (let i = 0; i <= 30; i++) {
    const template = templateB;
    result.push(
      ...boats
        .map((b): Timeblock[] => {
          return template.blocks.map((t): Timeblock => {
            return {
              id: 'id' + Math.random().toString(32).slice(2),
              boatId: 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: '1',
      status: 'confirmed',
    },
    {
      id: 2,
      user: 'Bob Barker',
      start: '16:00',
      end: '19:00',
      boat: '1',
      status: 'confirmed',
    },
    {
      id: 3,
      user: 'Peter Parker',
      start: '7:00',
      end: '13:00',
      boat: '4',
      status: 'tentative',
    },
    {
      id: 4,
      user: 'Vince McMahon',
      start: '10:00',
      end: '13:00',
      boat: '2',
      status: 'pending',
    },
    {
      id: 5,
      user: 'Heather Graham',
      start: '13:00',
      end: '19:00',
      boat: '4',
      status: 'confirmed',
    },
    {
      id: 6,
      user: 'Lawrence Fishburne',
      start: '13:00',
      end: '16:00',
      boat: '3',
    },
  ];
  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))),
      end: date.adjustDate(now, makeOpts(splitTime(entry.end))),
      resource: boat,
      reservationDate: now,
      status: entry.status as StatusTypes,
    };
  });
}
