package standby

import (
	"context"
	"fmt"
	"game-driver/internal/schema"
	"game-driver/pkg/utils"
	"game-driver/pkg/video"
	"go.uber.org/zap"
	"time"
)

func Video(item schema.WaitItemModel) func(c context.Context) error {
	return func(c context.Context) error {
		path, local, err := utils.LinkVideo(item.Data)
		if err != nil {
			return fmt.Errorf("视频文件获取异常: %w", err)
		}

		zap.S().Infoln("播放待机视频")
		defer zap.S().Infoln("结束待机视频")

		utils.BlankOpen()
		defer utils.BlankClose()

		for {
			err := video.Play(c, path, local)
			if err != nil {
				return fmt.Errorf("视频播放异常: %w", err)
			}
			select {
			case <-c.Done():
				return nil
			case <-time.After(time.Duration(item.Interval) * time.Second):
			}
		}
	}
}
