export const androidIpWebcamDefaultPort = 8080;
export const androidIpWebcamDefaultTimeoutMs = 10000;

export type TAndroidIpWebcamProtocol = 'http' | 'https';
export type TAndroidIpWebcamRtspVideoCodec = 'jpeg' | 'h264';
export type TAndroidIpWebcamRtspAudioCodec = 'ulaw' | 'alaw' | 'pcm' | 'opus' | 'aac';
export type TAndroidIpWebcamOrientation = 'landscape' | 'upsidedown' | 'portrait' | 'upsidedown_portrait';
export type TAndroidIpWebcamSettingCommand = 'setting' | 'torch' | 'focus' | 'record';
export type TAndroidIpWebcamCommandType =
  | 'snapshot_image'
  | 'stream_source'
  | 'setting'
  | 'torch'
  | 'focus'
  | 'record'
  | 'set_zoom'
  | 'set_quality'
  | 'set_orientation'
  | 'set_scenemode'
  | 'refresh';

export interface IAndroidIpWebcamConfig {
  protocol?: TAndroidIpWebcamProtocol;
  host?: string;
  port?: number;
  url?: string;
  username?: string;
  password?: string;
  timeoutMs?: number;
  name?: string;
  uniqueId?: string;
  manufacturer?: string;
  model?: string;
  connected?: boolean;
  deviceInfo?: IAndroidIpWebcamDeviceInfo;
  camera?: IAndroidIpWebcamCamera;
  sensors?: IAndroidIpWebcamSensor[];
  binarySensors?: IAndroidIpWebcamBinarySensor[];
  switches?: IAndroidIpWebcamSwitch[];
  statusData?: IAndroidIpWebcamStatusData;
  sensorData?: IAndroidIpWebcamSensorData;
  currentSettings?: Record<string, unknown>;
  enabledSensors?: string[];
  enabledSettings?: string[];
  availableSettings?: Record<string, unknown[]>;
  snapshot?: IAndroidIpWebcamSnapshot;
}

export interface IHomeAssistantAndroidIpWebcamConfig extends IAndroidIpWebcamConfig {}

export interface IAndroidIpWebcamDeviceInfo {
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  host?: string;
  port?: number;
  protocol?: TAndroidIpWebcamProtocol;
  url?: string;
  online?: boolean;
}

