export const doorbirdDomain = 'doorbird';
export const doorbirdDefaultPort = 80;
export const doorbirdDefaultHttpsPort = 443;
export const doorbirdDefaultRtspPort = 554;
export const doorbirdRtspOverHttpPort = 8557;
export const doorbirdDefaultTimeoutMs = 10000;
export const doorbirdDefaultSnapshotTimeoutMs = 15000;
export const doorbirdManufacturer = 'Bird Home Automation Group';
export const doorbirdOui = '1ccae3';
export const doorbirdMdnsType = '_axis-video._tcp';

export type TDoorbirdProtocol = 'http' | 'https';
export type TDoorbirdSnapshotSource = 'http' | 'client' | 'manual' | 'snapshot' | 'runtime';
export type TDoorbirdCameraKind = 'live' | 'last_ring' | 'last_motion';
export type TDoorbirdCommandAction =
  | 'refresh'
  | 'snapshot_image'
  | 'stream_source'
  | 'energize_relay'
  | 'turn_light_on'
  | 'doorbell_state'
  | 'motion_sensor_state'
  | 'favorites'
  | 'change_favorite'
  | 'delete_favorite'
  | 'schedule'
  | 'change_schedule'
  | 'delete_schedule'
  | 'reset_favorites';

export interface IDoorbirdConfig {
  host?: string;
  port?: number;
  protocol?: TDoorbirdProtocol;
  ssl?: boolean;
  tls?: boolean;
  username?: string;
  password?: string;
  timeoutMs?: number;
  snapshotTimeoutMs?: number;
  name?: string;
  uniqueId?: string;
  manufacturer?: string;
  model?: string;
  macAddress?: string;
  relays?: Array<string | number>;
  events?: string[];
  token?: string;
  customUrl?: string;
  eventBaseUrl?: string;
  rtspPort?: number;
  snapshot?: IDoorbirdSnapshot;
  rawInfo?: IDoorbirdInfo;
  status?: IDoorbirdStatus;
  favorites?: IDoorbirdFavorites;
  schedule?: IDoorbirdScheduleEntry[];
  client?: IDoorbirdClientLike;
  commandExecutor?: IDoorbirdCommandExecutor;
  online?: boolean;
}

export interface IHomeAssistantDoorbirdConfig extends IDoorbirdConfig {}

export interface IDoorbirdClientLike {
  getSnapshot?: () => Promise<IDoorbirdSnapshot | Partial<IDoorbirdRawData>>;
  getInfo?: () => Promise<IDoorbirdInfo>;
  getDoorbellState?: () => Promise<boolean>;
  getMotionSensorState?: () => Promise<boolean>;
  getSnapshotImage?: (cameraIdArg?: string) => Promise<IDoorbirdSnapshotImage>;
  energizeRelay?: (relayArg: string) => Promise<boolean | unknown>;
  turnLightOn?: () => Promise<boolean | unknown>;
  favorites?: () => Promise<IDoorbirdFavorites>;
  changeFavorite?: (favoriteArg: IDoorbirdFavoriteChange) => Promise<boolean | unknown>;
  deleteFavorite?: (favoriteTypeArg: string, favoriteIdArg: string) => Promise<boolean | unknown>;
  schedule?: () => Promise<IDoorbirdScheduleEntry[]>;
  changeSchedule?: (entryArg: IDoorbirdScheduleEntry) => Promise<boolean | unknown>;
  deleteSchedule?: (inputArg: string, paramArg?: string) => Promise<boolean | unknown>;
  resetFavorites?: () => Promise<boolean | unknown>;
}

export interface IDoorbirdCommandExecutor {
  execute(requestArg: IDoorbirdCommandRequest): Promise<unknown>;
}

export interface IDoorbirdCommandRequest {
  action: TDoorbirdCommandAction;
  service?: string;
  target?: {
    entityId?: string;
    deviceId?: string;
  };
  data?: Record<string, unknown>;
  relay?: string;
  cameraId?: string;
  favorite?: IDoorbirdFavoriteChange;
  favoriteType?: string;
  favoriteId?: string;
  scheduleEntry?: IDoorbirdScheduleEntry;
  scheduleInput?: string;
  scheduleParam?: string;
  path?: string;
  method?: 'GET' | 'POST';
}

export interface IDoorbirdRefreshResult {
  success: boolean;
  snapshot?: IDoorbirdSnapshot;
  error?: string;
  data?: Record<string, unknown>;
}

