package controller

import (
	"github.com/gin-gonic/gin"
	dev_controller "iot_manager_service/app/device/controller"
	"iot_manager_service/app/middleware"
	"iot_manager_service/app/multimedia/model"
	"iot_manager_service/app/multimedia/service"
	"iot_manager_service/util/common"
	"math"
	"net/http"
	"strconv"
	"strings"
)

var Program = new(programCtl)

type programCtl struct{}

func (c *programCtl) Detail(ctx *gin.Context) {
	id, e := strconv.Atoi(ctx.Query("id"))
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	Program, err := service.ProgramService.Get(id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
}

func (c *programCtl) List(ctx *gin.Context) {
	searchValue := ctx.Query("searchValue")
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	s := ctx.Query("sysType")
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	sysType := 0
	if s != "" {
		sysType, _ = strconv.Atoi(s)
	}

	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	libraries, total, err := service.ProgramService.List(claims.TenantId, searchValue, current, size, sysType)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(total) / float64(size))
	rsp := model.RsqProgramList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
		Records: libraries,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
}

func (c *programCtl) Remove(ctx *gin.Context) {
	ids := ctx.Query("ids")
	id, e := strconv.Atoi(ids)
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}

	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	err := service.ProgramService.Remove(claims.UserId, claims.TenantId, id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}

func (c *programCtl) GetList(ctx *gin.Context) {
	name := ctx.Query("name")
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	Program, err := service.ProgramService.GetList(claims.TenantId, name)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, Program))
}

func (c *programCtl) Submit(ctx *gin.Context) {
	var req model.ReqProgramSubmit
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	e := service.ProgramService.Submit(claims.TenantId, claims.UserId, req)
	if e != nil {
		ctx.JSON(http.StatusOK, e)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}

func (c *programCtl) GetLibraryList(ctx *gin.Context) {
	id := ctx.Query("id")
	programId, e := strconv.Atoi(id)
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	libraries, err := service.ProgramService.GetLibraryList(claims.TenantId, programId)
	if err != nil {
		ctx.JSON(http.StatusOK, e)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, libraries))
}

func (c *programCtl) RelationDeviceList(ctx *gin.Context) {
	resolutionStr := ctx.Query("resolution")
	sysTypeStr := ctx.Query("sysType")
	resolution := 0
	if resolutionStr != "" {
		resolution, _ = strconv.Atoi(resolutionStr)
	}
	sysType := 0
	if sysTypeStr != "" {
		sysType, _ = strconv.Atoi(sysTypeStr)
	}

	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	devices, err := service.ProgramService.RelationDeviceList(claims.TenantId, sysType, resolution)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, devices))
}

func (c *programCtl) RelationIpBroadcastList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	devices, err := service.ProgramService.RelationDeviceList(claims.TenantId, 1, 0)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, devices))
}

func (c *programCtl) Audit(ctx *gin.Context) {
	var req model.ReqProgramAudit
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	// 链接
	err := service.PublishLibrariesService.Audit(req)
	if err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}

	split := strings.Split(req.InfoId, ",")
	for _, s := range split {
		id, err := strconv.Atoi(s)
		if err != nil {
			dev_controller.InfoBoard.CronSyncLedPaying(id)
		}
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}
