package admin

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

type DeviceApi struct{}

func (da *DeviceApi) QueryAllDevices(c *gin.Context) {
	devices, err := deviceService.QueryAllDevices()
	if err != nil {
		logger.Get().Error("QueryAllDevices ---- " + err.Error())
		response.FailWithMessage("设备获取设备", c)
		return
	}
	response.OkWithData(devices, c)
}

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

func (da *DeviceApi) GenerateDeviceFile(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		logger.Get().Error("GenerateDeviceFile ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	data, err := deviceService.GenerateDeviceFile(id)
	if err != nil {
		logger.Get().Error("GenerateDeviceFile ---- " + err.Error())
		response.FailWithMessage("设备文件生成失败", c)
		return
	}
	response.OkWithData(data, c)
}

func (da *DeviceApi) DeviceSwitch(c *gin.Context) {
	var info request.SwitchTunnel
	err := c.ShouldBindJSON(&info)
	if err != nil {
		logger.Get().Error("DeviceSwitch ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err = deviceService.DeviceSwitch(info)
	if err != nil {
		logger.Get().Error("DeviceSwitch ---- " + err.Error())
		response.FailWithMessage("设备状态切换失败", c)
		return
	}
	response.Ok(c)
}

func (da *DeviceApi) CreateDevice(c *gin.Context) {
	var device dao.Device
	err := c.ShouldBindJSON(&device)
	if err != nil {
		logger.Get().Error("CreateDevice ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err = deviceService.CreateDevice(device)
	if err != nil {
		logger.Get().Error("CreateDevice ---- " + err.Error())
		response.FailWithMessage("设备创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (da *DeviceApi) UpdateDevice(c *gin.Context) {
	var device dao.Device
	err := c.ShouldBindJSON(&device)
	if err != nil {
		logger.Get().Error("UpdateDevice ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err = deviceService.UpdateDevice(device)
	if err != nil {
		logger.Get().Error("UpdateDevice ---- " + err.Error())
		response.FailWithMessage("设备更新失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (da *DeviceApi) DeleteDevice(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		logger.Get().Error("DeleteDevice ---- " + err.Error())
		response.FailWithMessage("参数错误", c)
		return
	}
	err = deviceService.DeleteDevice(id)
	if err != nil {
		logger.Get().Error("DeleteDevice ---- " + err.Error())
		response.FailWithMessage("设备删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (da *DeviceApi) QueryPowerStatisticsBySn(c *gin.Context) {
	sn := c.Query("sn")
	if sn == "" {
		response.FailWithMessage("序号不能为空", c)
		return
	}
	statistics, err := deviceService.QueryPowerStatisticsBySn(sn)
	if err != nil {
		logger.Get().Error("QueryPowerStatisticsBySn ---- " + err.Error())
		response.FailWithMessage("查询功率统计失败", c)
		return
	}
	response.OkWithDetailed(statistics, "查询功率统计成功", c)
}
