import {filterParam, getAllEntries, getEntry} from '../get';
import {DBMigration} from '../migration';
import {setEntry} from '../set';

import {databaseConf, possibleDBKeys} from '../types';

enum keys {
  UUID = 'a',
  Name = 'b',
  Description = 'c',
  StartDate = 'd',
  EndDate = 'e',
  Latitude = 'f',
  Longitude = 'g',
  Type = 'h',
  Theme = 'i',
  lastUpdateTimestamp = 'j',
  FriendList = 'k',
  UserLength = 'l',
  EventBannerPicture = 'm', //URL
  EventBannerPictureBinaryLQ = 'n',
  EventBannerPictureBinaryHQ = 'o',
  ButtonAction = 'p',
}

const name = 'events';
const primaryKey: keyof typeof propsDefault = keys.UUID;

const propsType: {[key in keyof typeof propsDefault]: string} = {
  [keys.UUID]: 'string',
  [keys.Name]: 'string',
  [keys.Description]: 'string',
  [keys.StartDate]: 'int',
  [keys.EndDate]: 'int',
  [keys.Latitude]: 'double',
  [keys.Longitude]: 'double',
  [keys.Type]: 'int',
  [keys.Theme]: 'int',
  [keys.lastUpdateTimestamp]: 'int',
  [keys.FriendList]: 'string[]',
  [keys.UserLength]: 'int',
  [keys.EventBannerPicture]: 'string', //URL
  [keys.EventBannerPictureBinaryLQ]: 'data',
  [keys.EventBannerPictureBinaryHQ]: 'data',
  [keys.ButtonAction]: 'string',
};

const propsDefault = {
  [keys.UUID]: '',
  [keys.Name]: '',
  [keys.Description]: '',
  [keys.StartDate]: 0,
  [keys.EndDate]: 0,
  [keys.Latitude]: 0,
  [keys.Longitude]: 0,
  [keys.Type]: 0,
  [keys.Theme]: 0,
  [keys.lastUpdateTimestamp]: 0,
  [keys.FriendList]: ['none'],
  [keys.UserLength]: 0,
  [keys.EventBannerPicture]: '', //URL
  [keys.EventBannerPictureBinaryLQ]: new ArrayBuffer(0),
  [keys.EventBannerPictureBinaryHQ]: new ArrayBuffer(0),
  [keys.ButtonAction]: '',
};

const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
  filePath: name,
  version: 1,
  keys,
  migration: () => {
    return DBMigration[name](thisSchema);
  },
  setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => {
    return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
      thisSchema,
      val,
      suffix,
    );
  },
  getEntry: (key: possibleDBKeys, suffix?: string) => {
    return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
      thisSchema,
      key,
      suffix,
    );
  },
  getAllEntries: (filter?: filterParam, suffix?: string) => {
    return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
      thisSchema,
      filter,
      suffix,
    );
  },
  defaultProps: propsDefault,
  details: {
    name,
    properties: propsType,
    primaryKey,
  },
};

export default thisSchema;
