import type { IServiceCallRequest } from '../../core/types.js';

export const fullyKioskDomain = 'fully_kiosk';
export const fullyKioskDefaultPort = 2323;
export const fullyKioskDefaultTimeoutMs = 15000;
export const fullyKioskMusicStream = 3;

export type TFullyKioskProtocol = 'http' | 'https';
export type TFullyKioskSnapshotSource = 'http' | 'snapshot' | 'client' | 'manual' | 'runtime';
export type TFullyKioskImageKind = 'screenshot' | 'camshot';

export interface IFullyKioskRestObject {
  status?: string;
  statustext?: string;
  [key: string]: unknown;
}

export interface IFullyKioskDeviceInfo extends IFullyKioskRestObject {
  deviceID?: string;
  deviceId?: string;
  deviceName?: string;
  deviceManufacturer?: string;
  deviceModel?: string;
  appVersionName?: string;
  Mac?: string;
  mac?: string;
  ip4?: string;
  ip6?: string;
  hostname4?: string;
  hostname6?: string;
  settings?: IFullyKioskSettings;
}

export interface IFullyKioskSettings extends IFullyKioskRestObject {}

export interface IFullyKioskSnapshot {
  deviceInfo: IFullyKioskDeviceInfo;
  settings: IFullyKioskSettings;
  online: boolean;
  updatedAt: string;
  source: TFullyKioskSnapshotSource;
  host?: string;
  port?: number;
  protocol: TFullyKioskProtocol;
  ssl: boolean;
  verifySsl?: boolean;
  name?: string;
  uniqueId?: string;
  macAddress?: string;
  error?: string;
  metadata?: Record<string, unknown>;
}

export interface IFullyKioskSnapshotImage {
  contentType: string;
  data: Uint8Array;
}

export interface IFullyKioskClientLike {
  getSnapshot?(): Promise<IFullyKioskSnapshot>;
  getDeviceInfo?(): Promise<IFullyKioskDeviceInfo>;
  getSettings?(): Promise<IFullyKioskSettings>;
  sendCommand?(cmdArg: string, paramsArg?: Record<string, unknown>): Promise<unknown>;
  getScreenshot?(): Promise<Uint8Array | ArrayBuffer | Buffer>;
  getCamshot?(): Promise<Uint8Array | ArrayBuffer | Buffer>;
  execute?(commandArg: IFullyKioskClientCommand): Promise<unknown>;
  destroy?(): Promise<void>;
}

export interface IFullyKioskCommandExecutor {
  execute(commandArg: IFullyKioskClientCommand): Promise<unknown>;
}

export interface IFullyKioskConfig {
  host?: string;
  port?: number;
  protocol?: TFullyKioskProtocol;
  ssl?: boolean;
  verifySsl?: boolean;
  password?: string;
  name?: string;
  uniqueId?: string;
  macAddress?: string;
  manufacturer?: string;
  model?: string;
  timeoutMs?: number;
  online?: boolean;
  snapshot?: IFullyKioskSnapshot;
  deviceInfo?: IFullyKioskDeviceInfo;
  settings?: IFullyKioskSettings;
  client?: IFullyKioskClientLike;
  commandExecutor?: IFullyKioskCommandExecutor | ((commandArg: IFullyKioskClientCommand) => Promise<unknown>);
  metadata?: Record<string, unknown>;
}

export interface IHomeAssistantFullyKioskConfig extends IFullyKioskConfig {}

export interface IFullyKioskManualEntry extends IFullyKioskConfig {
  id?: string;
  url?: string;
  source?: string;
  integrationDomain?: string;
  manufacturer?: string;
}

export interface IFullyKioskDhcpEntry {
  ip?: string;
  ipAddress?: string;
  host?: string;
  hostname?: string;
  macaddress?: string;
  macAddress?: string;
  registeredDevices?: boolean;
  registered_devices?: boolean;
  integrationDomain?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  metadata?: Record<string, unknown>;
}

export type TFullyKioskClientCommandType =
  | 'refresh'
  | 'send_command'
  | 'set_string_setting'
  | 'set_boolean_setting'
  | 'snapshot_image'
  | 'stream_source';

export interface IFullyKioskClientCommand {
  type: TFullyKioskClientCommandType;
  service?: string;
  cmd?: string;
  params?: Record<string, unknown>;
  settingKey?: string;
  value?: unknown;
  imageKind?: TFullyKioskImageKind;
  target?: IServiceCallRequest['target'];
  data?: Record<string, unknown>;
  entityId?: string;
  deviceId?: string;
  uniqueId?: string;
}
