import { ResourceType } from "src/app/components/resources-sidebar/sidebar-data";
import { RoomData } from "../room.service";
import {
  SubscriptionServiceEvent,
  SubscriptionServiceEventType,
} from "../subscription-event";
import { AccountAvatarRecord, MeRecord } from "./account-records";
import { ID } from "./common-records";
import { RoomRecord } from "./room-records";
import { TimedOfferData } from "./timed-offer-records";

/**
 * Raw Workspace Query records from direct the server
 *
 *
 *
 *
 *
 */
export interface RawWorkspaceRecord {
  grantedRoomIds: string[];
  me: MeRecord;
  rejectedRoomIds: string[];
  resourceGroups: RawWorkspaceQueryGroupRecord[];
  ungroupedResources: Array<
    RawWorkspaceQueryRoomRecord | RawWorkspaceQueryDialogueRecord
  >;
  waitingNanos: { [key: string]: number };
  contactAvatars: AccountAvatarRecord[];
  timedOffers: RawWorkspaceQueryTimedOffers[];
  newestNano: string;
}

export interface RawWorkspaceQueryTimedOffers {
  id: ID;
  /**
   * raw: JSON parsed data: {\"type\":\"license\",\"number\":1,\"months\":2}
   * after decryption: TimedOfferData
   */
  data: string | TimedOfferData;
  expiration: number;
}

export interface RawWorkspaceQueryGroupRecord {
  closed: boolean;
  id: ID;
  /**
   * encrypted: Uint8Array
   * decrypted: string
   */
  name: Uint8Array | string;
  ordering: number;
  resources: Array<
    RawWorkspaceQueryRoomRecord | RawWorkspaceQueryDialogueRecord
  >;
}

export interface RawWorkspaceQueryRoomRecord {
  data: Uint8Array;
  id: ID;
  ignore: boolean | null;
  nanoSessions: { [key: string]: number };
  ordering: number;
  ownerAccountId: ID;
  pinnedKey: Uint8Array;
  savedKey: Uint8Array;
  seenMessageId: ID;
  topMessageId: ID;
  type: ResourceType;
  inaccessible?: boolean;
}

export interface RawWorkspaceQueryDialogueRecord {
  hidden: boolean | null;
  ignore: boolean | null;
  id: ID;
  ordering: number;
  seenMessageId: ID;
  topMessageId: ID;
  type: ResourceType;
}

/**
 * Decrypted and parsed workspace query records
 *
 *
 *
 *
 *
 */
export interface WorkspaceQueryRecord {
  grantedRooms: Array<RoomRecord>;
  me: MeRecord;
  rejectedRooms: Array<RoomRecord>;
  resourceGroups: WorkspaceQueryResourceGroupRecord[];
  ungroupedResources: Array<
    WorkspaceQueryRoomRecord | WorkspaceQueryDialogueRecord
  >;
  waitingNanos: { [key: string]: number };
  contactAvatars: AccountAvatarRecord[];
  timedOffers: WorkspaceQueryTimedOffers[];
  newestNano: string;
}

export interface WorkspaceQueryTimedOffers
  extends RawWorkspaceQueryTimedOffers {}

export interface WorkspaceQueryResourceGroupRecord {
  closed: boolean;
  id: ID;
  name: string;
  ordering: number;
  resources: Array<WorkspaceQueryRoomRecord | WorkspaceQueryDialogueRecord>;
  error: boolean; // it is coming only from the decryption, if something is broken, error is true
  errorMessage?: string;
}

export interface WorkspaceQueryRoomRecord {
  data: RoomData;
  id: ID;
  ignore: boolean | null;
  nanoSessions: { [key: string]: number };
  ordering: number;
  ownerAccountId: ID;
  pinnedKey: Uint8Array;
  savedKey: Uint8Array;
  seenMessageId: ID;
  topMessageId: ID;
  type: ResourceType;
  inaccessible: boolean;
  public: boolean;
  error: boolean;
  errorMessage?: string;
}

export interface WorkspaceQueryDialogueRecord
  extends RawWorkspaceQueryDialogueRecord {
  account: AccountAvatarRecord;
  error: boolean;
  errorMessage?: string;
}

/**
 * Raw alive subsciption events from the server
 *
 *
 *
 *
 *
 *
 *
 */