export interface IAndroidIpWebcamCamera {
  id: string;
  name?: string;
  mjpegUrl?: string;
  imageUrl?: string;
  rtspUrl?: string;
  audioWavUrl?: string;
  audioAacUrl?: string;
  audioOpusUrl?: string;
  supportedFeatures?: string[];
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAndroidIpWebcamSensor<TValue = unknown> {
  key: string;
  name: string;
  value: TValue;
  unit?: string;
  deviceClass?: string;
  stateClass?: string;
  entityCategory?: string;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAndroidIpWebcamBinarySensor {
  key: string;
  name: string;
  isOn: boolean;
  deviceClass?: string;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAndroidIpWebcamSwitch {
  key: string;
  name: string;
  isOn: boolean;
  command: TAndroidIpWebcamSettingCommand;
  entityCategory?: string;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAndroidIpWebcamStatusData {
  curvals?: Record<string, unknown>;
  avail?: Record<string, unknown[]>;
  audio_connections?: unknown;
  video_connections?: unknown;
  [key: string]: unknown;
}

export interface IAndroidIpWebcamSensorDatum {
  unit?: string;
  data?: unknown;
  [key: string]: unknown;
}

export type IAndroidIpWebcamSensorData = Record<string, IAndroidIpWebcamSensorDatum>;

export interface IAndroidIpWebcamSnapshot {
  deviceInfo: IAndroidIpWebcamDeviceInfo;
  camera: IAndroidIpWebcamCamera;
  sensors: IAndroidIpWebcamSensor[];
  binarySensors: IAndroidIpWebcamBinarySensor[];
  switches: IAndroidIpWebcamSwitch[];
  statusData: IAndroidIpWebcamStatusData;
  sensorData: IAndroidIpWebcamSensorData;
  currentSettings: Record<string, unknown>;
  enabledSensors: string[];
  enabledSettings: string[];
  availableSettings: Record<string, unknown[]>;
  connected: boolean;
  updatedAt?: string;
  metadata?: Record<string, unknown>;
}

export interface IAndroidIpWebcamSensorDescription {
  key: string;
  name: string;
  deviceClass?: string;
  stateClass?: string;
  entityCategory?: string;
  statusKey?: 'audio_connections' | 'video_connections';
}

export interface IAndroidIpWebcamSwitchDescription {
  key: string;
  name: string;
  command: TAndroidIpWebcamSettingCommand;
  entityCategory?: string;
}

export interface IAndroidIpWebcamClientCommand {
  type: TAndroidIpWebcamCommandType;
  service: string;
  target?: {
    entityId?: string;
    deviceId?: string;
  };
  data?: Record<string, unknown>;
  key?: string;
  value?: unknown;
  activate?: boolean;
  record?: boolean;
  tag?: string;
  zoom?: number;
  quality?: number;
  orientation?: TAndroidIpWebcamOrientation;
  scenemode?: string;
  videoCodec?: TAndroidIpWebcamRtspVideoCodec;
  audioCodec?: TAndroidIpWebcamRtspAudioCodec;
  filename?: string;
}

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

export interface IAndroidIpWebcamManualEntry {
  host?: string;
  port?: number;
  url?: string;
  protocol?: TAndroidIpWebcamProtocol;
  username?: string;
  password?: string;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  metadata?: Record<string, unknown>;
}

export const androidIpWebcamSensorDescriptions: IAndroidIpWebcamSensorDescription[] = [
  { key: 'audio_connections', name: 'Audio connections', stateClass: 'total', entityCategory: 'diagnostic', statusKey: 'audio_connections' },
  { key: 'battery_level', name: 'Battery level', deviceClass: 'battery', stateClass: 'measurement', entityCategory: 'diagnostic' },
  { key: 'battery_temp', name: 'Battery temperature', stateClass: 'measurement', entityCategory: 'diagnostic' },
  { key: 'battery_voltage', name: 'Battery voltage', stateClass: 'measurement', entityCategory: 'diagnostic' },
  { key: 'light', name: 'Light level', stateClass: 'measurement' },
  { key: 'motion', name: 'Motion', stateClass: 'measurement' },
  { key: 'pressure', name: 'Pressure', stateClass: 'measurement' },
  { key: 'proximity', name: 'Proximity', stateClass: 'measurement' },
  { key: 'sound', name: 'Sound', stateClass: 'measurement' },
  { key: 'video_connections', name: 'Video connections', stateClass: 'total', entityCategory: 'diagnostic', statusKey: 'video_connections' },
];

export const androidIpWebcamSwitchDescriptions: IAndroidIpWebcamSwitchDescription[] = [
  { key: 'exposure_lock', name: 'Exposure lock', command: 'setting', entityCategory: 'config' },
  { key: 'ffc', name: 'Front-facing camera', command: 'setting', entityCategory: 'config' },
  { key: 'focus', name: 'Focus', command: 'focus', entityCategory: 'config' },
  { key: 'gps_active', name: 'GPS active', command: 'setting', entityCategory: 'config' },
  { key: 'motion_detect', name: 'Motion detection', command: 'setting', entityCategory: 'config' },
  { key: 'night_vision', name: 'Night vision', command: 'setting', entityCategory: 'config' },
  { key: 'overlay', name: 'Overlay', command: 'setting', entityCategory: 'config' },
  { key: 'torch', name: 'Torch', command: 'torch', entityCategory: 'config' },
  { key: 'whitebalance_lock', name: 'White balance lock', command: 'setting', entityCategory: 'config' },
  { key: 'video_recording', name: 'Video recording', command: 'record', entityCategory: 'config' },
];
