import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BluesoundMapper, type IBluesoundSnapshot } from '../../ts/integrations/bluesound/index.js';

const snapshot: IBluesoundSnapshot = {
  online: true,
  source: 'snapshot',
  players: [{
    host: '192.168.1.10',
    port: 11000,
    syncStatus: {
      id: '192.168.1.10:11000',
      mac: 'AA:BB:CC:DD:EE:01',
      name: 'Living Room',
      brand: 'Bluesound',
      model: 'NODE2I',
      modelName: 'NODE 2i',
      volume: 35,
      followers: [{ ip: '192.168.1.11', port: 11000 }],
      group: 'Downstairs',
    },
    status: {
      state: 'play',
      name: 'Example Track',
      artist: 'Example Artist',
      album: 'Example Album',
      image: '/Artwork?sid=1',
      volume: 35,
      mute: false,
      seconds: 31.6,
      totalSeconds: 181.2,
      streamUrl: 'http://radio.example/jazz.mp3',
      shuffle: true,
      canSeek: true,
      service: 'Radio Paradise',
    },
    presets: [{ id: 1, name: 'Jazz Radio', url: 'http://radio.example/jazz.mp3', image: '/preset.jpg' }],
    inputs: [{ id: 'linein', text: 'Analog Input', image: '/input.png', url: 'Capture:linein' }],
    available: true,
  }, {
    host: '192.168.1.11',
    port: 11000,
    syncStatus: {
      id: '192.168.1.11:11000',
      mac: 'AA:BB:CC:DD:EE:02',
      name: 'Kitchen',
      brand: 'Bluesound',
      model: 'PULSE',
      modelName: 'PULSE Flex',
      volume: 35,
      leader: { ip: '192.168.1.10', port: 11000 },
      group: 'Downstairs',
    },
    status: { state: 'play', volume: 25, mute: false, name: 'Follower Track' },
    presets: [],
    inputs: [],
    available: true,
  }],
};

tap.test('maps Bluesound players to devices with media source state', async () => {
  const devices = BluesoundMapper.toDevices(snapshot);
  const livingRoom = devices.find((deviceArg) => deviceArg.id === 'bluesound.player.aa_bb_cc_dd_ee_01');

  expect(livingRoom?.manufacturer).toEqual('Bluesound');
  expect(livingRoom?.state.some((stateArg) => stateArg.featureId === 'source' && stateArg.value === 'Jazz Radio')).toBeTrue();
  expect(livingRoom?.state.some((stateArg) => stateArg.featureId === 'presets' && stateArg.value === 1)).toBeTrue();
});

tap.test('maps Bluesound media players presets sources and groups', async () => {
  const entities = BluesoundMapper.toEntities(snapshot);
  const livingRoom = entities.find((entityArg) => entityArg.id === 'media_player.living_room');
  const kitchen = entities.find((entityArg) => entityArg.id === 'media_player.kitchen');
  const presets = entities.find((entityArg) => entityArg.id === 'sensor.living_room_bluesound_presets');
  const sources = entities.find((entityArg) => entityArg.id === 'sensor.living_room_bluesound_sources');

  expect(livingRoom?.state).toEqual('playing');
  expect(livingRoom?.attributes?.volumeLevel).toEqual(0.35);
  expect(livingRoom?.attributes?.source).toEqual('Jazz Radio');
  expect(livingRoom?.attributes?.mediaPosition).toEqual(31);
  expect(livingRoom?.attributes?.mediaDuration).toEqual(181);
  expect(livingRoom?.attributes?.groupMembers).toEqual(['media_player.living_room', 'media_player.kitchen']);
  expect(kitchen?.state).toEqual('idle');
  expect(kitchen?.attributes?.mediaTitle).toEqual(undefined);
  expect(presets?.state).toEqual(1);
  expect(sources?.attributes?.sourceList).toEqual(['Jazz Radio', 'Analog Input']);
});

export default tap.start();
