package controller

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

var IntelligentLighting = new(intelligentLightingCtl)

type intelligentLightingCtl struct{}

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

	publicId, e := strconv.Atoi(ctx.Query("publicId"))
	reqType, _ := strconv.Atoi(ctx.Query("type"))
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	if publicId == 0 {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(model.RelationIdInvalid, nil))
		return
	}

	detail, err := service.IntelligentLightingService.GetDetailByLight(claims.TenantId, publicId, reqType)

	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, detail))
}

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

	searchValue := ctx.Query("searchValue")
	groupId := ctx.Query("groupId")
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	gId, err := strconv.Atoi(groupId)
	if err != nil {
		//ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, "分组没选 "))
		//return
		gId = 0
	}
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	strategies, total, err := service.IntelligentLightingService.List(claims.TenantId, searchValue, current, size, gId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(len(strategies)) / float64(size))
	rsp := model.RspIntelligentLightList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
		Records: strategies,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

// RelationList 策略列表
func (c *intelligentLightingCtl) RelationList(ctx *gin.Context) {
	//value, _ := ctx.Get(middleware.Authorization)
	//claims := value.(*middleware.Claims)
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	searchValue := ""
	strategies, _, err := service.LightStrategyService.List(searchValue, current, size)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	rsp := strategies
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

// Relation 关联策略
func (c *intelligentLightingCtl) Relation(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	publicId, _ := strconv.Atoi(ctx.Query("publicId"))
	lightId, _ := strconv.Atoi(ctx.Query("lightId"))
	relationType, _ := strconv.Atoi(ctx.Query("relationType"))

	err := service.IntelligentLightingService.Relation(claims.UserId, claims.TenantId, publicId, lightId, relationType)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}

// Remove 恢复到分组策略
func (c *intelligentLightingCtl) Recovery(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	groupId, _ := strconv.Atoi(ctx.Query("groupId"))
	id, _ := strconv.Atoi(ctx.Query("id"))

	err := service.IntelligentLightingService.Recovery(claims.UserId, claims.TenantId, groupId, id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}

	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}

// ChangeHandSwitch 开灯控制
func (c *intelligentLightingCtl) ChangeHandSwitch(ctx *gin.Context) {
	handSwitch, _ := strconv.Atoi(ctx.Query("handSwitch"))
	handType, _ := strconv.Atoi(ctx.Query("handType"))
	publicId, _ := strconv.Atoi(ctx.Query("publicId"))
	handTime, _ := strconv.Atoi(ctx.Query("handTime"))
	luminance, _ := strconv.Atoi(ctx.Query("luminance"))
	explain := ctx.Query("explain")
	publicName := ctx.Query("publicName")
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	err := service.IntelligentLightingService.ChangeHandSwitch(claims.UserId, claims.TenantId, handSwitch, handType, publicId, handTime, luminance, explain, publicName)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}
func (c *intelligentLightingCtl) GetStatus(ctx *gin.Context) {

}
