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 CaptureUint = new(captureUintCtl)

type captureUintCtl struct{}

func (c *captureUintCtl) Detail(ctx *gin.Context) {
	id, e := strconv.Atoi(ctx.Query("id"))
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	deviceType := ctx.Query("type")
	if deviceType == model.TypeCapture {
		device, err := service.CaptureUintService.GetCapture(id)
		if err != nil {
			ctx.JSON(http.StatusOK, err)
			return
		}
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
	} else if deviceType == model.TypePoint {
		device, err := service.CaptureUintService.GetPoint(id)
		if err != nil {
			ctx.JSON(http.StatusOK, err)
			return
		}
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, device))
	} else {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(model.TypeInvalid, nil))
	}
}

func (c *captureUintCtl) CaptureList(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, total, err := service.CaptureUintService.CaptureList(searchValue, current, size)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(total) / float64(size))
	rsp := model.RspCaptureList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
		Records: devices,
	}

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

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

	// 参数验证
	var req *model.CaptureDetail
	if err := ctx.ShouldBind(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.CaptureUintService.CaptureSubmit(claims.UserId, claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, nil)
}

func (c *captureUintCtl) PointList(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, total, err := service.CaptureUintService.PointList(searchValue, current, size)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(len(devices)) / float64(size))
	rsp := model.RspPointList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
		Records: devices,
	}

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

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

	// 参数验证
	var req *dao.CheckPoint
	if err := ctx.ShouldBind(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.CaptureUintService.PointSubmit(claims.UserId, claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, nil)
}

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

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

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

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

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

	var req *model.ReqCaptureRemove
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	if req.Type == model.TypeCapture {
		err := service.CaptureUintService.RemoveCapture(claims.UserId, claims.TenantId, req.IDs)
		if err != nil {
			ctx.JSON(http.StatusOK, err)
			return
		}
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
	} else if req.Type == model.TypePoint {
		err := service.CaptureUintService.RemovePoint(claims.UserId, claims.TenantId, req.IDs)
		if err != nil {
			ctx.JSON(http.StatusOK, err)
			return
		}
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
	} else {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(model.TypeInvalid, nil))
	}
}
