import { expect, tap } from '@git.zone/tstest/tapbundle';
import { YamahaMusiccastMapper, type IYamahaMusiccastSnapshot } from '../../ts/integrations/yamaha_musiccast/index.js';

const snapshot: IYamahaMusiccastSnapshot = {
  deviceInfo: {
    model_name: 'RX-V685',
    device_id: '4C1B86A6CBF5',
    system_id: '03E88CF3',
    serial_number: 'Y459229YO',
    system_version: 1.96,
    api_version: 2.11,
  },
  networkStatus: {
    network_name: 'Living Room RX',
    ip_address: '192.168.1.70',
    mac_address: {
      wired_lan: '4C1B86A6CBF5',
    },
  },
  inputNames: {
    hdmi1: 'Apple TV',
    server: 'Media Server',
    tuner: 'Tuner',
    audio1: 'AUDIO1',
  },
  netusb: {
    input: 'server',
    playback: 'pause',
    repeat: 'all',
    shuffle: 'on',
    artist: 'Artist One',
    album: 'Album One',
    track: 'Track One',
    total_time: 240,
    play_time: 12,
  },
  distribution: {
    group_id: '00000000000000000000000000000000',
    role: 'none',
  },
  zones: [{
    zone: 'main',
    name: 'Main Zone',
    power: 'on',
    available: true,
    volume: 80,
    minVolume: 0,
    maxVolume: 160,
    muted: false,
    input: 'server',
    inputList: ['hdmi1', 'server', 'tuner'],
    soundProgram: 'straight',
    soundProgramList: ['straight', '7ch_stereo'],
    toneControl: { mode: 'manual', bass: 1, treble: -1 },
    toneControlModeList: ['manual', 'auto', 'bypass'],
    linkControl: 'standard',
    linkControlList: ['speed', 'standard', 'stability'],
    extraBass: true,
    enhancer: false,
    rangeStep: [
      { id: 'volume', min: 0, max: 160, step: 1 },
      { id: 'tone_control', min: -12, max: 12, step: 1 },
    ],
  }, {
    zone: 'zone2',
    name: 'Patio',
    power: 'standby',
    available: true,
    volumeLevel: 0.25,
    muted: true,
    input: 'audio1',
    inputList: ['server', 'tuner', 'audio1'],
  }],
};

tap.test('maps Yamaha MusicCast zones to canonical devices', async () => {
  const devices = YamahaMusiccastMapper.toDevices(snapshot);
  expect(devices.length).toEqual(2);
  expect(devices[0].id).toEqual('yamaha_musiccast.player.03e88cf3');
  expect(devices[0].manufacturer).toEqual('Yamaha Corporation');
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'server')).toBeTrue();
  expect(devices[0].state.some((stateArg) => stateArg.featureId === 'capability_extra_bass' && stateArg.value === true)).toBeTrue();
  expect(devices[1].metadata?.viaDeviceId).toEqual('yamaha_musiccast.player.03e88cf3');
});

tap.test('maps Yamaha MusicCast zones to media, switch, select, and number entities', async () => {
  const entities = YamahaMusiccastMapper.toEntities(snapshot);
  const media = entities.find((entityArg) => entityArg.id === 'media_player.living_room_rx');
  const zone2 = entities.find((entityArg) => entityArg.id === 'media_player.living_room_rx_zone2');
  const extraBass = entities.find((entityArg) => entityArg.id === 'switch.living_room_rx_extra_bass');
  const linkControl = entities.find((entityArg) => entityArg.id === 'select.living_room_rx_link_control');
  const toneBass = entities.find((entityArg) => entityArg.id === 'number.living_room_rx_tone_control_bass');

  expect(media?.state).toEqual('paused');
  expect(media?.attributes?.volumeLevel).toEqual(0.5);
  expect(media?.attributes?.source).toEqual('Media Server');
  expect(media?.attributes?.mediaTitle).toEqual('Track One');
  expect(zone2?.state).toEqual('off');
  expect(extraBass?.state).toEqual(true);
  expect(extraBass?.attributes?.capabilityId).toEqual('extra_bass');
  expect(linkControl?.state).toEqual('standard');
  expect(toneBass?.state).toEqual(1);
  expect(toneBass?.attributes?.nativeMinValue).toEqual(-12);
});

export default tap.start();
