export const frontierSiliconDefaultPin = '1234';
export const frontierSiliconDefaultPort = 80;
export const frontierSiliconDefaultTimeoutMs = 15000;
export const frontierSiliconSsdpSt = 'urn:schemas-frontier-silicon-com:undok:fsapi:1';

export type TFrontierSiliconProtocol = 'http' | 'https';
export type TFrontierSiliconSnapshotSource = 'snapshot' | 'http' | 'executor' | 'runtime' | 'manual';
export type TFrontierSiliconPlayState = 'off' | 'idle' | 'buffering' | 'playing' | 'paused' | 'unknown' | (string & {});
export type TFrontierSiliconRepeatMode = 'off' | 'all' | 'one';
export type TFrontierSiliconFsapiOperation = 'GET' | 'SET' | 'LIST_GET_NEXT' | 'CREATE_SESSION';
export type TFrontierSiliconCommand =
  | 'play'
  | 'pause'
  | 'stop'
  | 'previous_track'
  | 'next_track'
  | 'set_power'
  | 'set_volume'
  | 'volume_up'
  | 'volume_down'
  | 'mute'
  | 'select_source'
  | 'set_sound_mode'
  | 'set_repeat'
  | 'set_shuffle'
  | 'seek'
  | 'play_preset'
  | 'play_media'
  | 'raw_command';

export interface IFrontierSiliconConfig {
  host?: string;
  port?: number;
  protocol?: TFrontierSiliconProtocol;
  pin?: string;
  deviceUrl?: string;
  webfsapiUrl?: string;
  name?: string;
  uniqueId?: string;
  manufacturer?: string;
  model?: string;
  timeoutMs?: number;
  snapshot?: IFrontierSiliconSnapshot;
  commandExecutor?: IFrontierSiliconCommandExecutor;
}

export interface IHomeAssistantFrontierSiliconConfig extends IFrontierSiliconConfig {}

export interface IFrontierSiliconCommandExecutor {
  execute(requestArg: IFrontierSiliconRawCommandRequest): Promise<IFrontierSiliconCommandExecutorResult | unknown>;
}

export interface IFrontierSiliconRawCommandRequest {
  operation: TFrontierSiliconFsapiOperation;
  path: string;
  node?: string;
  value?: string | number | boolean;
  params: Record<string, string | number | boolean>;
  url?: string;
  method: 'GET';
  host?: string;
  port?: number;
  protocol?: TFrontierSiliconProtocol;
  webfsapiUrl?: string;
  pin: string;
  sessionId?: string;
}

export interface IFrontierSiliconCommandExecutorResult {
  ok?: boolean;
  success?: boolean;
  status?: number;
  response?: string;
  text?: string;
  data?: unknown;
}

export interface IFrontierSiliconCommandRequest {
  command: TFrontierSiliconCommand;
  powered?: boolean;
  volumeLevel?: number;
  volume?: number;
  muted?: boolean;
  source?: string;
  soundMode?: string;
  repeat?: TFrontierSiliconRepeatMode;
  shuffle?: boolean;
  position?: number;
  preset?: number;
  presetIndex?: number;
  mediaId?: string;
  mediaType?: string;
  path?: string;
  node?: string;
  value?: string | number | boolean;
  params?: Record<string, string | number | boolean>;
}

export interface IFrontierSiliconDeviceInfo {
  id: string;
  name: string;
  host?: string;
  port?: number;
  protocol?: TFrontierSiliconProtocol;
  deviceUrl?: string;
  webfsapiUrl?: string;
  manufacturer?: string;
  model?: string;
  version?: string;
  radioId?: string;
  macAddress?: string;
  rssi?: number;
}

export interface IFrontierSiliconMode {
  key: string;
  id: string;
  label: string;
  selectable?: number;
  streamable?: number;
  modetype?: number;
}

export interface IFrontierSiliconEqualiser {
  key: string;
  label: string;
}

export interface IFrontierSiliconPreset {
  key: number;
  type?: string;
  name?: string;
}

export interface IFrontierSiliconPlayerState {
  power?: boolean;
  playState: TFrontierSiliconPlayState;
  playStatusCode?: number;
  volume?: number;
  maxVolume?: number;
  muted?: boolean;
  mode?: IFrontierSiliconMode;
  source?: string;
  repeat?: TFrontierSiliconRepeatMode;
  shuffle?: boolean;
  soundMode?: IFrontierSiliconEqualiser;
  mediaName?: string;
  mediaText?: string;
  mediaTitle?: string;
  mediaArtist?: string;
  mediaAlbum?: string;
  mediaImageUrl?: string;
  mediaDuration?: number;
  mediaPosition?: number;
}

export interface IFrontierSiliconSnapshot {
  device: IFrontierSiliconDeviceInfo;
  player: IFrontierSiliconPlayerState;
  modes: IFrontierSiliconMode[];
  equalisers: IFrontierSiliconEqualiser[];
  presets: IFrontierSiliconPreset[];
  online: boolean;
  updatedAt?: string;
  source?: TFrontierSiliconSnapshotSource;
  error?: string;
  raw?: Record<string, unknown>;
}

export interface IFrontierSiliconSsdpRecord {
  st?: string;
  usn?: string;
  location?: string;
  ssdpLocation?: string;
  headers?: Record<string, string | undefined>;
  ssdpHeaders?: Record<string, string | undefined>;
  upnp?: Record<string, string | undefined>;
  [key: string]: unknown;
}

export interface IFrontierSiliconManualEntry {
  id?: string;
  host?: string;
  port?: number;
  protocol?: TFrontierSiliconProtocol;
  pin?: string;
  deviceUrl?: string;
  webfsapiUrl?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  radioId?: string;
  metadata?: Record<string, unknown>;
}
