package controllers

import "sws_xcx/service"

type DeviceApiController struct {
	BaseApiAuthController
}

// @Title GetMyDevices
// @Description 获取绑定的设备列表
// @Success 200 {array} models.Device success
// @Failure 500 error
// @Security token
// @router /getmydevices [get]
func (c *DeviceApiController) GetMyDevices() {

	devices, err := service.NewDeviceService().GetMyDevices(c.CurrentUser.Id)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())

	}
	c.ServeSuccessJSON(devices)

}

// @Title GetDeviceDetail
// @Description 根据设备ID获取设备详细信息
// @Param   id path int true "设备ID"
// @Success 200 {object} models.Device success
// @Failure 500 error
// @Security token
// @router /getdevicedetail [get]
func (c *DeviceApiController) GetDeviceDetail() {

	id, err := c.GetInt("id")
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
	}
	device, err := service.NewDeviceService().GetDeviceInfo(id)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
	}
	c.ServeSuccessJSON(device)
}

// @Title BindDevice
// @Description 绑定设备
// @Param   deviceid path int true "设备ID"
// @Success 200  success
// @Failure 500 error
// @Security token
// @router /binddevice [post]
func (c *DeviceApiController) BindDevice() {
	id, err := c.GetInt("deviceid")
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
	}
	if err := service.NewDeviceService().BindDevice(c.CurrentUser.Id, uint64(id)); err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
	}
	c.ServeSuccessJSON(new(interface{}))
}

// @Title UnBindDevice
// @Description 解绑设备
// @Param   deviceid path int true "设备ID"
// @Success 200  success
// @Failure 500 error
// @Security token
// @router /unbinddevice [post]
func (c *DeviceApiController) UnbindDevice() {
	id, err := c.GetInt("deviceid")
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
	}
	if err := service.NewDeviceService().UnbindDevice(c.CurrentUser.Id, uint64(id)); err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
	}
	c.ServeSuccessJSON(new(interface{}))
}
