package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/device/dao"
	"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 OnDemandGroup = new(onDemandGroupCtl)

type onDemandGroupCtl struct{}

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

	device, err := service.OnDemandGroupService.Get(id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
}

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

	devices, err := service.OnDemandGroupService.List(searchValue, current, size)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(len(devices)) / float64(size))
	rsp := model.RspOnDemandGroupList{
		Current: current,
		Size:    size,
		Total:   len(devices),
		Pages:   int(pages),
		Records: devices,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

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

	var req *dao.OnDemandGroup
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.OnDemandGroupService.CreateOrUpdate(claims.UserId, claims.TenantId, req)
	ctx.JSON(http.StatusOK, err)
}

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

	var req *model.ReqOnDemandGroupRemove
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.OnDemandGroupService.Remove(claims.UserId, claims.TenantId, req.IDs)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}

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

	devices, err := service.OnDemandGroupService.GetList(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, devices))
}

func (c *onDemandGroupCtl) GroupNumber(ctx *gin.Context) {
	id, e := strconv.Atoi(ctx.Query("id"))
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	if id <= 0 {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(model.ParamsInvalid, nil))
		return
	}

	device, err := service.OnDemandGroupService.GroupNumberList(id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
}
