import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BleboxMapper } from '../../ts/integrations/blebox/index.js';
import type { IBleboxSnapshot } from '../../ts/integrations/blebox/index.js';

tap.test('maps BleBox switch, power sensor, light, cover, and moisture snapshots', async () => {
  const switchSnapshot: IBleboxSnapshot = {
    device: {
      id: '1afe34e750b8',
      type: 'switchBoxD',
      deviceName: 'Kitchen Switch',
      fv: '0.200',
      hv: '0.7',
      apiLevel: 20200831,
    },
    state: {
      relays: [
        { relay: 0, state: 1, name: 'Counter' },
        { relay: 1, state: 0, name: 'Sink' },
      ],
      sensors: [{ type: 'activePower', id: 0, value: 12.5 }],
    },
  };
  const switchEntities = BleboxMapper.toEntities(switchSnapshot);
  expect(switchEntities.find((entityArg) => entityArg.id === 'switch.kitchen_switch_relay_0')?.state).toEqual('on');
  expect(switchEntities.find((entityArg) => entityArg.id === 'sensor.kitchen_switch_activepower_0')?.state).toEqual(12.5);

  const lightSnapshot: IBleboxSnapshot = {
    device: {
      id: '2bee34e750b8',
      type: 'wLightBox',
      deviceName: 'Cabinet Light',
      fv: '0.993',
      hv: '4.3',
      apiLevel: 20200229,
    },
    extendedState: {
      rgbw: {
        desiredColor: 'fa00203a',
        colorMode: 4,
        effectID: 0,
        effectsNames: { 0: 'NONE', 1: 'FADE' },
      },
    },
  };
  const lightEntity = BleboxMapper.toEntities(lightSnapshot)[0];
  expect(lightEntity.id).toEqual('light.cabinet_light_color');
  expect(lightEntity.attributes?.brightness).toEqual(250);
  expect(lightEntity.attributes?.colorMode).toEqual('rgbw');

  const coverSnapshot: IBleboxSnapshot = {
    device: {
      id: '3cee34e750b8',
      type: 'shutterBox',
      deviceName: 'Bedroom Shutter',
      fv: '0.147',
      hv: '0.7',
      apiLevel: 20180604,
    },
    state: {
      shutter: {
        state: 1,
        desiredPos: { position: 25, tilt: 80 },
      },
    },
  };
  const coverEntity = BleboxMapper.toEntities(coverSnapshot)[0];
  expect(coverEntity.state).toEqual('opening');
  expect(coverEntity.attributes?.currentPosition).toEqual(75);
  expect(coverEntity.attributes?.currentTiltPosition).toEqual(20);

  const sensorSnapshot: IBleboxSnapshot = {
    device: {
      id: '4dee34e750b8',
      type: 'multiSensor',
      deviceName: 'Garden Sensor',
      fv: '1.0',
      hv: '1.0',
      apiLevel: 20230606,
    },
    extendedState: {
      multiSensor: {
        sensors: [
          { id: 0, type: 'temperature', value: 2234 },
          { id: 1, type: 'flood', value: 1 },
        ],
      },
    },
  };
  const sensorEntities = BleboxMapper.toEntities(sensorSnapshot);
  expect(sensorEntities.find((entityArg) => entityArg.id === 'sensor.garden_sensor_temperature_0')?.state).toEqual(22.34);
  expect(sensorEntities.find((entityArg) => entityArg.id === 'binary_sensor.garden_sensor_flood_1')?.state).toEqual('on');
});

export default tap.start();
