export type TDlnaDmrDeviceType =
  | 'urn:schemas-upnp-org:device:MediaRenderer:1'
  | 'urn:schemas-upnp-org:device:MediaRenderer:2'
  | 'urn:schemas-upnp-org:device:MediaRenderer:3';

export type TDlnaDmrServiceKey = 'AVTransport' | 'RenderingControl' | 'ConnectionManager';

export type TDlnaDmrTransportState =
  | 'STOPPED'
  | 'PLAYING'
  | 'TRANSITIONING'
  | 'PAUSED_PLAYBACK'
  | 'PAUSED_RECORDING'
  | 'RECORDING'
  | 'NO_MEDIA_PRESENT'
  | 'VENDOR_DEFINED';

export type TDlnaDmrMediaPlayerState = 'off' | 'on' | 'idle' | 'playing' | 'paused' | 'unknown';

export type TDlnaDmrCommandName =
  | 'play'
  | 'pause'
  | 'stop'
  | 'set_volume'
  | 'set_mute'
  | 'select_source'
  | 'set_uri';

export interface IDlnaDmrConfig {
  location?: string;
  url?: string;
  host?: string;
  port?: number;
  path?: string;
  deviceId?: string;
  udn?: string;
  deviceType?: TDlnaDmrDeviceType | string;
  name?: string;
  manufacturer?: string;
  model?: string;
  modelNumber?: string;
  serialNumber?: string;
  macAddress?: string;
  timeoutMs?: number;
  services?: Partial<Record<TDlnaDmrServiceKey, IDlnaDmrServiceDescription>>;
  snapshot?: IDlnaDmrSnapshot;
}

export interface IHomeAssistantDlnaDmrConfig extends IDlnaDmrConfig {}

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

export interface IDlnaDmrDeviceDescription {
  location?: string;
  udn: string;
  deviceType: TDlnaDmrDeviceType | string;
  friendlyName: string;
  manufacturer?: string;
  modelName?: string;
  modelNumber?: string;
  serialNumber?: string;
  macAddress?: string;
  services: Partial<Record<TDlnaDmrServiceKey, IDlnaDmrServiceDescription>>;
  icons?: IDlnaDmrDeviceIcon[];
}

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

export interface IDlnaDmrTransportInfo {
  currentTransportState?: TDlnaDmrTransportState | string;
  currentTransportStatus?: string;
  currentSpeed?: string;
  currentTransportActions?: string[];
  currentPlayMode?: string;
}

export interface IDlnaDmrRenderingState {
  volume?: number;
  muted?: boolean;
  presets?: string[];
  selectedPreset?: string;
}

export interface IDlnaDmrMediaMetadata {
  title?: string;
  artist?: string;
  album?: string;
  albumArtist?: string;
  albumArtUri?: string;
  upnpClass?: string;
  contentType?: string;
  duration?: number;
  raw?: string;
}

export interface IDlnaDmrPositionInfo {
  track?: number;
  trackDuration?: string;
  trackDurationSeconds?: number;
  trackMetaData?: string;
  trackUri?: string;
  relativeTime?: string;
  relativeTimeSeconds?: number;
}

export interface IDlnaDmrMediaState {
  currentUri?: string;
  currentUriMetaData?: string;
  currentTrackUri?: string;
  mediaClass?: string;
  metadata?: IDlnaDmrMediaMetadata;
  position?: IDlnaDmrPositionInfo;
}

export interface IDlnaDmrRendererState {
  online: boolean;
  transport: IDlnaDmrTransportInfo;
  rendering: IDlnaDmrRenderingState;
  media: IDlnaDmrMediaState;
  sinkProtocolInfo?: string[];
  updatedAt?: string;
}

export interface IDlnaDmrSnapshot {
  device: IDlnaDmrDeviceDescription;
  state: IDlnaDmrRendererState;
}

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

export interface IDlnaDmrCommand {
  name: TDlnaDmrCommandName;
  uri?: string;
  metadata?: string;
  title?: string;
  volume?: number;
  muted?: boolean;
  source?: string;
  autoplay?: boolean;
}

export interface IDlnaDmrEvent {
  service: TDlnaDmrServiceKey | string;
  variables: Record<string, unknown>;
  snapshot?: IDlnaDmrSnapshot;
}

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

export interface IDlnaDmrSsdpServiceList {
  service?: IDlnaDmrSsdpService | IDlnaDmrSsdpService[];
}

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

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

export interface IDlnaDmrDiscoveryMetadata {
  st?: string;
  nt?: string;
  usn?: string;
  location?: string;
  deviceType?: string;
  serviceIds?: string[];
}
