import 'package:just_audio/just_audio.dart';

abstract class AudioService {
  Future<void> playTone({required String path});

  Future<void> listenAudioPlayerEvents({required Function() onAudioCompleted});

  Future<void> disposeAudioPlayer();
}

class AudioServiceImp implements AudioService {
  AudioPlayer audioPlayerInstance;

  AudioServiceImp({required this.audioPlayerInstance});

  @override
  Future<void> listenAudioPlayerEvents({required Function() onAudioCompleted}) async {
    audioPlayerInstance.playerStateStream.listen((playerState) async {
      if (playerState.processingState == ProcessingState.completed) {
        onAudioCompleted();
      }
    });
  }

  @override
  Future<void> playTone({required String path}) async {
    audioPlayerInstance.setAsset(path);
    await audioPlayerInstance.play();
  }

  @override
  Future<void> disposeAudioPlayer() async {
    await audioPlayerInstance.dispose();
  }
}
