package controllers

import (
	"fmt"
	"strings"

	"github.com/astaxie/beego"

	"lc/common/models"
)

type GatewayController struct {
	BaseController
}

type ReqGateway struct {
	Code   string `json:"code"`   //设备编号，禁止修改
	Tenant string `json:"tenant"` //租户ID
	Name   string `json:"name"`   //设备名称
	Brand  int    `json:"brand"`  //品牌
	Model  int    `json:"model"`  //型号
	State  int    `json:"state"`  //1启用，0禁用
}

type Gateway struct {
	Code  string `json:"code"`  //设备编号，禁止修改
	Name  string `json:"name"`  //设备名称
	Brand int    `json:"brand"` //品牌
	Model int    `json:"model"` //型号
	State int    `json:"state"` //1启用，0禁用
}
type ReqImportGateway struct {
	Tenant string    `json:"tenant"` //租户ID
	List   []Gateway `json:"list"`   //网关列表
}
type RespImport struct {
	Code  string `json:"code"`
	Error string `json:"error"`
}

// CreateGateway @Title 创建网关设备
// @Description 创建网关设备
// @Param   body controllers.ReqGateway true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/create [post]
func (o *GatewayController) CreateGateway() {
	var obj ReqGateway
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
		beego.Debug(string(o.Ctx.Input.RequestBody))
		o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
		return
	}
	oo := models.Gateway{
		ID:     obj.Code,
		Name:   obj.Name,
		Tenant: obj.Tenant,
		Brand:  obj.Brand,
		Model:  obj.Model,
		State:  obj.State,
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// UpdateGateway @Title 更新网关设备
// @Description 更新网关设备
// @Param   body controllers.ReqGateway true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/update [post]
func (o *GatewayController) UpdateGateway() {
	var obj ReqGateway
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
		beego.Debug(string(o.Ctx.Input.RequestBody))
		o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
		return
	}
	oo := models.Gateway{
		ID:     obj.Code,
		Name:   obj.Name,
		Tenant: obj.Tenant,
		Brand:  obj.Brand,
		Model:  obj.Model,
		State:  obj.State,
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据更新失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// DeleteGateway @Title 删除网关设备
// @Description 删除网关设备
// @Param   code    query   string     true "设备ID"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/delete [post]
func (o *GatewayController) DeleteGateway() {
	code := strings.Trim(o.GetString("code"), " ")
	if len(code) == 0 {
		o.Response(Failure, "code为空", nil)
		return
	}
	c := models.Gateway{
		ID: code,
	}
	if err := c.Delete(); err != nil {
		beego.Error(fmt.Sprintf("删除失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("数据删除失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", c.ID)
}

// ImportGateway @Title 批量导入网关设备
// @Description 批量导入网关设备
// @Param   body controllers.ReqImportGateway true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/import [post]
func (o *GatewayController) ImportGateway() {
	var obj ReqImportGateway
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
		beego.Debug(string(o.Ctx.Input.RequestBody))
		o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
		return
	}
	var resp []RespImport
	for _, v := range obj.List {
		var aresp RespImport
		aresp.Code = v.Code
		oo := models.Gateway{
			ID:     v.Code,
			Name:   v.Name,
			Tenant: obj.Tenant,
			Brand:  v.Brand,
			Model:  v.Model,
			State:  v.State,
		}
		err := oo.SaveFromWeb()
		if err != nil {
			aresp.Error = err.Error()
			beego.Error(fmt.Sprintf("zigbee集控器数据导入失败,code=%s,失败原因:%s", v.Code, err.Error()))
		}
		resp = append(resp, aresp)
	}
	o.Response(Success, "成功", resp)
}
