import { iterateMinutes } from '@dvn/alaca/components/dvn-date-picker/date-utils';
import { notFound } from '@dvn/dvn-mock/api/utils';
export class TakvimApiDemo {
  // private storage: Storage;
  constructor(database, _storage, logger) {
    this.logger = logger.new('TakvimApiDemo').setColor('asphalt');
    // this.storage = storage;
    this.database = database;
  }
  getCalendarConfig(id) {
    const item = this.database.takvimCalendarConfigs[id];
    if (!item) {
      this.logger.warnGroup('getCalendarConfig: calendar config not found', { id });
      return Promise.reject(`TakvimApiDemo.getCalendarConfig: Calendar config "${id}" not found`);
    }
    return new Promise((resolve => {
      setTimeout(() => resolve(item), 1000);
    }));
  }
  getMeetingsForCalendar(_calendarConfigId, _startDate, _endDate) {
    return Promise.resolve(undefined);
  }
  // getAvailabilities(calendarConfigId: string): Promise<GetListResponse<Availability>> {
  //   const userId = this.storage.get(STORAGE_KEY_KIMLIK_USER_ID);
  //   const user = this.database.kimlikUsers[userId];
  //   if (!user) {
  //     this.logger.warnGroup('getAvailabilities: user not found', { userId, calendarConfigId });
  //     return Promise.reject(notFound());
  //   }
  //
  //   const table = this.database[calendarConfigId];
  //   if (!table) {
  //     this.logger.warnGroup(
  //       'getAvailabilities: table not found',
  //       { calendarConfigId },
  //     );
  //
  //     return Promise.reject(`TakvimApiDemo.getAvailabilities: Table "${calendarConfigId}" not found`);
  //   }
  //
  //   const items = Object.keys(table).map(id => table[id]) as Availability[];
  //   const results = items.filter(item => item.user._id === userId);
  //
  //   this.logger.debugGroup(
  //     'getAvailabilities',
  //     { calendarConfigId, results },
  //   );
  //
  //   return Promise.resolve({ results });
  // }
  getAvailabilitiesOfUser(userId) {
    const user = this.database.kimlikUsers[userId];
    if (!user) {
      this.logger.warnGroup('getAvailabilities: user not found', { userId });
      return Promise.reject(notFound());
    }
    const table = this.database.takvimAvailabilities;
    if (!table) {
      this.logger.warnGroup('getAvailabilities: table "takvimAvailabilities" not found');
      return Promise.reject(`TakvimApiDemo.getAvailabilities: Table "takvimAvailabilities" not found`);
    }
    const items = Object.keys(table).map(id => table[id]);
    const results = items.filter(item => item.user._id === userId);
    this.logger.debugGroup('getAvailabilitiesOfUser', { results, userId });
    return Promise.resolve({ results });
  }
  getUnavailabilitiesOfUser(_calendarConfigId, userId, startTime, _endTime) {
    console.log('userId', userId);
    if (userId === 'kimlikUsers1') {
      return new Promise((resolve => {
        setTimeout(() => resolve({
          results: [
            {
              startDate: '2021-05-02T12:00:00Z',
              endDate: '2021-05-02T13:30:00Z',
              reason: {
                source: 'google-calendar',
                name: 'Family Lunch'
              }
            }
          ]
        }), 200);
      }));
    }
    const morningStart = new Date(startTime);
    morningStart.setHours(7);
    morningStart.setMinutes(0);
    const morningEnd = iterateMinutes(morningStart, 120);
    const noonStart = new Date(startTime);
    noonStart.setHours(12);
    noonStart.setMinutes(0);
    const noonEnd = iterateMinutes(noonStart, 60);
    const eveningStart = new Date(startTime);
    eveningStart.setHours(18);
    eveningStart.setMinutes(0);
    const eveningEnd = iterateMinutes(eveningStart, 60);
    return new Promise((resolve => {
      setTimeout(() => resolve({
        results: [
          {
            startDate: morningStart.toISOString(),
            endDate: morningEnd.toISOString(),
          },
          {
            startDate: noonStart.toISOString(),
            endDate: noonEnd.toISOString(),
          },
          {
            startDate: eveningStart.toISOString(),
            endDate: eveningEnd.toISOString(),
          },
        ]
      }), 300);
    }));
  }
  getMeetingsOfUser(calendarConfigId, guestIDs) {
    const [userId, otherGuestId] = guestIDs;
    const table = this.database.takvimEvents;
    if (!table) {
      this.logger.warnGroup('getMeetingsOfUser: table not found', { calendarConfigId, userId, otherGuestId });
      return Promise.reject(`TakvimApiDemo.getMeetingsOfUser: Table "takvimEvents" not found`);
    }
    const items = Object.keys(table).map(id => table[id]);
    const results = items.filter(item => {
      const hasUser = item.guests.some(guest => guest._id == userId);
      if (!hasUser)
        return false;
      if (!otherGuestId)
        return hasUser;
      const hasOtherUser = item.guests.some(guest => guest._id == userId);
      return hasUser && hasOtherUser;
    });
    this.logger.debugGroup('getMeetingsOfUser', { calendarConfigId, userId, otherGuestId, results });
    return Promise.resolve({ results });
  }
  createMeeting(meeting) {
    this.logger.warnGroup('createMeeting - NOT IMPLEMENTED', { meeting });
    return Promise.resolve({});
  }
  createAvailability(availability) {
    this.logger.warnGroup('createAvailability - NOT IMPLEMENTED', { availability });
    return Promise.resolve({});
  }
  updateAvailability(availability) {
    this.logger.warnGroup('updateAvailability - NOT IMPLEMENTED', { availability });
    return Promise.resolve({});
  }
  deleteAvailability(id) {
    this.logger.warnGroup('deleteAvailability - NOT IMPLEMENTED', { id });
    return Promise.resolve({});
  }
}
