package standby

import (
	"context"
	"fmt"
	"game-driver/internal/schema"
	"game-driver/pkg/audio"
	"game-driver/pkg/utils"
	"github.com/gopxl/beep/v2/speaker"
	"go.uber.org/zap"
)

func Audio(item schema.WaitItemModel) func(c context.Context) error {
	return func(c context.Context) error {
		data, err := utils.LinkAudio(item.Data)
		if err != nil {
			return fmt.Errorf("音频数据获取异常: %w", err)
		}
		if data == nil {
			return fmt.Errorf("音频数据获取为空")
		}

		zap.S().Infoln("播放待机音乐")
		defer zap.S().Infoln("结束待机音乐")

		ctrl, closer, e := audio.PlayBgmMP3(data)
		defer closer()
		if e != nil {
			return fmt.Errorf("播放待机音乐异常: %w", e)
		}

		<-c.Done()

		speaker.Lock()
		ctrl.Streamer = nil
		speaker.Unlock()

		return nil
	}
}