export interface RawWorkspaceSubscriptionRecord {
  [SubscriptionServiceEvent.ACCOUNT_EVENT]?: RawWorkspaceSubscriptionAccountEventRecord;
  [SubscriptionServiceEvent.ACCOUNT_CONTACT_EVENT]?: RawWorkspaceSubscriptionAccountContactEventRecord;
  [SubscriptionServiceEvent.ACCOUNT_TRUST_EVENT]?: RawWorkspaceSubscriptionAccountTrustEventRecord;
  [SubscriptionServiceEvent.DIALOGUE_EVENT]?: RawWorkspaceSubscriptionDialogueEventRecord;
  [SubscriptionServiceEvent.DIALOGUE_MESSAGE_EVENT]?: RawWorkspaceSubscriptionDialogueMessageEventRecord;
  [SubscriptionServiceEvent.RESOURCE_GROUP_EVENT]?: RawWorkspaceSubscriptionResourceGroupEventRecord;
  [SubscriptionServiceEvent.ROOM_EVENT]?: RawWorkspaceSubscriptionRoomEventRecord;
  [SubscriptionServiceEvent.ROOM_MESSAGE_EVENT]?: RawWorkspaceSubscriptionRoomMessageEventRecord;
  [SubscriptionServiceEvent.ROOM_PERMISSION_EVENT]?: RawWorkspaceSubscriptionRoomPermissionEventRecord;
  [SubscriptionServiceEvent.ROOM_ACCOUNT_PERMISSION_EVENT]?: RawWorkspaceSubscriptionRoomAccountPermissionEventRecord;
  [SubscriptionServiceEvent.NANO_WAITING_EVENT]?: RawWorkspaceSubscriptionNanoWaitingEventRecord;
  [SubscriptionServiceEvent.ACCOUNT_OFFER_EVENT]?: RawWorkspaceSubscriptionAccountOfferEventRecord;
}

export interface WorkspaceSubscriptionRecordCMD {
  cmd: SubscriptionServiceEventType;
}

export interface RawWorkspaceSubscriptionAccountEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  scopesAdded?: string[];
  scopesRemoved?: string[];
  scopesReset?: string[];
  parentAccountId?: string; // not used/obsolete
  avatarImageKey?: string;
  avatarName?: string;
  email?: string;
  selectedStatus?: number; // not used
}

export interface RawWorkspaceSubscriptionAccountContactEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  peerId: ID;
}

export interface RawWorkspaceSubscriptionAccountTrustEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  peerId: ID;
  trust: Uint8Array;
}

export interface RawWorkspaceSubscriptionDialogueEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  ordering?: number;
  ignore?: boolean;
  hidden?: boolean;
  groupId?: ID;
  seenMessageId?: ID;
  topMessageId?: ID;
}

export interface RawWorkspaceSubscriptionDialogueMessageEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  messageId: ID;
  posterId: ID;
  /**
   * raw: Uint8Array
   * decrypted: string
   */
  content?: Uint8Array | string;
  edited?: boolean;
}

export interface RawWorkspaceSubscriptionResourceGroupEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  /**
   * raw: Uint8Array
   * decrypted: string
   */
  name?: Uint8Array | string;
  ordering?: number;
  closed?: boolean;
}

export interface RawWorkspaceSubscriptionRoomEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  topMessageId?: ID;
  /**
   * raw: Uint8Array
   * decrypted: roomDataQueryDataRecord
   */
  data?: Uint8Array | RoomData;
  nanoSessionsAdded?: { [key: string]: number };
  nanoSessionsRemoved?: string[];
  nanoSessionsReset?: { [key: string]: number };
  ordering?: number;
  ignore?: boolean;
  groupId?: ID;
  seenMessageId?: ID;
  rejected?: boolean;
}
export interface RawWorkspaceSubscriptionRoomMessageEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  messageId: ID;
  posterId: ID;
  /**
   * raw: Uint8Array
   * decrypted: string
   */
  content?: Uint8Array | string;
  edited?: boolean;
}
export interface RawWorkspaceSubscriptionRoomPermissionEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  blockId: ID;
  ownerAccountId?: ID;
  allowAnonymous?: boolean;
  msgBrdReadOnly?: boolean;
}
export interface RawWorkspaceSubscriptionRoomAccountPermissionEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  accountId: ID;
  ownerAccountId?: ID;
  msgBrdModerate?: boolean;
  administrator?: boolean;
  driveWrite?: boolean;
  driveAdhoc?: boolean;
  driveUpload?: boolean;
}
export interface RawWorkspaceSubscriptionNanoWaitingEventRecord
  extends WorkspaceSubscriptionRecordCMD {
  id: ID;
  waiterId: string;
  aliveUntil: number;
}

