export type TAndroidtvRemoteMediaState =
  | 'off'
  | 'on'
  | 'idle'
  | 'playing'
  | 'paused'
  | 'stopped'
  | 'buffering'
  | 'unknown';

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

export type TAndroidtvRemoteCommandDirection = 'SHORT' | 'START_LONG' | 'END_LONG';

export type TAndroidtvRemoteKeyCode =
  | '0'
  | '1'
  | '2'
  | '3'
  | '4'
  | '5'
  | '6'
  | '7'
  | '8'
  | '9'
  | 'ASSIST'
  | 'BACK'
  | 'BUTTON_A'
  | 'BUTTON_B'
  | 'BUTTON_MODE'
  | 'BUTTON_X'
  | 'BUTTON_Y'
  | 'CAPTIONS'
  | 'CHANNEL_DOWN'
  | 'CHANNEL_UP'
  | 'DEL'
  | 'DPAD_CENTER'
  | 'DPAD_DOWN'
  | 'DPAD_LEFT'
  | 'DPAD_RIGHT'
  | 'DPAD_UP'
  | 'DVR'
  | 'ENTER'
  | 'EXPLORER'
  | 'F1'
  | 'F2'
  | 'F3'
  | 'F4'
  | 'F5'
  | 'F6'
  | 'F7'
  | 'F8'
  | 'F9'
  | 'F10'
  | 'F11'
  | 'F12'
  | 'GUIDE'
  | 'HOME'
  | 'INFO'
  | 'MEDIA_AUDIO_TRACK'
  | 'MEDIA_FAST_FORWARD'
  | 'MEDIA_NEXT'
  | 'MEDIA_PAUSE'
  | 'MEDIA_PLAY'
  | 'MEDIA_PLAY_PAUSE'
  | 'MEDIA_PREVIOUS'
  | 'MEDIA_RECORD'
  | 'MEDIA_REWIND'
  | 'MEDIA_STOP'
  | 'MENU'
  | 'MUTE'
  | 'POWER'
  | 'PROG_BLUE'
  | 'PROG_GREEN'
  | 'PROG_RED'
  | 'PROG_YELLOW'
  | 'SEARCH'
  | 'SETTINGS'
  | 'TV'
  | 'TV_TELETEXT'
  | 'VOLUME_DOWN'
  | 'VOLUME_MUTE'
  | 'VOLUME_UP';

export type TAndroidtvRemoteCommandReason =
  | 'connect'
  | 'finish_pairing'
  | 'launch_app'
  | 'media_next_track'
  | 'media_pause'
  | 'media_play'
  | 'media_play_pause'
  | 'media_previous_track'
  | 'media_stop'
  | 'play_channel'
  | 'remote_send_command'
  | 'select_activity'
  | 'send_text'
  | 'start_pairing'
  | 'turn_off'
  | 'turn_on'
  | 'volume_down'
  | 'volume_mute'
  | 'volume_set'
  | 'volume_up';

export type TAndroidtvRemoteCommandAction =
  | 'connect'
  | 'finish_pairing'
  | 'key_command'
  | 'launch_app'
  | 'remote_send_command'
  | 'send_text'
  | 'start_pairing'
  | 'volume_set';

export interface IAndroidtvRemoteVolumeInfo {
  level?: number;
  max?: number;
  muted?: boolean;
  playerModel?: string;
}

export interface IAndroidtvRemoteDeviceInfo {
  id?: string;
  name?: string;
  host?: string;
  apiPort?: number;
  pairPort?: number;
  macAddress?: string;
  manufacturer?: string;
  model?: string;
  softwareVersion?: string;
  appVersion?: string;
}

export interface IAndroidtvRemoteDeviceState {
  available?: boolean;
  isOn?: boolean | null;
  powerState?: TAndroidtvRemotePowerState;
  mediaState?: TAndroidtvRemoteMediaState | string;
  rawState?: string;
  currentApp?: string;
  currentAppName?: string;
  currentActivity?: string;
  volumeInfo?: IAndroidtvRemoteVolumeInfo;
  volumeLevel?: number;
  isVolumeMuted?: boolean;
  voiceEnabled?: boolean;
}

export interface IAndroidtvRemoteApp {
  id: string;
  name?: string;
  icon?: string;
  link?: string;
}

export interface IAndroidtvRemoteConfiguredApp {
  appName?: string;
  appIcon?: string;
  app_name?: string;
  app_icon?: string;
  name?: string;
  icon?: string;
  link?: string;
}

export interface IAndroidtvRemoteSnapshot {
  deviceInfo: IAndroidtvRemoteDeviceInfo;
  state: IAndroidtvRemoteDeviceState;
  apps: IAndroidtvRemoteApp[];
  updatedAt?: string;
}

export interface IAndroidtvRemoteKeyPress {
  keyCode: TAndroidtvRemoteKeyCode | string;
  direction?: TAndroidtvRemoteCommandDirection;
}

export interface IAndroidtvRemoteCommand {
  action: TAndroidtvRemoteCommandAction;
  reason?: TAndroidtvRemoteCommandReason;
  keyCode?: TAndroidtvRemoteKeyCode | string;
  direction?: TAndroidtvRemoteCommandDirection;
  keys?: IAndroidtvRemoteKeyPress[];
  appId?: string;
  appLink?: string;
  appName?: string;
  text?: string;
  pin?: string;
  volumeLevel?: number;
  muted?: boolean;
  repeats?: number;
  delaySecs?: number;
  holdSecs?: number;
}

export interface IAndroidtvRemoteCommandContext {
  config: IAndroidtvRemoteConfig;
  snapshot: IAndroidtvRemoteSnapshot;
}

export type TAndroidtvRemoteCommandExecutor =
  | ((commandArg: IAndroidtvRemoteCommand, contextArg: IAndroidtvRemoteCommandContext) => Promise<void> | void)
  | {
      execute(commandArg: IAndroidtvRemoteCommand, contextArg: IAndroidtvRemoteCommandContext): Promise<void> | void;
    };

export interface IAndroidtvRemoteConfig {
  host?: string;
  apiPort?: number;
  pairPort?: number;
  deviceName?: string;
  macAddress?: string;
  manufacturer?: string;
  model?: string;
  enableIme?: boolean;
  deviceInfo?: IAndroidtvRemoteDeviceInfo;
  state?: IAndroidtvRemoteDeviceState;
  volumeInfo?: IAndroidtvRemoteVolumeInfo;
  apps?: IAndroidtvRemoteApp[] | Record<string, IAndroidtvRemoteConfiguredApp>;
  snapshot?: IAndroidtvRemoteSnapshot;
  executor?: TAndroidtvRemoteCommandExecutor;
}

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

export interface IAndroidtvRemoteManualEntry {
  host?: string;
  apiPort?: number;
  pairPort?: number;
  port?: number;
  id?: string;
  name?: string;
  deviceName?: string;
  macAddress?: string;
  manufacturer?: string;
  model?: string;
  enableIme?: boolean;
  metadata?: Record<string, unknown>;
}

export type TAndroidtvRemoteDiscoveryRecord = IAndroidtvRemoteMdnsRecord | IAndroidtvRemoteManualEntry;

export type IHomeAssistantAndroidtvRemoteConfig = IAndroidtvRemoteConfig;
