export type TBleboxProtocol = 'http';

export type TBleboxEntityPlatform = 'switch' | 'light' | 'cover' | 'sensor' | 'binary_sensor';

export type TBleboxJsonValue = string | number | boolean | null | TBleboxJsonValue[] | {
  [key: string]: TBleboxJsonValue | undefined;
};

export interface IBleboxConfig {
  host?: string;
  port?: number;
  protocol?: TBleboxProtocol;
  username?: string;
  password?: string;
  timeoutMs?: number;
  snapshot?: IBleboxSnapshot;
}

export interface IBleboxDeviceInfo {
  id?: string;
  type?: string;
  product?: string;
  deviceName?: string;
  fv?: string;
  hv?: string;
  apiLevel?: string | number;
  ip?: string;
  availableFv?: string | null;
  [key: string]: TBleboxJsonValue | undefined;
}

export interface IBleboxProductProfile {
  productType: string;
  model: string;
  apiLevel: number;
  apiPath: string;
  extendedStatePath?: string;
  supports: Partial<Record<TBleboxEntityPlatform, boolean>>;
}

export interface IBleboxSnapshot {
  device: IBleboxDeviceInfo;
  state?: TBleboxJsonValue;
  extendedState?: TBleboxJsonValue;
  rawInfo?: TBleboxJsonValue;
  host?: string;
  port?: number;
  profile?: IBleboxProductProfile;
}

export interface IBleboxSwitchFeature {
  platform: 'switch';
  id: string;
  alias: string;
  uniqueId: string;
  name: string;
  unitId: number;
  isOn: boolean | null;
  deviceClass: 'switch';
}

export interface IBleboxLightFeature {
  platform: 'light';
  id: string;
  alias: string;
  uniqueId: string;
  name: string;
  isOn: boolean | null;
  brightness?: number | null;
  desiredColor?: string | null;
  colorMode?: number | null;
  colorModeName?: string;
  effect?: string | number | null;
  effectList?: string[];
  supportsColor?: boolean;
  supportsWhite?: boolean;
}

export type TBleboxCoverStateName = 'opening' | 'closing' | 'open' | 'closed' | 'unknown';

export interface IBleboxCoverFeature {
  platform: 'cover';
  id: string;
  alias: string;
  uniqueId: string;
  name: string;
  deviceClass: 'gate' | 'door' | 'shutter';
  state: number | null;
  stateName: TBleboxCoverStateName;
  position: number | null;
  tiltPosition?: number | null;
  supportsOpen: boolean;
  supportsClose: boolean;
  supportsStop: boolean;
  supportsPosition: boolean;
  supportsTilt: boolean;
  isPositionInverted: boolean;
}

export interface IBleboxSensorFeature {
  platform: 'sensor';
  id: string;
  alias: string;
  uniqueId: string;
  name: string;
  sensorType: string;
  sensorId?: string | number;
  nativeValue: number | string | null;
  unit?: string;
  deviceClass?: string;
}

export interface IBleboxBinarySensorFeature {
  platform: 'binary_sensor';
  id: string;
  alias: string;
  uniqueId: string;
  name: string;
  sensorType: 'rain' | 'flood' | string;
  sensorId?: string | number;
  isOn: boolean;
  deviceClass: 'moisture';
}

export type TBleboxFeature =
  | IBleboxSwitchFeature
  | IBleboxLightFeature
  | IBleboxCoverFeature
  | IBleboxSensorFeature
  | IBleboxBinarySensorFeature;

export interface IBleboxLightStatePatch {
  on: boolean;
  brightness?: number;
  rgbColor?: [number, number, number];
  rgbwColor?: [number, number, number, number];
  rgbwwColor?: [number, number, number, number, number];
  colorTempKelvin?: number;
  effect?: string | number;
}

export type TBleboxCoverCommand =
  | 'open_cover'
  | 'close_cover'
  | 'stop_cover'
  | 'set_cover_position'
  | 'open_cover_tilt'
  | 'close_cover_tilt'
  | 'set_cover_tilt_position';

export interface IBleboxCoverCommandRequest {
  command: TBleboxCoverCommand;
  position?: number;
  tiltPosition?: number;
}

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

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

export interface IHomeAssistantBleboxConfig extends IBleboxConfig {}
