import 'package:flutter_test/flutter_test.dart';
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
import 'package:wolf_3d_flutter/wolf_3d_flutter.dart';

class _CountingAudio implements EngineAudio {
  @override
  WolfensteinData? activeGame;

  int stopAllAudioCallCount = 0;
  int disposeCallCount = 0;

  @override
  Future<void> debugSoundTest() async {}

  @override
  Future<void> init() async {}

  @override
  void playLevelMusic(Music music) {}

  @override
  void playMenuMusic() {}

  @override
  void playSoundEffect(SoundEffect effect) {}

  @override
  void playSoundEffectId(int sfxId) {}

  @override
  Future<void> stopAllAudio() async {
    stopAllAudioCallCount++;
    await Future<void>.delayed(const Duration(milliseconds: 1));
  }

  @override
  void stopMusic() {}

  @override
  void dispose() {
    disposeCallCount++;
  }
}

void main() {
  group('Wolf3d.shutdownAudio', () {
    test('stops and disposes audio once', () async {
      final audio = _CountingAudio();
      final wolf3d = Wolf3d(audioBackend: audio);

      await wolf3d.shutdownAudio();
      await wolf3d.shutdownAudio();

      expect(audio.stopAllAudioCallCount, 1);
      expect(audio.disposeCallCount, 1);
    });

    test('concurrent calls share the same shutdown work', () async {
      final audio = _CountingAudio();
      final wolf3d = Wolf3d(audioBackend: audio);

      await Future.wait<void>([
        wolf3d.shutdownAudio(),
        wolf3d.shutdownAudio(),
        wolf3d.shutdownAudio(),
      ]);

      expect(audio.stopAllAudioCallCount, 1);
      expect(audio.disposeCallCount, 1);
    });
  });
}
