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 LampPole = new(lampPoleCtl)

type lampPoleCtl struct{}

func (c *lampPoleCtl) 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.LampPoleService.Get(id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
}

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

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

func (c *lampPoleCtl) List(ctx *gin.Context) {
	searchValue := ctx.Query("searchValue")
	groupId := ctx.Query("groupId")
	boxId := ctx.Query("boxId")
	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, total, err := service.LampPoleService.List(searchValue, groupId, boxId, current, size)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(total) / float64(size))
	rsp := model.RspLampPoleList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
	}
	for _, device := range devices {
		rsp.Records = append(rsp.Records, lampPoleDaoToModel(device))
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

func (c *lampPoleCtl) GetRelevanceDetail(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.LampPoleService.GetRelevanceDetail(id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
}

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

	var req *model.ReqLampPoleRemove
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.LampPoleService.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 *lampPoleCtl) ImportExcel(ctx *gin.Context) {
}

func (c *lampPoleCtl) ExportExcel(ctx *gin.Context) {
}

func (c *lampPoleCtl) ExportTemplate(ctx *gin.Context) {
}

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

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

func lampPoleDaoToModel(device dao.LampPole) model.LampPoleDetail {
	return model.LampPoleDetail{
		LampPole:          device,
		AlarmTerminalList: nil,
		CameraList:        nil,
		CaptureUnitList:   nil,
		GatewayList:       nil,
		InfoBoardList:     nil,
		IpBroadcastList:   nil,
		LightControlList:  nil,
		SensorList:        nil,
		ZigbeeList:        nil,
	}
}
