import type { Profile } from './types';

export type ContactStatus = {
  token: string;
  status: string;
  viewRevision: number;
  channelRevision: number;
  profileRevision: number;
  articleRevision: number;
};

export type DataMessage = {
  message: string;
  keyType: string;
  publicKey: string;
  signature: string;
  signatureType: string;
};

export type CardDetailEntity = {
  status: string;
  statusUpdated: number;
  token: string;
  notes: string;
  groups: [string];
};

export type CardProfileEntity = {
  guid: string;
  handle: string;
  name: string;
  description: string;
  location: string;
  imageSet: boolean;
  version: string;
  node: string;
  seal: string;
  revision: number;
};

export type CardEntity = {
  id: string;
  revision: number;
  data?: {
    detailRevision: number;
    profileRevision: number;
    notifiedProfile: number;
    notifiedArticle: number;
    notifiedChannel: number;
    cardDetail?: CardDetailEntity;
    cardProfile?: CardProfileEntity;
  };
};

export type ChannelSummaryEntity = {
  lastTopic: {
    guid: string;
    dataType: string;
    data: string;
    created: number;
    updated: number;
    status: string;
    transform: string;
  };
};

export type ChannelDetailEntity = {
  dataType: string;
  data: string;
  created: number;
  updated: number;
  enableImage: boolean;
  enableAudio: boolean;
  enableVideo: boolean;
  enableBinary: boolean;
  contacts: {
    groups: string[];
    cards: string[];
  };
  members: string[];
};

export type ChannelEntity = {
  id: string;
  revision: number;
  data: {
    detailRevision: number;
    topicRevision: number;
    channelSummary?: ChannelSummaryEntity;
    channelDetail?: ChannelDetailEntity;
  };
};

export type TopicDetailEntity = {
  guid: string;
  dataType: string;
  data: string;
  created: number;
  updated: number;
  status: string;
  transform: string;
}

export type TopicEntity = {
  id: string;
  revision: number;
  data?: {
    detailRevision: number;
    tagRevision: number;
    topicDetail: TopicDetailEntity;
  };
};

export type TagEntity = {
  id: string;
  revision: number;
  data?: {
    guid: string;
    dataType: string;
    data: string;
    created: number;
    updated: number;
  };
};

export type AssetEntity = {
  assetId: string;
  transform: string;
  status: string;
};

export type RepeaterEntity = {
  id: string;
  revision: number;
  data?: {
    name: string;
    token: string;
  };
};

export type GroupEntity = {
  id: string;
  revision: number;
  data?: {
    dataType: string;
    data: string;
    created: number;
    updated: number;
    cards: [string];
  };
};

export type ArticleEntity = {
  id: string;
  revision: number;
  data?: {
    dataType: string;
    data: string;
    created: number;
    updated: number;
    status: string;
    contacts?: {
      cards: [string];
      groups: [string];
    };
  };
};

export type SealEntity = {
  passwordSalt: string;
  privateKeyIv: string;
  privateKeyEncrypted: string;
  publicKey: string;
};

export type ConfigEntity = {
  disabled: boolean;
  storageUsed: number;
  storageAvailable: number;
  forwardingAddress: string;
  searchable: boolean;
  allowUnsealed: boolean;
  pushEnabled: boolean;
  sealable: boolean;
  seal: SealEntity;
  enableIce: boolean;
  mfaEnabled: boolean;
  webPushKey: string;
};

export const defaultConfigEntity = {
  disabled: false,
  storageUsed: 0,
  storageAvailable: 0,
  forwardingAddress: '',
  searchable: false,
  allowUnsealed: false,
  pushEnabled: false,
  seal: {
    passwordSalt: '',
    privateKeyIv: '',
    privateKeyEncrypted: '',
    publicKey: '',
  },
  sealable: false,
  enableIce: false,
  mfaEnabled: false,
  webPushKey: '',
};

export type ProfileEntity = {
  guid: string;
  handle: string;
  name: string;
  description: string;
  location: string;
  image: string;
  revision: number;
  seal?: string;
  version: string;
  node: string;
};

export type AccountEntity = {
  accountId: number;
  guid: string;
  handle: string;
  name: string;
  description: string;
  location: string;
  imageSet: boolean;
  revision: number;
  seal?: string;
  version: string;
  node: string;
  storageUsed: number;
  disabled: boolean;
};

export const defaultProfileEntity = {
  guid: '',
  handle: '',
  name: '',
  description: '',
  location: '',
  image: '',
  revision: 0,
  version: '',
  node: '',
};

export type SetupEntity = {
  domain: string;
  accountStorage: number;
  enableImage: boolean;
  enableAudio: boolean;
  enableVideo: boolean;
  enableBinary: boolean;
  keyType: string;
  pushSupported: boolean;
  allowUnsealed: boolean;
  transformSupported: boolean;
  enableIce: boolean;
  iceService: string;
  iceUrl: string;
  iceUsername: string;
  icePassword: string;
  enableOpenAccess: boolean;
  openAccessLimit: number;
};

export type Calling = {
  id: string;
  cardId: string;
  callerToken: string;
  calleeToken: string;
  keepAlive: number;
  ice: { urls: string; username: string; credential: string }[];
}

export type Ringing = {
  callId: string;
  calleeToken: string;
  ice: { urls: string[]; username: string; credential: string }[];
  iceUrl?: string;
  iceUsername?: string;
  icePassword?: string;
};

export type Revision = {
  account: number;
  profile: number;
  article: number;
  group: number;
  channel: number;
  card: number;
};

export type Login = {
  guid: string;
  node: string;
  secure: boolean;
  token: string;
  timestamp: number;
  pushSupported: boolean;
};

export type BasicAsset = {
  encrypted?: { type: string, thumb: string, label: string, extension: string, parts: { blockIv: string, partId: string }[] },
  image?: { thumb: string, full: string },
  audio?: { label: string, full: string },
  video?: { thumb: string, lq: string, hd: string },
  binary?: { label: string, extension: string, data: string }
}

export type BasicEntity = {
  text: string,
  textColor: string,
  textSize: string,
  assets: BasicAsset[], 
}

export type SealedBasicEntity = {
  messageEncrypted: string,
  messageIv: string
}

export const avatar =
  'data:image/png;base64, ';
