export const heosDefaultPort = 1255;

export type THeosPlayState = 'play' | 'pause' | 'stop' | 'unknown' | string;
export type THeosRepeatType = 'on_all' | 'on_one' | 'off' | string;
export type THeosMediaType =
  | 'album'
  | 'artist'
  | 'container'
  | 'dlna_server'
  | 'genre'
  | 'heos_server'
  | 'heos_service'
  | 'music_service'
  | 'playlist'
  | 'song'
  | 'station'
  | string;
export type THeosNetworkType = 'wired' | 'wifi' | 'unknown' | string;
export type THeosCommand =
  | 'play'
  | 'pause'
  | 'stop'
  | 'previous_track'
  | 'next_track'
  | 'volume_up'
  | 'volume_down'
  | 'set_volume'
  | 'mute'
  | 'select_source'
  | 'play_media'
  | 'play_preset'
  | 'play_input'
  | 'set_group'
  | 'join'
  | 'unjoin'
  | 'group_volume_set'
  | 'group_volume_up'
  | 'group_volume_down';

export interface IHeosConfig {
  host?: string;
  port?: number;
  name?: string;
  username?: string;
  password?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  timeoutMs?: number;
  snapshot?: IHeosSnapshot;
  commandExecutor?: IHeosCommandExecutor;
}

export interface IHomeAssistantHeosConfig extends IHeosConfig {}

export interface IHeosCommandExecutor {
  execute(requestArg: IHeosRawCommandRequest): Promise<IHeosMessage | unknown>;
}

export interface IHeosRawCommandRequest {
  command: string;
  parameters: Record<string, string | number | boolean>;
  uri: string;
  host?: string;
  port: number;
}

export interface IHeosCommandRequest {
  command: THeosCommand;
  playerId?: number;
  groupId?: number;
  playerIds?: number[];
  source?: string;
  mediaId?: string;
  mediaType?: string;
  preset?: number;
  url?: string;
  volumeLevel?: number;
  volume?: number;
  muted?: boolean;
  step?: number;
}

export interface IHeosMessage {
  command: string;
  result: boolean;
  message: Record<string, string>;
  payload?: unknown;
  options?: unknown;
  raw?: string;
}

export interface IHeosSystemInfo {
  host?: string;
  currentHost?: string;
  signedInUsername?: string;
  isSignedIn?: boolean;
  hosts?: IHeosHost[];
  preferredHosts?: IHeosHost[];
}

export interface IHeosHost {
  name: string;
  model: string;
  serial?: string;
  version?: string;
  ipAddress?: string;
  network?: THeosNetworkType;
  supportedVersion?: boolean;
  preferredHost?: boolean;
}

export interface IHeosPlayer {
  name: string;
  playerId: number;
  model: string;
  serial?: string;
  version?: string;
  supportedVersion?: boolean;
  ipAddress?: string;
  network?: THeosNetworkType;
  lineOut?: number | string;
  control?: number | string;
  state?: THeosPlayState;
  volume?: number;
  muted?: boolean;
  repeat?: THeosRepeatType;
  shuffle?: boolean;
  playbackError?: string;
  nowPlayingMedia?: IHeosNowPlayingMedia;
  available?: boolean;
  groupId?: number;
}

export interface IHeosNowPlayingMedia {
  type?: THeosMediaType;
  song?: string;
  station?: string;
  album?: string;
  artist?: string;
  imageUrl?: string;
  albumId?: string;
  mediaId?: string;
  queueId?: number;
  sourceId?: number;
  currentPosition?: number;
  duration?: number;
  supportedControls?: string[];
}

export interface IHeosGroup {
  name: string;
  groupId: number;
  leadPlayerId: number;
  memberPlayerIds: number[];
  volume?: number;
  muted?: boolean;
}

export interface IHeosMediaBase {
  sourceId: number;
  name: string;
  type: THeosMediaType;
  imageUrl?: string;
}

export interface IHeosMusicSource extends IHeosMediaBase {
  available?: boolean;
  serviceUsername?: string;
}

export interface IHeosMediaItem extends IHeosMediaBase {
  playable?: boolean;
  browsable?: boolean;
  containerId?: string;
  mediaId?: string;
  artist?: string;
  album?: string;
  albumId?: string;
}

export interface IHeosQueueItem {
  queueId: number;
  song?: string;
  album?: string;
  artist?: string;
  imageUrl?: string;
  mediaId?: string;
  albumId?: string;
}

export interface IHeosSnapshot {
  system: IHeosSystemInfo;
  players: IHeosPlayer[];
  groups?: IHeosGroup[];
  musicSources?: IHeosMusicSource[];
  favorites?: Record<number, IHeosMediaItem>;
  inputSources?: IHeosMediaItem[];
  sourceList?: string[];
  lastUpdated?: string;
}

export interface IHeosSsdpRecord {
  st?: string;
  usn?: string;
  location?: string;
  headers?: Record<string, string | undefined>;
  upnp?: Record<string, string | undefined>;
}

export interface IHeosMdnsRecord {
  name?: string;
  type?: string;
  host?: string;
  port?: number;
  txt?: Record<string, string | undefined>;
}

export interface IHeosManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  username?: string;
  password?: string;
  metadata?: Record<string, unknown>;
}