export interface RawWorkspaceSubscriptionAccountOfferEventRecord
  extends WorkspaceSubscriptionRecordCMD,
    RawWorkspaceQueryTimedOffers {}

/**
 * Decrypted and parsed subscription events from the server
 *
 *
 *
 *
 */

export interface WorkspaceSubscriptionRecord {
  [SubscriptionServiceEvent.ACCOUNT_EVENT]?: WorkspaceSubscriptionAccountEventRecord;
  [SubscriptionServiceEvent.ACCOUNT_CONTACT_EVENT]?: WorkspaceSubscriptionAccountContactEventRecord;
  [SubscriptionServiceEvent.ACCOUNT_TRUST_EVENT]?: WorkspaceSubscriptionAccountTrustEventRecord;
  [SubscriptionServiceEvent.DIALOGUE_EVENT]?: WorkspaceSubscriptionDialogueEventRecord;
  [SubscriptionServiceEvent.DIALOGUE_MESSAGE_EVENT]?: WorkspaceSubscriptionDialogueMessageEventRecord;
  [SubscriptionServiceEvent.RESOURCE_GROUP_EVENT]?: WorkspaceSubscriptionResourceGroupEventRecord;
  [SubscriptionServiceEvent.ROOM_EVENT]?: WorkspaceSubscriptionRoomEventRecord;
  [SubscriptionServiceEvent.ROOM_MESSAGE_EVENT]?: WorkspaceSubscriptionRoomMessageEventRecord;
  [SubscriptionServiceEvent.ROOM_PERMISSION_EVENT]?: WorkspaceSubscriptionRoomPermissionEventRecord;
  [SubscriptionServiceEvent.ROOM_ACCOUNT_PERMISSION_EVENT]?: WorkspaceSubscriptionRoomAccountPermissionEventRecord;
  [SubscriptionServiceEvent.NANO_WAITING_EVENT]?: WorkspaceSubscriptionNanoWaitingEventRecord;
  [SubscriptionServiceEvent.ACCOUNT_OFFER_EVENT]?: WorkspaceSubscriptionAccountOfferEventRecord;
}

export interface WorkspaceSubscriptionAccountEventRecord
  extends RawWorkspaceSubscriptionAccountEventRecord {}

export interface WorkspaceSubscriptionAccountContactEventRecord
  extends RawWorkspaceSubscriptionAccountContactEventRecord {}

export interface WorkspaceSubscriptionAccountTrustEventRecord
  extends RawWorkspaceSubscriptionAccountTrustEventRecord {}

export interface WorkspaceSubscriptionDialogueEventRecord
  extends RawWorkspaceSubscriptionDialogueEventRecord {}

export interface WorkspaceSubscriptionDialogueMessageEventRecord
  extends RawWorkspaceSubscriptionDialogueMessageEventRecord {
  edited?: boolean;
  error: boolean;
  errorMessage?: string;
}

export interface WorkspaceSubscriptionResourceGroupEventRecord
  extends RawWorkspaceSubscriptionResourceGroupEventRecord {
  error: boolean;
  errorMessage?: string;
}

export interface WorkspaceSubscriptionRoomEventRecord
  extends RawWorkspaceSubscriptionRoomEventRecord {
  public: boolean;
  error: boolean;
  errorMessage?: string;
}
export interface WorkspaceSubscriptionRoomMessageEventRecord
  extends RawWorkspaceSubscriptionRoomMessageEventRecord {
  error: boolean;
  errorMessage?: string;
}
export interface WorkspaceSubscriptionRoomPermissionEventRecord
  extends RawWorkspaceSubscriptionRoomPermissionEventRecord {}
export interface WorkspaceSubscriptionRoomAccountPermissionEventRecord
  extends RawWorkspaceSubscriptionRoomAccountPermissionEventRecord {}
export interface WorkspaceSubscriptionNanoWaitingEventRecord
  extends RawWorkspaceSubscriptionNanoWaitingEventRecord {}

export interface WorkspaceSubscriptionAccountOfferEventRecord
  extends RawWorkspaceSubscriptionAccountOfferEventRecord {}
