export const dlnaDmsDomain = 'dlna_dms';
export const dlnaDmsRootObjectId = '0';
export const dlnaDmsDefaultPort = 80;
export const dlnaDmsDefaultTimeoutMs = 10000;

export type TDlnaDmsDeviceType =
  | 'urn:schemas-upnp-org:device:MediaServer:1'
  | 'urn:schemas-upnp-org:device:MediaServer:2'
  | 'urn:schemas-upnp-org:device:MediaServer:3'
  | 'urn:schemas-upnp-org:device:MediaServer:4'
  | (string & {});

export type TDlnaDmsServiceKey = 'ContentDirectory' | 'ConnectionManager';
export type TDlnaDmsSnapshotSource = 'snapshot' | 'client' | 'manual' | 'upnp' | 'runtime';
export type TDlnaDmsContentObjectType = 'container' | 'item';

export interface IDlnaDmsConfig {
  location?: string;
  url?: string;
  host?: string;
  port?: number;
  path?: string;
  timeoutMs?: number;
  deviceId?: string;
  udn?: string;
  deviceType?: TDlnaDmsDeviceType;
  sourceId?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  modelName?: string;
  modelNumber?: string;
  serialNumber?: string;
  services?: Partial<Record<TDlnaDmsServiceKey, IDlnaDmsServiceDescription>>;
  maxBrowseChildren?: number;
  snapshot?: IDlnaDmsSnapshot;
  client?: IDlnaDmsClientLike;
  online?: boolean;
}

export interface IHomeAssistantDlnaDmsConfig extends IDlnaDmsConfig {}

export interface IDlnaDmsClientLike {
  getSnapshot?: () => Promise<IDlnaDmsSnapshot>;
  snapshot?: () => Promise<IDlnaDmsSnapshot>;
}

export interface IDlnaDmsServiceDescription {
  serviceId: string;
  serviceType: string;
  controlUrl: string;
  eventSubUrl?: string;
  scpdUrl?: string;
}

export interface IDlnaDmsDeviceIcon {
  mimeType?: string;
  width?: number;
  height?: number;
  depth?: number;
  url?: string;
}

export interface IDlnaDmsDeviceDescription {
  location?: string;
  udn: string;
  usn?: string;
  deviceType: TDlnaDmsDeviceType;
  friendlyName: string;
  manufacturer?: string;
  modelName?: string;
  modelNumber?: string;
  serialNumber?: string;
  sourceId?: string;
  services: Partial<Record<TDlnaDmsServiceKey, IDlnaDmsServiceDescription>>;
  icons?: IDlnaDmsDeviceIcon[];
}

export interface IDlnaDmsResource {
  uri?: string;
  protocolInfo?: string;
  mimeType?: string;
  size?: number;
  duration?: string;
  bitrate?: number;
  resolution?: string;
}

export interface IDlnaDmsContentObject {
  id: string;
  parentId?: string;
  type: TDlnaDmsContentObjectType;
  title: string;
  upnpClass?: string;
  mediaClass?: string;
  childCount?: number;
  restricted?: boolean;
  searchable?: boolean;
  albumArtUri?: string;
  thumbnail?: string;
  canPlay?: boolean;
  canExpand?: boolean;
  resources: IDlnaDmsResource[];
  raw?: Record<string, unknown>;
}

export interface IDlnaDmsBrowseResult {
  objects: IDlnaDmsContentObject[];
  totalMatches?: number;
  numberReturned?: number;
  updateId?: number;
}

export interface IDlnaDmsContentDirectorySnapshot {
  root?: IDlnaDmsContentObject;
  children: IDlnaDmsContentObject[];
  totalMatches?: number;
  numberReturned?: number;
  updateId?: number;
}

export interface IDlnaDmsStatus {
  online: boolean;
  contentDirectoryAvailable: boolean;
  connectionManagerAvailable: boolean;
  systemUpdateId?: number;
  searchCapabilities?: string[];
  sortCapabilities?: string[];
  sourceProtocolInfo?: string[];
  sinkProtocolInfo?: string[];
  currentConnectionIds?: string[];
  updatedAt?: string;
  error?: string;
}

export interface IDlnaDmsSnapshot {
  device: IDlnaDmsDeviceDescription;
  status: IDlnaDmsStatus;
  contentDirectory: IDlnaDmsContentDirectorySnapshot;
  libraries: IDlnaDmsContentObject[];
  online: boolean;
  updatedAt?: string;
  source?: TDlnaDmsSnapshotSource;
  error?: string;
}

export interface IDlnaDmsSoapCommand {
  service: TDlnaDmsServiceKey;
  action: string;
  args: Record<string, string | number | boolean | undefined>;
}

export interface IDlnaDmsSsdpRecord {
  st?: string;
  nt?: string;
  usn?: string;
  udn?: string;
  ssdpUdn?: string;
  location?: string;
  deviceType?: string;
  headers?: Record<string, string | undefined>;
  upnp?: {
    deviceType?: string;
    friendlyName?: string;
    manufacturer?: string;
    modelName?: string;
    serviceList?: IDlnaDmsSsdpServiceList;
    [key: string]: unknown;
  };
}

export interface IDlnaDmsSsdpServiceList {
  service?: IDlnaDmsSsdpService | IDlnaDmsSsdpService[];
}

export interface IDlnaDmsSsdpService {
  serviceId?: string;
  serviceType?: string;
}

export interface IDlnaDmsManualEntry {
  url?: string;
  location?: string;
  host?: string;
  port?: number;
  path?: string;
  id?: string;
  deviceId?: string;
  udn?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  metadata?: Record<string, unknown>;
}
