import { expect, tap } from '@git.zone/tstest/tapbundle';
import { MadvrMapper, type IMadvrSnapshot } from '../../ts/integrations/madvr/index.js';

const snapshot: IMadvrSnapshot = {
  deviceInfo: {
    host: '192.168.1.90',
    port: 44077,
    name: 'Cinema Envy',
    manufacturer: 'madVR',
    model: 'Envy Extreme',
    macAddress: 'AA:BB:CC:DD:EE:FF',
  },
  processor: {
    isOn: true,
    isSignal: true,
    incoming: {
      resolution: '3840x2160',
      frameRate: '23.976',
      signalType: '2D',
      colorSpace: '444',
      bitDepth: '10bit',
      hdr: true,
      colorimetry: 'HDR10',
      blackLevels: 'TV',
      aspectRatio: '16:9',
    },
    outgoing: {
      resolution: '4096x2160',
      frameRate: '23.976',
      signalType: '2D',
      colorSpace: 'RGB',
      bitDepth: '12bit',
      hdr: true,
      colorimetry: '2020',
      blackLevels: 'PC',
    },
    temperatures: {
      gpu: 55,
      hdmi: 0,
      cpu: 44,
      mainboard: 41,
    },
    profileName: 'Cinema',
    profileNumber: '1',
  },
  projector: {
    name: 'Projector',
    activeProfile: 'Cinema',
  },
  display: {
    hdrFlag: true,
    outgoingHdrFlag: true,
    aspect: {
      resolution: '3840x2160',
      decimal: 1.78,
      integer: '178',
      name: '16:9',
    },
    masking: {
      resolution: '3840x1600',
      decimal: 2.4,
      integer: '240',
    },
  },
  buttons: [
    { key: 'hotplug', name: 'Hotplug', command: 'Hotplug' },
  ],
  selects: [
    {
      key: 'display_profile',
      name: 'Display profile',
      current: 'Cinema',
      options: ['Cinema', 'TV'],
      commands: {
        Cinema: { command: 'ActivateProfile', args: ['DISPLAY', '1'] },
        TV: { command: 'ActivateProfile', args: ['DISPLAY', '2'] },
      },
    },
  ],
  available: true,
};

tap.test('maps madVR Envy snapshots to processor/display devices and entities', async () => {
  const devices = MadvrMapper.toDevices(snapshot);
  const entities = MadvrMapper.toEntities(snapshot);

  expect(devices[0].id).toEqual('madvr.device.aa_bb_cc_dd_ee_ff');
  expect(devices[0].online).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'incoming_res' && stateArg.value === '3840x2160')).toBeTrue();
  expect(devices[0].features.some((featureArg) => featureArg.id === 'button_hotplug' && featureArg.writable)).toBeTrue();

  const mediaEntity = entities.find((entityArg) => entityArg.platform === 'media_player');
  expect(mediaEntity?.id).toEqual('media_player.cinema_envy');
  expect(mediaEntity?.state).toEqual('on');
  expect(mediaEntity?.attributes?.hdrFlag).toBeTrue();
  expect((mediaEntity?.attributes?.incoming as Record<string, unknown>).resolution).toEqual('3840x2160');

  expect(entities.find((entityArg) => entityArg.uniqueId.endsWith('_incoming_colorimetry'))?.state).toEqual('HDR10');
  expect(entities.find((entityArg) => entityArg.uniqueId.endsWith('_temp_hdmi'))?.state).toEqual(null);
  expect(entities.find((entityArg) => entityArg.uniqueId.endsWith('_power_state'))?.state).toBeTrue();
  expect(entities.find((entityArg) => entityArg.platform === 'button')?.attributes?.writable).toBeTrue();
  expect(entities.find((entityArg) => entityArg.platform === 'select')?.attributes?.options).toEqual(['Cinema', 'TV']);
});

export default tap.start();
