import { expect, tap } from '@git.zone/tstest/tapbundle';
import { BluesoundClient, parseBluesoundInputsXml, parseBluesoundPresetsXml, parseBluesoundStatusXml, parseBluesoundSyncStatusXml, type IBluesoundRawCommandRequest } from '../../ts/integrations/bluesound/index.js';

tap.test('parses native Bluesound XML status and sync payloads', async () => {
  const sync = parseBluesoundSyncStatusXml('<SyncStatus etag="s1" id="192.168.1.10:11000" mac="AA:BB:CC:DD:EE:01" name="Living Room" icon="/icon.png" initialized="true" brand="Bluesound" model="NODE2I" modelName="NODE 2i" db="-32.5" volume="35"><slave id="192.168.1.11" port="11000" /></SyncStatus>');
  const status = parseBluesoundStatusXml('<status etag="st1"><state>play</state><name>Track</name><artist>Artist</artist><album>Album</album><image>/Artwork</image><volume>35</volume><db>-32.5</db><mute>0</mute><secs>12.8</secs><totlen>180</totlen><canSeek>1</canSeek><shuffle>1</shuffle><streamUrl>http://radio.example/jazz.mp3</streamUrl></status>');

  expect(sync.followers?.[0].ip).toEqual('192.168.1.11');
  expect(sync.modelName).toEqual('NODE 2i');
  expect(status.state).toEqual('play');
  expect(status.seconds).toEqual(12.8);
  expect(status.shuffle).toBeTrue();
});

tap.test('parses native Bluesound presets and inputs XML payloads', async () => {
  const presets = parseBluesoundPresetsXml('<presets><preset id="1" name="Jazz Radio" url="http://radio.example/jazz.mp3" image="/jazz.png" volume="40" /></presets>');
  const inputs = parseBluesoundInputsXml('<radiotime><item id="linein" text="Analog Input" image="/analog.png" URL="Capture%3Alinein" /></radiotime>');

  expect(presets[0].name).toEqual('Jazz Radio');
  expect(presets[0].volume).toEqual(40);
  expect(inputs[0].url).toEqual('Capture:linein');
});

tap.test('can build a live snapshot through a command executor', async () => {
  const requests: IBluesoundRawCommandRequest[] = [];
  const client = new BluesoundClient({
    commandExecutor: {
      execute: async (requestArg) => {
        requests.push(requestArg);
        if (requestArg.path === '/SyncStatus') {
          return '<SyncStatus etag="s1" id="192.168.1.10:11000" mac="AA:BB:CC:DD:EE:01" name="Living Room" brand="Bluesound" model="NODE2I" modelName="NODE 2i" db="-32.5" volume="35" />';
        }
        if (requestArg.path === '/Status') {
          return '<status etag="st1"><state>play</state><name>Track</name><volume>35</volume><db>-32.5</db><mute>0</mute></status>';
        }
        if (requestArg.path === '/Presets') {
          return '<presets><preset id="1" name="Jazz Radio" url="http://radio.example/jazz.mp3" /></presets>';
        }
        return '<radiotime><item id="linein" text="Analog Input" image="/analog.png" URL="Capture%3Alinein" /></radiotime>';
      },
    },
  });

  const snapshot = await client.getSnapshot();
  expect(snapshot.players[0].syncStatus.name).toEqual('Living Room');
  expect(snapshot.players[0].presets?.[0].name).toEqual('Jazz Radio');
  expect(requests.map((requestArg) => requestArg.path)).toEqual(['/SyncStatus', '/Status', '/Presets', '/RadioBrowse']);
});

export default tap.start();
