package play

import (
	"game-driver/config/game"
	"game-driver/internal/middleware"
	"game-driver/internal/schema"
	"game-driver/leaf"
	"game-driver/pkg/oscx"
	"go.uber.org/zap"
	"time"
)

func LaserShow(c *leaf.Context) {
	cfg := (game.C).(game.LaserConfig)

	payload := leaf.Value[*schema.PlayModal](c, middleware.PayloadJSONKey)

	if data, ok := payload.Game["osc"].(string); ok {
		zap.S().Infoln("开始播放激光秀:", data)

		o := oscx.New(cfg.Host, cfg.Port)
		err := o.EnableLaserOutput()
		if err != nil {
			zap.S().Warnln("激光打开异常:", err)
			return
		} else {
			defer zap.S().Infoln("激光秀播放结束:", data)
			defer o.DisableLaserOutput()

			err = o.StartCue(data)
			if err != nil {
				zap.S().Warnln("播放激光节目异常:", err)
				return
			}
		}
	}
	if w, ok := payload.Game["wait"]; ok {
		if v, ok := w.(float64); ok {
			select {
			case <-c.Done():
			case <-time.After(time.Duration(v) * time.Second):
			}
		}
	}
}
