import {timestamp, UserId} from '@configs/types';
import DBSchemas from '@helper/storage/bdm/schemas';
import LangFormat from '@lang/default';
import {timeStamp} from 'console';

export enum chatTags {
  GROUP = 0,
  FRIEND = 1,
  GOOD_FRIEND = 2,
}

interface tagUIType {
  name: string;
  background: string;
  textColor?: string;
}

interface tagUITypeFinal extends tagUIType {
  textColor: string;
}

const tagUI: {[key in chatTags]: tagUIType} = {
  [chatTags.GROUP]: {
    name: 'Group',
    background: '#37f',
  },
  [chatTags.FRIEND]: {
    name: 'Friend',
    background: 'lime.300',
    textColor: '#111',
  },
  [chatTags.GOOD_FRIEND]: {
    name: 'Good friend',
    background: 'lime.600',
  },
};

/*export function getTagUI(tagKey: chatTags, lang?: LangFormat): tagUITypeFinal {
  let tag = tagUI[tagKey];

  tag.textColor = tag.textColor || '#fff';

  if (lang !== undefined && lang.chat.tags.names[tagKey] !== undefined) {
    tag.name = lang.chat.tags.names[tagKey];
  }

  return tag as tagUITypeFinal;
}*/

export type roomId = string;
type syncId = number;

export interface chatEntity {
  roomId: roomId;

  syncId: syncId;
  initSyncId: syncId;
  timestamp: timestamp;

  users: [UserId, ...UserId[]];
  title?: string;
  unreadMessages: number;

  tags: chatTags[];
}

export function convertDatabaseChat(
  chat: typeof DBSchemas.chatRoomInfos.defaultProps,
): chatEntity | undefined {
  const keys = DBSchemas.chatRoomInfos.keys;

  if (chat[keys.users].length === 0) return undefined;

  const newChat: chatEntity = {
    initSyncId: chat[keys.initSyncId],
    roomId: chat[keys.RoomId],
    syncId: chat[keys.syncId],
    tags: [],
    unreadMessages: chat[keys.unreadMessages],
    timestamp: chat[keys.timestamp],
    //@ts-ignore
    users: [...chat[keys.users]],
    //title: chat[keys.title]
  };

  return newChat;
}

export function convertChatDatabase(
  chat: chatEntity,
): typeof DBSchemas.chatRoomInfos.defaultProps {
  const keys = DBSchemas.chatRoomInfos.keys;

  const newChat: typeof DBSchemas.chatRoomInfos.defaultProps = {
    [keys.initSyncId]: chat.initSyncId,
    [keys.RoomId]: chat.roomId,
    [keys.syncId]: chat.syncId,

    [keys.unreadMessages]: chat.unreadMessages,
    [keys.timestamp]: chat.timestamp,
    //@ts-ignore
    [keys.users]: chat.users,
    //title: chat[keys.title]
  };

  return newChat;
}
