package admin

import (
	"github.com/gin-gonic/gin"
	"server/dao"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils/logger"
	"strconv"
)

type TunnelApi struct{}

func (ta *TunnelApi) QueryAllTunnels(c *gin.Context) {
	tunnels, err := tunnelService.QueryAllTunnels()
	if err != nil {
		logger.Get().Error("QueryAllTunnels ---- " + err.Error())
		response.FailWithMessage("获取隧道失败", c)
		return
	}
	response.OkWithData(tunnels, c)
}

func (ta *TunnelApi) QueryTunnelList(c *gin.Context) {
	var info request.TunnelSearch
	err := c.ShouldBindJSON(&info)
	if err != nil {
		logger.Get().Error("QueryTunnelList ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	list, total, err := tunnelService.QueryTunnelList(info)
	if err != nil {
		logger.Get().Error("QueryTunnelList ---- " + err.Error())
		response.FailWithMessage("设备获取列表错误", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     info.PageInfo.Page,
		PageSize: info.PageInfo.PageSize,
	}, "获取成功", c)
}

func (ta *TunnelApi) QueryNoRegionTunnels(c *gin.Context) {
	tunnels, err := tunnelService.QueryNoRegionTunnels()
	if err != nil {
		logger.Get().Error("QueryNoRegionTunnels ---- " + err.Error())
		response.FailWithMessage("获取未分配区域隧道失败", c)
		return
	}
	response.OkWithData(tunnels, c)
}

func (ta *TunnelApi) CreateTunnel(c *gin.Context) {
	var tunnel dao.Tunnel
	if err := c.ShouldBindJSON(&tunnel); err != nil {
		logger.Get().Error("CreateTunnel ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.CreateTunnel(tunnel)
	if err != nil {
		logger.Get().Error("CreateTunnel ---- " + err.Error())
		response.FailWithMessage("创建隧道失败", c)
		return
	}
	response.OkWithMessage("创建隧道成功", c)
}

func (ta *TunnelApi) UpdateTunnel(c *gin.Context) {
	var tunnel dao.Tunnel
	if err := c.ShouldBindJSON(&tunnel); err != nil {
		logger.Get().Error("UpdateTunnel ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.UpdateTunnel(tunnel)
	if err != nil {
		logger.Get().Error("UpdateTunnel ---- " + err.Error())
		response.FailWithMessage("更新隧道失败", c)
		return
	}
	response.OkWithMessage("更新隧道成功", c)
}

func (ta *TunnelApi) UpdateTunnelLamp(c *gin.Context) {
	var info request.TunnelLamp
	err := c.ShouldBindJSON(&info)
	if err != nil {
		logger.Get().Error("UpdateTunnelLamp ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err = tunnelService.UpdateTunnelLamp(info)
	if err != nil {
		logger.Get().Error("UpdateTunnelLamp ---- " + err.Error())
		response.FailWithMessage("更新隧道灯光状态失败", c)
		return
	}
	response.OkWithMessage("更新隧道灯光状态成功", c)
}

func (ta *TunnelApi) UpdateTactics(c *gin.Context) {
	var tactics request.EditTactics
	if err := c.ShouldBindJSON(&tactics); err != nil {
		logger.Get().Error("UpdateTactics ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.UpdateTactics(tactics.Sn, tactics.Tactics)
	if err != nil {
		logger.Get().Error("UpdateTactics ---- " + err.Error())
		response.FailWithMessage("更新隧道策略失败", c)
		return
	}
	response.OkWithMessage("更新隧道策略成功", c)
}

func (ta *TunnelApi) UpdateTunnelsRegion(c *gin.Context) {
	var info request.RegionTunnelsId
	if err := c.ShouldBindJSON(&info); err != nil {
		logger.Get().Error("UpdateTunnelsRegion ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.UpdateTunnelsRegion(info.TunnelIds, info.RegionId)
	if err != nil {
		logger.Get().Error("UpdateTunnelsRegion ---- " + err.Error())
		response.FailWithMessage("更新隧道所属区域失败", c)
		return
	}
	response.OkWithMessage("更新隧道所属区域成功", c)
}

func (ta *TunnelApi) UpdateTunnelTime(c *gin.Context) {
	var tunnelTime dao.TunnelTime
	if err := c.ShouldBindJSON(&tunnelTime); err != nil {
		logger.Get().Error("UpdateTunnelTime ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err := tunnelService.UpdateTunnelTime(tunnelTime)
	if err != nil {
		logger.Get().Error("UpdateTunnelTime ---- " + err.Error())
		response.FailWithMessage("更新隧道定时失败", c)
		return
	}
	response.OkWithMessage("更新隧道定时成功", c)
}

func (ta *TunnelApi) DeleteTunnel(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		logger.Get().Error("DeleteTunnel ---- " + err.Error())
		response.FailWithMessage("参数错误", c)
		return
	}
	err = tunnelService.DeleteTunnel(id)
	if err != nil {
		logger.Get().Error("DeleteTunnel ---- " + err.Error())
		response.FailWithMessage("删除隧道失败", c)
		return
	}
	response.OkWithMessage("删除隧道成功", c)
}
