package player

import (
	"context"
	"netease_control_center/globals"
	"netease_control_center/interfaces/http"
	"netease_control_center/interfaces/log"
	"netease_control_center/interfaces/schedule"
)

var (
	hdNoticeCur     schedule.SSceneHd
	taskNoNoticeCur int

	ctx    context.Context    = nil
	cancle context.CancelFunc = nil
)

func ScenesNoticePlay(startType schedule.PlayerStartType, position int, hd schedule.SSceneHd, taskno int) {
	hdNoticeCur = hd
	taskNoNoticeCur = taskno

	if arg, err := hd.GetCurServerScenetask(); nil == err {
		switch schedule.SSTaskId(arg.LocalId) {
		case schedule.SSTaskId_QueryWeather:
			weather, _, _, err := http.GetWeatherInfo()
			if nil == err {
				if nil != hd.GetCtx() {
					ctx, cancle = context.WithCancel(*hd.GetCtx())
				} else {
					ctx, cancle = context.WithCancel(context.Background())
				}

				schedule.PlayNoticeAsync(globals.AdSrcType_TTS, weather.Tts, globals.CONST_AUDIO_ID, true, ctx, noticeScenePlayerCb)
			} else {
				hd.Finish(taskno, true)
			}

		default:
			log.Println(log.ErrorLog, "err type of localId:", arg.LocalId)
			hd.Finish(taskno, true)
		}
	}
}

func ScenesNoticeStop(hd schedule.SSceneHd) {
	if nil != cancle {
		cancle()
	}
}

func noticeScenePlayerCb(id uint32, reason schedule.NoticeStopReason) {
	if reason.IsStart() {
		return
	}
	switch reason {
	case schedule.NoticeStopReason_Interrupt,
		schedule.NoticeStopReason_Cancel,
		schedule.NoticeStopReason_Error:
		log.Println(log.ErrorLog, "finish task of scene")
		hdNoticeCur.Finish(taskNoNoticeCur, false)
	case schedule.NoticeStopReason_Complete:
		log.Println(log.ErrorLog, "finish task of scene")
		hdNoticeCur.Finish(taskNoNoticeCur, false)
	default:

	}
	NoticeTaskHander.FinishExec()
}
