export const soundtouchDefaultPort = 8090;
export const soundtouchDefaultDlnaPort = 8091;
export const soundtouchDefaultSourceList = ['AUX', 'BLUETOOTH'] as const;

export type TSoundtouchPlayStatus = 'PLAY_STATE' | 'BUFFERING_STATE' | 'PAUSE_STATE' | 'STOP_STATE' | string;

export type TSoundtouchSource =
  | 'SLAVE_SOURCE'
  | 'INTERNET_RADIO'
  | 'PANDORA'
  | 'AIRPLAY'
  | 'STORED_MUSIC'
  | 'AUX'
  | 'OFF_SOURCE'
  | 'CURRATED_RADIO'
  | 'STANDBY'
  | 'UPDATE'
  | 'DEEZER'
  | 'SPOTIFY'
  | 'IHEART'
  | 'LOCAL_MUSIC'
  | 'BLUETOOTH'
  | 'INVALID_SOURCE'
  | string;

export type TSoundtouchContentType = 'uri' | 'track' | 'album' | 'playlist' | string;

export type TSoundtouchCommand =
  | 'turn_on'
  | 'turn_off'
  | 'play'
  | 'pause'
  | 'play_pause'
  | 'previous_track'
  | 'next_track'
  | 'volume_up'
  | 'volume_down'
  | 'set_volume'
  | 'mute'
  | 'select_source'
  | 'play_media'
  | 'play_preset';

export interface ISoundtouchConfig {
  host?: string;
  port?: number;
  dlnaPort?: number;
  name?: string;
  deviceId?: string;
  model?: string;
  macAddress?: string;
  sourceList?: string[];
  timeoutMs?: number;
  snapshot?: ISoundtouchSnapshot;
  commandExecutor?: ISoundtouchCommandExecutor;
}

export interface IHomeAssistantSoundtouchConfig extends ISoundtouchConfig {}

export interface ISoundtouchCommandExecutor {
  execute(requestArg: ISoundtouchRawCommandRequest): Promise<unknown>;
}

export interface ISoundtouchRawCommandRequest {
  command: TSoundtouchCommand | 'refresh';
  method: 'GET' | 'POST';
  path: string;
  url: string;
  host?: string;
  port: number;
  body?: string;
  headers?: Record<string, string>;
}

export interface ISoundtouchCommandRequest {
  command: TSoundtouchCommand;
  source?: string;
  presetId?: string;
  presetName?: string;
  mediaId?: string;
  mediaType?: string;
  url?: string;
  volumeLevel?: number;
  volume?: number;
  muted?: boolean;
  contentItem?: ISoundtouchContentItem;
}

export interface ISoundtouchSnapshot {
  config: ISoundtouchDeviceInfo;
  status?: ISoundtouchStatus;
  volume?: ISoundtouchVolume;
  presets?: ISoundtouchPreset[];
  zone?: ISoundtouchZoneStatus;
  sourceList?: string[];
  available?: boolean;
  lastUpdated?: string;
  error?: string;
}

export interface ISoundtouchDeviceInfo {
  deviceId?: string;
  name?: string;
  type?: string;
  accountUuid?: string;
  moduleType?: string;
  variant?: string;
  variantMode?: string;
  countryCode?: string;
  regionCode?: string;
  host?: string;
  port?: number;
  networks?: ISoundtouchNetworkInfo[];
  components?: ISoundtouchComponentInfo[];
}

export interface ISoundtouchNetworkInfo {
  type?: string;
  macAddress?: string;
  ipAddress?: string;
}

export interface ISoundtouchComponentInfo {
  category?: string;
  softwareVersion?: string;
  serialNumber?: string;
}

export interface ISoundtouchContentItem {
  name?: string;
  source?: TSoundtouchSource;
  type?: TSoundtouchContentType;
  location?: string;
  sourceAccount?: string;
  isPresetable?: boolean;
}

export interface ISoundtouchStatus {
  source?: TSoundtouchSource;
  contentItem?: ISoundtouchContentItem;
  track?: string;
  artist?: string;
  album?: string;
  image?: string;
  duration?: number;
  position?: number;
  playStatus?: TSoundtouchPlayStatus;
  shuffleSetting?: string;
  repeatSetting?: string;
  streamType?: string;
  trackId?: string;
  stationName?: string;
  description?: string;
  stationLocation?: string;
}

export interface ISoundtouchVolume {
  actual?: number;
  target?: number;
  muted?: boolean;
}

export interface ISoundtouchPreset extends ISoundtouchContentItem {
  presetId: string;
  sourceXml?: string;
}

export interface ISoundtouchZoneStatus {
  masterId?: string;
  masterIp?: string;
  isMaster?: boolean;
  slaves: ISoundtouchZoneMember[];
}

export interface ISoundtouchZoneMember {
  deviceId?: string;
  ipAddress?: string;
  role?: string;
}

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

export interface ISoundtouchManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  deviceId?: string;
  macAddress?: string;
  metadata?: Record<string, unknown>;
}
