package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/middleware"
	edgeService "iot_manager_service/app/security/edge_service"
	"iot_manager_service/app/security/service"
	"iot_manager_service/util/common"
	"net/http"
	"strconv"
)

// TODO: 未记录日志
var Security = new(securityCtl)

type securityCtl struct{}

func (c *securityCtl) GetCameraLiveList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	list, err := service.SecurityService.GetCameraLiveList(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, list))
}

func (c *securityCtl) Operate(ctx *gin.Context) {
	code := ctx.Query("code")
	name := ctx.Query("name")
	atoi, _ := strconv.Atoi(ctx.Query("direction"))
	speed, _ := strconv.Atoi(ctx.Query("speed"))
	vidiconService := edgeService.ForVidiconService{}
	operate, err := vidiconService.Operate(code, name, atoi, speed)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
	}
	if !operate {
		ctx.JSON(http.StatusOK, nil)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}

func (c *securityCtl) Ptzhome(ctx *gin.Context) {
	code := ctx.Query("sn")
	name := ctx.Query("name")
	flag, _ := strconv.Atoi(ctx.Query("flag"))
	vidiconService := edgeService.ForVidiconService{}
	operate, err := vidiconService.Ptzhome(code, name, flag)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
	}
	if !operate {
		ctx.JSON(http.StatusOK, nil)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}
