export type TAndroidtvDeviceClass = 'auto' | 'androidtv' | 'firetv';

export type TAndroidtvMediaState = 'off' | 'idle' | 'standby' | 'playing' | 'paused' | 'stopped' | 'unknown';

export type TAndroidtvPowerState = 'on' | 'off' | 'unknown';

export type TAndroidtvKeyCommand =
  | 'BACK'
  | 'BLUE'
  | 'CENTER'
  | 'COMPONENT1'
  | 'COMPONENT2'
  | 'COMPOSITE1'
  | 'COMPOSITE2'
  | 'DOWN'
  | 'END'
  | 'ENTER'
  | 'ESCAPE'
  | 'FAST_FORWARD'
  | 'GREEN'
  | 'HDMI1'
  | 'HDMI2'
  | 'HDMI3'
  | 'HDMI4'
  | 'HOME'
  | 'INPUT'
  | 'LEFT'
  | 'MENU'
  | 'MOVE_HOME'
  | 'MUTE'
  | 'PAIRING'
  | 'POWER'
  | 'RED'
  | 'RESUME'
  | 'REWIND'
  | 'RIGHT'
  | 'SAT'
  | 'SEARCH'
  | 'SETTINGS'
  | 'SLEEP'
  | 'SUSPEND'
  | 'SYSDOWN'
  | 'SYSLEFT'
  | 'SYSRIGHT'
  | 'SYSUP'
  | 'TEXT'
  | 'TOP'
  | 'UP'
  | 'VGA'
  | 'VOLUME_DOWN'
  | 'VOLUME_UP'
  | 'WAKEUP'
  | 'YELLOW';

export type TAndroidtvCommandAction =
  | 'turn_on'
  | 'turn_off'
  | 'media_play'
  | 'media_pause'
  | 'media_play_pause'
  | 'media_stop'
  | 'volume_set'
  | 'volume_step'
  | 'volume_mute'
  | 'select_source'
  | 'remote_send_command'
  | 'adb_command';

export interface IAndroidtvConfig {
  host?: string;
  port?: number;
  deviceClass?: TAndroidtvDeviceClass;
  deviceName?: string;
  model?: string;
  manufacturer?: string;
  adbKeyPath?: string;
  adbServerIp?: string;
  adbServerPort?: number;
  deviceInfo?: IAndroidtvDeviceInfo;
  state?: IAndroidtvDeviceState;
  apps?: IAndroidtvApp[];
  snapshot?: IAndroidtvSnapshot;
}

export interface IAndroidtvDeviceInfo {
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  deviceClass?: TAndroidtvDeviceClass;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  softwareVersion?: string;
  productId?: string;
  wifiMac?: string;
  ethernetMac?: string;
}

export interface IAndroidtvDeviceState {
  rawState?: TAndroidtvMediaState | string;
  powerState?: TAndroidtvPowerState;
  available?: boolean;
  currentAppId?: string;
  currentAppName?: string;
  runningAppIds?: string[];
  source?: string;
  volumeLevel?: number;
  isVolumeMuted?: boolean;
  hdmiInput?: string;
  adbResponse?: string;
  mediaTitle?: string;
  mediaArtist?: string;
  mediaAlbumName?: string;
}

export interface IAndroidtvApp {
  id: string;
  name?: string;
  version?: string;
  isRunning?: boolean;
  isCurrent?: boolean;
}

export interface IAndroidtvCommand {
  action: TAndroidtvCommandAction;
  key?: TAndroidtvKeyCommand | string;
  keys?: Array<TAndroidtvKeyCommand | string>;
  shell?: string;
  source?: string;
  appId?: string;
  volumeLevel?: number;
  volumeStep?: number;
  muted?: boolean;
  repeats?: number;
}

export interface IAndroidtvEvent {
  type: 'snapshot' | 'command' | 'error';
  command?: IAndroidtvCommand;
  snapshot?: IAndroidtvSnapshot;
  message?: string;
  timestamp: number;
}

export interface IAndroidtvSnapshot {
  deviceInfo: IAndroidtvDeviceInfo;
  state: IAndroidtvDeviceState;
  apps: IAndroidtvApp[];
  updatedAt?: string;
}

export interface IAndroidtvManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  deviceName?: string;
  model?: string;
  manufacturer?: string;
  deviceClass?: TAndroidtvDeviceClass;
  metadata?: Record<string, unknown>;
}

export interface IAndroidtvAdbHostRecord {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  model?: string;
  manufacturer?: string;
  serialNumber?: string;
  macAddress?: string;
  deviceClass?: TAndroidtvDeviceClass;
  protocol?: string;
  service?: string;
  metadata?: Record<string, unknown>;
}

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

export type TAndroidtvDiscoveryRecord = IAndroidtvManualEntry | IAndroidtvAdbHostRecord | IAndroidtvMdnsRecord;

export type IHomeAssistantAndroidtvConfig = IAndroidtvConfig;
