package uhf

import (
	"fmt"
	"wails-app/internal/model/common/response"
	"wails-app/internal/model/uhf/request"
	"wails-app/internal/service/uhf"

	"github.com/gin-gonic/gin"
)

func CreateReader(c *gin.Context) {
	var req request.UHFReaderCreate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := uhf.UHFService.CreateReader(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("创建成功", c)
}

func UpdateReader(c *gin.Context) {
	var req request.UHFReaderUpdate
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := uhf.UHFService.UpdateReader(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("更新成功", c)
}

func ConnectedDevice(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	err = uhf.UHFService.ConnectedDevice(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.Ok(c)
}

func GetReaderByCode(c *gin.Context) {
	code := c.Query("code")
	if code == "" {
		response.FailWithMessage("设备编码不能为空", c)
		return
	}

	reader, err := uhf.UHFService.GetReaderByCode(code)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(reader, c)
}

func QueryAllReaders(c *gin.Context) {
	readers, err := uhf.UHFService.QueryAllReaders()
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(readers, c)
}

func ListReaders(c *gin.Context) {
	var req request.UHFReaderQuery
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := uhf.UHFService.ListReaders(req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, "获取成功", c)
}

func DeleteReader(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	err = uhf.UHFService.DeleteReader(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("删除成功", c)
}

func ConnectReader(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	err = uhf.UHFService.ConnectReader(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("连接成功", c)
}

func DisconnectReader(c *gin.Context) {
	id := c.Query("id")
	uintID := uint(0)
	_, err := fmt.Sscanf(id, "%d", &uintID)
	if err != nil {
		response.FailWithMessage("无效的ID", c)
		return
	}

	err = uhf.UHFService.DisconnectReader(uintID)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("断开连接成功", c)
}

func SetReaderPower(c *gin.Context) {
	var req request.UHFReaderPower
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := uhf.UHFService.SetReaderPower(req.ID, int(req.Power))
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("功率设置成功", c)
}

func ReportTag(c *gin.Context) {
	var req request.UHFReaderTagReport
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := uhf.UHFService.ReportTag(req.DeviceCode, req.Tag)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("标签上报成功", c)
}
