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

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

enum keys {
 // UserId = 'a',
  AccountName = 'a',
  Username = 'b',
  Description = 'c',
  FollowersCount = 'd',
  FollowingCount = 'e',
  lastUpdateTimestamp = 'f',
  ProfilePicture = 'g',
  ProfilePictureBinaryLQ = 'h',
  ProfilePictureBinaryHQ = 'i',

  XpLevel = 'j',
  XpPoints = 'k',
}

const name = 'users';
const primaryKey: keyof typeof propsDefault = keys.AccountName;

const propsType: {[key in keyof typeof propsDefault]: string} = {
  [keys.AccountName]: 'string',
  [keys.Username]: 'string',
  [keys.Description]: 'string',
  [keys.FollowersCount]: 'int',
  [keys.FollowingCount]: 'int',
  [keys.lastUpdateTimestamp]: 'int',
  [keys.ProfilePicture]: 'string', //URL
  [keys.ProfilePictureBinaryLQ]: 'data',
  [keys.ProfilePictureBinaryHQ]: 'data',

  [keys.XpLevel]: 'int',
  [keys.XpPoints]: 'int',
};

const propsDefault = {
  [keys.AccountName]: '',
  [keys.Username]: '',
  [keys.Description]: '',
  [keys.FollowersCount]: 0,
  [keys.FollowingCount]: 0,
  [keys.lastUpdateTimestamp]: 0,
  [keys.ProfilePicture]: '', //URL
  [keys.ProfilePictureBinaryLQ]: new ArrayBuffer(0),
  [keys.ProfilePictureBinaryHQ]: new ArrayBuffer(0),
  [keys.XpLevel]: 0,
  [keys.XpPoints]: 0,
};

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;
