import { createServer } from 'node:net';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantOnkyoIntegration, OnkyoClient, OnkyoConfigFlow, OnkyoIntegration, OnkyoMapper, createOnkyoDiscoveryDescriptor, onkyoProfile, type IOnkyoSnapshot, type TOnkyoRawData } from '../../ts/integrations/onkyo/index.js';

const rawData: TOnkyoRawData = {
  device: {
    id: 'onkyo-device-1',
    name: "Onkyo Device",
    manufacturer: "Onkyo",
    model: "Onkyo local integration",
    serialNumber: 'onkyo-serial-1',
  },
  entities: [
    { id: 'status', name: 'Status', platform: "media_player", state: true, attributes: { domain: "onkyo" } },
  ],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
};

tap.test('matches manual Onkyo candidates and creates config flow output', async () => {
  const descriptor = createOnkyoDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'onkyo-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'onkyo-device-1', name: "Onkyo Device", metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual("onkyo");

  const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();

  const done = await (await new OnkyoConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.uniqueId).toEqual('onkyo-device-1');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Onkyo raw snapshots to runtime devices and entities', async () => {
  const client = new OnkyoClient({ name: "Onkyo Runtime", rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = OnkyoMapper.toSnapshotFromRaw({ name: "Onkyo Runtime" }, rawData);
  const devices = OnkyoMapper.toDevices(mappedSnapshot);
  const entities = OnkyoMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("onkyo");
  expect(devices[0].manufacturer).toEqual("Onkyo");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "onkyo" && entityArg.platform === "media_player")).toBeTrue();
});

tap.test('polls and controls Onkyo receivers through the native eISCP TCP protocol', async () => {
  const commands: string[] = [];
  const state = {
    power: '01',
    volume: 0x20,
    muted: '00',
    source: '10',
    listeningMode: '00',
  };
  const server = createServer((socketArg) => {
    let buffer = Buffer.alloc(0);
    socketArg.on('data', (chunkArg) => {
      buffer = Buffer.concat([buffer, Buffer.isBuffer(chunkArg) ? chunkArg : Buffer.from(chunkArg)]);
      const parsed = parseEiscpFrames(buffer);
      buffer = Buffer.from(parsed.remaining);
      for (const command of parsed.commands) {
        commands.push(command);
        socketArg.write(eiscpResponse(handleOnkyoCommand(command, state)));
      }
    });
  });
  await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));

  try {
    const address = server.address();
    const port = typeof address === 'object' && address ? address.port : 0;
    const client = new OnkyoClient({ host: '127.0.0.1', port, timeoutMs: 1000, name: 'Theater Receiver', volumeResolution: 80 });
    const snapshot = await client.getSnapshot(true);
    const volume = await client.execute({ domain: 'media_player', service: 'volume_up', target: {} });
    const source = await client.execute({ domain: 'media_player', service: 'select_source', target: {}, data: { source: 'Bluetooth' } });

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('tcp');
    expect(snapshot.entities[0].state).toEqual('on');
    expect(snapshot.entities[0].attributes?.source).toEqual('BD/DVD');
    expect(snapshot.entities[0].attributes?.volume_level).toEqual(0.4);
    expect(volume.success).toBeTrue();
    expect(source.success).toBeTrue();
    expect(commands.includes('MVLUP')).toBeTrue();
    expect(commands.includes('SLI2E')).toBeTrue();

    const runtime = await new OnkyoIntegration().setup({ host: '127.0.0.1', port, timeoutMs: 1000, name: 'Theater Receiver', volumeResolution: 80 }, {});
    const status = await runtime.callService!({ domain: 'onkyo', service: 'status', target: {} });
    expect(status.success).toBeTrue();
    expect((status.data as IOnkyoSnapshot).source).toEqual('tcp');
    expect((status.data as IOnkyoSnapshot).entities[0].attributes?.source).toEqual('Bluetooth');
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('exposes Onkyo runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new OnkyoIntegration();
  const alias = new HomeAssistantOnkyoIntegration();
  expect(alias instanceof OnkyoIntegration).toBeTrue();
  expect(alias.domain).toEqual("onkyo");
  expect(integration.status).toEqual("control-runtime");
  expect(onkyoProfile.metadata.configFlow).toEqual(true);
  expect(onkyoProfile.metadata.requirements).toEqual([
    "aioonkyo==0.4.0",
  ]);

  const runtime = await integration.setup({ name: "Onkyo Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "onkyo", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "onkyo", service: 'refresh', target: {} });
  const snapshot = statusResult.data as IOnkyoSnapshot;

  expect(statusResult.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual("Onkyo Device");

  const command = await runtime.callService!({ domain: "onkyo", service: onkyoProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();

const eiscpResponse = (commandArg: string): Buffer => {
  const data = Buffer.from(`!1${commandArg}\r`, 'ascii');
  const header = Buffer.alloc(16);
  header.write('ISCP', 0, 'ascii');
  header.writeUInt32BE(16, 4);
  header.writeUInt32BE(data.length, 8);
  header.writeUInt8(1, 12);
  return Buffer.concat([header, data]);
};

const parseEiscpFrames = (bufferArg: Buffer): { commands: string[]; remaining: Buffer } => {
  const commands: string[] = [];
  let offset = 0;
  while (bufferArg.length - offset >= 16) {
    const headerSize = bufferArg.readUInt32BE(offset + 4);
    const dataSize = bufferArg.readUInt32BE(offset + 8);
    const frameSize = headerSize + dataSize;
    if (bufferArg.length - offset < frameSize) {
      break;
    }
    const body = bufferArg.subarray(offset + headerSize, offset + frameSize).toString('ascii').replace(/[\u0000\r\n]+$/g, '');
    commands.push(body.slice(2));
    offset += frameSize;
  }
  return { commands, remaining: Buffer.from(bufferArg.subarray(offset)) };
};

const handleOnkyoCommand = (commandArg: string, stateArg: { power: string; volume: number; muted: string; source: string; listeningMode: string }): string => {
  if (commandArg === 'PWRQSTN') {
    return `PWR${stateArg.power}`;
  }
  if (commandArg === 'MVLQSTN') {
    return `MVL${stateArg.volume.toString(16).toUpperCase().padStart(2, '0')}`;
  }
  if (commandArg === 'AMTQSTN') {
    return `AMT${stateArg.muted}`;
  }
  if (commandArg === 'SLIQSTN') {
    return `SLI${stateArg.source}`;
  }
  if (commandArg === 'LMDQSTN') {
    return `LMD${stateArg.listeningMode}`;
  }
  if (commandArg.startsWith('PWR')) {
    stateArg.power = commandArg.slice(3);
    return commandArg;
  }
  if (commandArg === 'MVLUP') {
    stateArg.volume += 1;
    return `MVL${stateArg.volume.toString(16).toUpperCase().padStart(2, '0')}`;
  }
  if (commandArg === 'MVLDOWN') {
    stateArg.volume -= 1;
    return `MVL${stateArg.volume.toString(16).toUpperCase().padStart(2, '0')}`;
  }
  if (commandArg.startsWith('MVL')) {
    stateArg.volume = Number.parseInt(commandArg.slice(3), 16);
    return commandArg;
  }
  if (commandArg.startsWith('AMT')) {
    stateArg.muted = commandArg.slice(3);
    return commandArg;
  }
  if (commandArg.startsWith('SLI')) {
    stateArg.source = commandArg.slice(3);
    return commandArg;
  }
  return commandArg;
};
