package main

import (
	"errors"
	"github.com/gofiber/fiber/v2"
	"log"
	"net/url"
	"time"
)

type SceneCommand struct {
	Scene      string   `json:"scene"`
	PushUrl    string   `json:"pushUrl"`
	CameraUrls []string `json:"cameraURls"`
	proc       func(int64) error
}

type SceneData struct {
	State   string   `json:"state"`
	LiveUrl []string `json:"liveUrl"`
}

type SceneReadyResponse struct {
	Scene     string    `json:"scene"`
	Timestamp int64     `json:"timestamp"`
	SceneData SceneData `json:"sceneData"`
}

var currentScene *SceneCommand
var sceneStopChannel chan bool

func validateSceneCmd(cmd SceneCommand) error {
	switch cmd.Scene {
	case "pullUp", "standJump", "sitUps":
		if len(cmd.CameraUrls) < 1 {
			return errors.New("cameraUrls can not less than 1")
		}
	case "race":
		if len(cmd.CameraUrls) < 2 {
			return errors.New("cameraUrls can not less than 2")
		}
	default:
		return errors.New("unknown scene type:" + cmd.Scene)
	}
	if u, e := url.Parse(cmd.PushUrl); nil != e {
		return errors.New("invalid pushUrl:" + e.Error())
	} else if u.Scheme != "http" && u.Scheme != "https" {
		return errors.New("can not support pushUrl with " + u.Scheme)
	}
	return nil
}

func setScene(c *fiber.Ctx) error {
	var sceneCmd SceneCommand
	if err := c.BodyParser(&sceneCmd); nil != err {
		log.Println("ERROR:", c.Body())
		return failureResponse(c, "-1", err.Error(), 200)
	} else if nil != currentScene {
		return failureResponse(c, "1", "A working scene is started", 200)
	} else if err = validateSceneCmd(sceneCmd); nil != err {
		return failureResponse(c, "2", err.Error(), 200)
	} else {
		// currentScene = &sceneCmd
		log.Println("Starting scene ...")
		log.Println(sceneCmd)
		if e := sceneTaskProc(&sceneCmd); nil != e {
			return failureResponse(c, "3", err.Error(), 200)
		} else {
			return successResponse(c, "Updated successfully.")
		}
	}
}

func stopScene(c *fiber.Ctx) error {
	if nil != testStopChannel {
		testStopChannel <- true
	}
	if nil != sceneStopChannel {
		sceneStopChannel <- true
	}
	return successResponse(c, "Updated successfully.")
}

func getStatus(c *fiber.Ctx) error {
	var scene = ""
	var seq int64 = 0
	if nil != currentScene {
		scene = currentScene.Scene
	}
	if nil != currentTest {
		seq = currentTest.SequenceId
	}
	return successResponseEx(c, "success", scene, seq)
}

func sceneTaskProc(cmd *SceneCommand) error {
	var f func(int64) error = nil
	switch cmd.Scene {
	case "pullUp":
		f = pullUpTask
	case "standJump":
		f = standJumpTask
	case "sitUps":
		f = sitUpsTask
	case "race":
		f = raceTask
	default:
		return errors.New("unknown scene type:" + cmd.Scene)
	}
	sceneStopChannel = make(chan bool)
	var tk = time.NewTicker(time.Second * 3)
	go func() {
		defer func() {
			tk.Stop()
			close(sceneStopChannel)
			sceneStopChannel = nil
		}()
		currentScene = cmd
		currentScene.proc = f
		time.Sleep(time.Second)
		var st = SceneReadyResponse{
			Scene:     cmd.Scene,
			Timestamp: time.Now().Unix(),
			SceneData: SceneData{
				State:   "SCENE READY",
				LiveUrl: cmd.CameraUrls,
			},
		}
		go httpPostEx(cmd.PushUrl, st, 3)
		for {
			select {
			case <-tk.C:
				// log.Println(" ...")
			case <-sceneStopChannel:
				log.Println("Scene Stopping...")
				go pushEvent(cmd.PushUrl, cmd.Scene, 0, "SCENE END", false, false)
				currentScene = nil
				return
			}
		}
	}()
	return nil
}

func init() {
	currentScene = nil
	sceneStopChannel = nil
}
