export type TAxisProtocol = 'http' | 'https';
export type TAxisAuthScheme = 'auto' | 'basic' | 'digest';
export type TAxisPortState = 'open' | 'closed' | 'unknown';
export type TAxisPortDirection = 'input' | 'output' | 'unknown';
export type TAxisPtzMove = 'home' | 'up' | 'down' | 'left' | 'right' | 'upleft' | 'upright' | 'downleft' | 'downright' | 'stop';
export type TAxisPtzState = 'on' | 'off' | 'auto';

export interface IAxisConfig {
  protocol?: TAxisProtocol;
  host?: string;
  port?: number;
  username?: string;
  password?: string;
  authScheme?: TAxisAuthScheme;
  timeoutMs?: number;
  name?: string;
  uniqueId?: string;
  model?: string;
  streamProfile?: string;
  videoSource?: string | number;
  deviceInfo?: IAxisDeviceInfo;
  cameras?: IAxisCameraStream[];
  sensors?: IAxisSensor[];
  binarySensors?: IAxisBinarySensor[];
  events?: IAxisEvent[];
  ports?: IAxisPort[];
  relays?: IAxisRelay[];
  switches?: IAxisRelay[];
  lights?: IAxisLight[];
  apiDiscovery?: IAxisApiDescription[];
  params?: IAxisParamTree;
  connected?: boolean;
  snapshot?: IAxisSnapshot;
}

export interface IHomeAssistantAxisConfig extends IAxisConfig {}

export interface IAxisDeviceInfo {
  id?: string;
  serialNumber?: string;
  macAddress?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  productNumber?: string;
  productType?: string;
  firmwareVersion?: string;
  hardwareId?: string;
  host?: string;
  port?: number;
  protocol?: TAxisProtocol;
  online?: boolean;
}

export interface IAxisCameraStream {
  id: string;
  name?: string;
  enabled?: boolean;
  videoSource?: string | number;
  streamProfile?: string;
  resolution?: string;
  imageFormats?: string[];
  snapshotUrl?: string;
  mjpegUrl?: string;
  rtspUrl?: string;
  supportsPtz?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAxisSensor<TValue = unknown> {
  id: string;
  name: string;
  value: TValue;
  unit?: string;
  deviceClass?: string;
  category?: string;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAxisBinarySensor {
  id: string;
  name: string;
  isOn: boolean;
  deviceClass?: string;
  eventTopic?: string;
  source?: string;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAxisEvent {
  id: string;
  name?: string;
  topic?: string;
  topicBase?: string;
  group?: 'input' | 'light' | 'motion' | 'output' | 'ptz' | 'sound' | 'none' | string;
  source?: string;
  sourceIndex?: string;
  operation?: 'Initialized' | 'Changed' | 'Deleted' | 'Unknown' | string;
  state?: string;
  type?: string;
  isTripped?: boolean;
  deviceClass?: string;
  updatedAt?: string;
  data?: Record<string, unknown>;
}

export interface IAxisPort {
  id: string;
  name?: string;
  usage?: string;
  direction: TAxisPortDirection;
  state?: TAxisPortState;
  normalState?: TAxisPortState;
  configurable?: boolean;
  readonly?: boolean;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAxisRelay extends IAxisPort {
  direction: 'output';
}

export interface IAxisLight {
  id: string;
  name?: string;
  enabled?: boolean;
  isOn?: boolean;
  lightType?: string;
  numberOfLeds?: number;
  brightness?: number;
  maxIntensity?: number;
  available?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IAxisApiDescription {
  id: string;
  name?: string;
  version?: string;
  status?: string;
  docLink?: string;
}

export interface IAxisParamTree {
  root?: Record<string, unknown>;
  [key: string]: unknown;
}

export interface IAxisSnapshot {
  deviceInfo: IAxisDeviceInfo;
  cameras: IAxisCameraStream[];
  sensors: IAxisSensor[];
  binarySensors: IAxisBinarySensor[];
  events: IAxisEvent[];
  ports: IAxisPort[];
  relays: IAxisRelay[];
  switches: IAxisRelay[];
  lights: IAxisLight[];
  apiDiscovery: IAxisApiDescription[];
  params?: IAxisParamTree;
  connected: boolean;
  updatedAt?: string;
}

export interface IAxisBasicDeviceInfoResponse {
  apiVersion?: string;
  context?: string;
  method?: string;
  data?: {
    propertyList?: Record<string, string | undefined>;
  };
  error?: IAxisApiError;
}

export interface IAxisApiDiscoveryResponse {
  apiVersion?: string;
  context?: string;
  method?: string;
  data?: {
    apiList?: IAxisApiDescription[];
  };
  error?: IAxisApiError;
}

export interface IAxisPortsResponse {
  apiVersion?: string;
  context?: string;
  method?: string;
  data?: {
    numberOfPorts?: number;
    items?: IAxisPortManagementItem[];
  };
  error?: IAxisApiError;
}

export interface IAxisPortManagementItem {
  port: string;
  state?: TAxisPortState | string;
  configurable?: boolean;
  readonly?: boolean;
  usage?: string;
  direction?: TAxisPortDirection | string;
  name?: string;
  normalState?: TAxisPortState | string;
}

export interface IAxisLightInformationResponse {
  apiVersion?: string;
  context?: string;
  method?: string;
  data?: {
    items?: IAxisLightInformationItem[];
  };
  error?: IAxisApiError;
}

export interface IAxisLightInformationItem {
  enabled?: boolean;
  lightID?: string;
  lightState?: boolean;
  lightType?: string;
  nrOfLEDs?: number;
  automaticAngleOfIlluminationMode?: boolean;
  automaticIntensityMode?: boolean;
  synchronizeDayNightMode?: boolean;
  error?: boolean;
  errorInfo?: string;
}

export interface IAxisApiError {
  code?: number;
  message?: string;
  details?: Record<string, unknown>;
  errors?: IAxisApiError[];
}

export interface IAxisPtzCommand {
  camera?: string | number;
  move?: TAxisPtzMove;
  pan?: number;
  tilt?: number;
  zoom?: number;
  focus?: number;
  iris?: number;
  brightness?: number;
  rpan?: number;
  rtilt?: number;
  rzoom?: number;
  rfocus?: number;
  riris?: number;
  rbrightness?: number;
  autofocus?: boolean;
  autoiris?: boolean;
  continuouspantiltmove?: [number, number];
  continuouszoommove?: number;
  continuousfocusmove?: number;
  continuousirismove?: number;
  continuousbrightnessmove?: number;
  auxiliary?: string;
  gotoserverpresetname?: string;
  gotoserverpresetno?: number;
  gotodevicepreset?: number;
  speed?: number;
  imagerotation?: '0' | '90' | '180' | '270';
  ircutfilter?: TAxisPtzState;
  backlight?: boolean;
  center?: [number, number];
  areazoom?: [number, number, number];
  imagewidth?: number;
  imageheight?: number;
}

export interface IAxisRelayCommand {
  portId: string;
  state: 'open' | 'closed';
}

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

export interface IAxisMdnsRecord {
  type?: string;
  name?: string;
  host?: string;
  port?: number;
  addresses?: string[];
  hostname?: string;
  txt?: Record<string, string | undefined>;
  properties?: Record<string, string | undefined>;
}

export interface IAxisSsdpRecord {
  manufacturer?: string;
  server?: string;
  st?: string;
  usn?: string;
  location?: string;
  upnp?: Record<string, string | undefined>;
  headers?: Record<string, string | undefined>;
}

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