import SwiftUI
import Observation
import AVFoundation

@MainActor
@Observable class ApplicationData: NSObject, @preconcurrency AVSpeechSynthesizerDelegate {
   let synthesizer: AVSpeechSynthesizer
   var buttonLabel: String = "Talk"

   static let shared: ApplicationData = ApplicationData()
   private override init() {
      synthesizer = AVSpeechSynthesizer()
      super.init()
      synthesizer.delegate = self
   }
   func say(text: String) {
      let utterance = AVSpeechUtterance(string: text)
      let voiceID = "com.apple.voice.premium.en-US.Ava"
      utterance.voice = AVSpeechSynthesisVoice(identifier: voiceID)
      synthesizer.speak(utterance)
   }
   func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
      buttonLabel = "Pause"
   }
   func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
      buttonLabel = "Talk"
   }
   func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
      buttonLabel = "Pause"
   }
   func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
      buttonLabel = "Talk"
   }
}