import 'package:flutter_translate/common/utils/bot_toast.dart';
import 'package:flutter_tts/flutter_tts.dart';

class Tts {
  Tts._();

  factory Tts() {
    return _instance;
  }

  static final Tts _instance = Tts._();

  late FlutterTts ttsController = FlutterTts();

  Future init() async {
    await ttsController.setSharedInstance(true);
    await ttsController.setIosAudioCategory(
        IosTextToSpeechAudioCategory.ambient,
        [
          IosTextToSpeechAudioCategoryOptions.allowBluetooth,
          IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
          IosTextToSpeechAudioCategoryOptions.mixWithOthers
        ],
        IosTextToSpeechAudioMode.voicePrompt);
    await ttsController.awaitSpeakCompletion(true);
    await ttsController.awaitSynthCompletion(true);
  }

  Future voiceTranslator(String text, String language) async {
    try {
      await ttsController.stop();
      await ttsController.setLanguage(language);
      await ttsController.speak(text);
    } catch (e) {
      toast('This language cannot be played');
    }
  }
}