export interface IDoorbirdRawData {
  info?: IDoorbirdInfo;
  status?: IDoorbirdStatus;
  favorites?: IDoorbirdFavorites;
  schedule?: IDoorbirdScheduleEntry[];
}

export interface IDoorbirdInfo {
  FIRMWARE?: string;
  BUILD_NUMBER?: string;
  'DEVICE-TYPE'?: string;
  DEVICE_TYPE?: string;
  RELAYS?: Array<string | number>;
  PRIMARY_MAC_ADDR?: string;
  WIFI_MAC_ADDR?: string;
  [key: string]: unknown;
}

export interface IDoorbirdStatus {
  doorbell?: boolean;
  motion?: boolean;
  rawDoorbell?: string;
  rawMotion?: string;
  updatedAt?: string;
}

export interface IDoorbirdDeviceInfo {
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  firmware?: string;
  buildNumber?: string;
  swVersion?: string;
  macAddress?: string;
  primaryMacAddress?: string;
  wifiMacAddress?: string;
  host?: string;
  port?: number;
  protocol?: TDoorbirdProtocol;
  baseUrl?: string;
  rtspUrl?: string;
  html5ViewerUrl?: string;
  relays: string[];
  online?: boolean;
}

export interface IDoorbirdCameraSnapshot {
  id: TDoorbirdCameraKind;
  name: string;
  kind: TDoorbirdCameraKind;
  stillImageUrl?: string;
  liveVideoUrl?: string;
  rtspUrl?: string;
  historyEvent?: 'doorbell' | 'motionsensor';
  historyIndex?: number;
  available?: boolean;
}

export interface IDoorbirdButtonSnapshot {
  key: string;
  name: string;
  action: 'energize_relay' | 'turn_light_on' | 'reset_favorites';
  relay?: string;
  entityCategory?: string;
  available?: boolean;
}

export interface IDoorbirdEventSnapshot {
  key: string;
  name: string;
  eventType: 'doorbell' | 'motion' | string;
  doorbirdEvent: string;
  available?: boolean;
}

export interface IDoorbirdSnapshot {
  deviceInfo: IDoorbirdDeviceInfo;
  cameras: IDoorbirdCameraSnapshot[];
  buttons: IDoorbirdButtonSnapshot[];
  events: IDoorbirdEventSnapshot[];
  status: IDoorbirdStatus;
  rawInfo?: IDoorbirdInfo;
  favorites?: IDoorbirdFavorites;
  schedule?: IDoorbirdScheduleEntry[];
  online: boolean;
  updatedAt?: string;
  source?: TDoorbirdSnapshotSource;
  error?: string;
}

export interface IDoorbirdSnapshotImage {
  contentType: string;
  data: Uint8Array;
}

export interface IDoorbirdFavorite {
  title?: string;
  value?: string;
  [key: string]: unknown;
}

export interface IDoorbirdFavorites {
  [favoriteType: string]: Record<string, IDoorbirdFavorite> | undefined;
}

export interface IDoorbirdFavoriteChange {
  type: string;
  title: string;
  value: string;
  id?: string;
}

export interface IDoorbirdScheduleEntry {
  input: string;
  param?: string;
  output: IDoorbirdScheduleOutput[];
  [key: string]: unknown;
}

export interface IDoorbirdScheduleOutput {
  enabled?: boolean | string;
  event?: string;
  param?: string;
  schedule?: Record<string, unknown>;
  [key: string]: unknown;
}

export interface IDoorbirdManualEntry {
  host?: string;
  url?: string;
  port?: number;
  protocol?: TDoorbirdProtocol;
  ssl?: boolean;
  tls?: boolean;
  username?: string;
  password?: string;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  macAddress?: string;
  rawInfo?: IDoorbirdInfo;
  status?: IDoorbirdStatus;
  snapshot?: IDoorbirdSnapshot;
  client?: IDoorbirdClientLike;
  commandExecutor?: IDoorbirdCommandExecutor;
  metadata?: Record<string, unknown>;
}

export interface IDoorbirdMdnsRecord {
  type?: string;
  serviceType?: string;
  name?: string;
  host?: string;
  port?: number;
  addresses?: string[];
  ipAddress?: string;
  hostname?: string;
  txt?: Record<string, string | undefined>;
  properties?: Record<string, string | undefined>;
}
