import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:chanora_flutter/src/rust/api.dart' as rust;
import 'package:chanora_flutter/widgets/audio_device_list_tile.dart';

void main() {
  testWidgets(
    'audio device tile renders loading state before devices resolve',
    (tester) async {
      final pendingDevices = Completer<rust.BridgeAudioDeviceList>();

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: AudioDeviceListTile(
              label: 'Input',
              kind: AudioDeviceKind.input,
              loadDevices: () => pendingDevices.future,
            ),
          ),
        ),
      );

      expect(find.text('Input'), findsOneWidget);
      expect(find.text('Loading...'), findsOneWidget);
      expect(find.byType(CircularProgressIndicator), findsOneWidget);
    },
  );

  testWidgets('audio device tile shows selected device and details', (
    tester,
  ) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: AudioDeviceListTile(
            label: 'Output',
            kind: AudioDeviceKind.output,
            loadDevices: () async => const rust.BridgeAudioDeviceList(
              inputDevices: [],
              outputDevices: [
                rust.BridgeAudioDevice(
                  id: 'default-speakers',
                  name: 'Speakers',
                  details: 'Realtek · Speaker · id=1111',
                  isDefault: true,
                  isSelected: false,
                ),
                rust.BridgeAudioDevice(
                  id: 'usb-headset',
                  name: 'USB Headset',
                  details: 'SteelSeries · Headset · id=2222',
                  isDefault: false,
                  isSelected: true,
                ),
              ],
            ),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('USB Headset'), findsOneWidget);
    expect(find.text('SteelSeries · Headset · id=2222'), findsNothing);

    await tester.tap(find.text('Output'));
    await tester.pumpAndSettle();

    expect(find.text('SteelSeries · Headset · id=2222'), findsOneWidget);
    expect(find.text('System default'), findsOneWidget);
  });

  testWidgets('audio device tile selects by id', (tester) async {
    String? selectedId = 'device-a';

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: AudioDeviceListTile(
            label: 'Input',
            kind: AudioDeviceKind.input,
            loadDevices: () async => const rust.BridgeAudioDeviceList(
              inputDevices: [
                rust.BridgeAudioDevice(
                  id: 'device-a',
                  name: 'Built-in Mic',
                  details: 'Realtek · Microphone · id=aaaa',
                  isDefault: true,
                  isSelected: true,
                ),
                rust.BridgeAudioDevice(
                  id: 'device-b',
                  name: 'USB Mic',
                  details: 'Shure · Microphone · id=bbbb',
                  isDefault: false,
                  isSelected: false,
                ),
              ],
              outputDevices: [],
            ),
            setInputDevice: ({String? id}) async {
              selectedId = id;
            },
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();
    await tester.tap(find.text('Input'));
    await tester.pumpAndSettle();
    await tester.tap(find.text('USB Mic'));
    await tester.pump();

    expect(selectedId, 'device-b');
  });
}
