package middleware

import (
	"game-driver/internal/schema"
	"game-driver/leaf"
	"game-driver/pkg/tts"
)

// SoundStart 开始词播报
func SoundStart() leaf.HandlerFunc {
	return func(c *leaf.Context) {
		pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)

		// 使用请求的 context，支持取消和超时
		if pm.TTS.Start != "" {
			tts.DefaultTTS.Sound(c, pm.TTS.Start)
		}

		defer func() {
			var text string
			switch leaf.Value[leaf.EndType](c, leaf.EndKey) {
			case leaf.End:
				text = pm.TTS.End
			case leaf.EndTimeout:
				text = pm.TTS.Timeout
			case leaf.EndStop:
				text = pm.TTS.Stop
			}
			if text != "" {
				tts.DefaultTTS.Sound(c, text)
			}
		}()

		c.Next()
	}
}
