package controllers

import (
	"fmt"
	"strings"

	"github.com/astaxie/beego"
	validation "github.com/go-ozzo/ozzo-validation/v4"

	"lc/common/models"
)

// ConcentratorController 长和zigbee集控器
type ConcentratorController struct {
	BaseController
}

type ReqConcentrator struct {
	Code    string `json:"code"`    //设备编号，禁止修改
	GID     string `json:"gid"`     //网关ID
	Tenant  string `json:"tenant"`  //租户ID
	Name    string `json:"name"`    //设备名称
	Brand   int    `json:"brand"`   //品牌
	Model   int    `json:"model"`   //型号
	NetID   int    `json:"netid"`   //网络号，zigbee灯控专用，其他类型灯控置0
	Channel int    `json:"channel"` //通道号，zigbee灯控专用，其他类型灯控置0
	State   int    `json:"state"`   //1启用，0禁用
}

func (a ReqConcentrator) Validate() error {
	return validation.ValidateStruct(&a,
		validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.Tenant, validation.Required, validation.Length(4, 8)),
		validation.Field(&a.NetID, validation.Required, validation.Min(1)),
		validation.Field(&a.Channel, validation.Required, validation.Min(1)),
		validation.Field(&a.State, validation.In(0, 1)),
	)
}

type Concentrator struct {
	Code    string `json:"code"`    //设备编号，禁止修改
	GID     string `json:"gid"`     //网关ID
	Name    string `json:"name"`    //设备名称
	Brand   int    `json:"brand"`   //品牌
	Model   int    `json:"model"`   //型号
	NetID   int    `json:"netid"`   //网络号，zigbee灯控专用，其他类型灯控置0
	Channel int    `json:"channel"` //通道号，zigbee灯控专用，其他类型灯控置0
	State   int    `json:"state"`   //1启用，0禁用
}

func (a Concentrator) Validate() error {
	return validation.ValidateStruct(&a,
		validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.NetID, validation.Required, validation.Min(1)),
		validation.Field(&a.Channel, validation.Required, validation.Min(1)),
		validation.Field(&a.State, validation.In(0, 1)),
	)
}

type ReqImportConcentrator struct {
	Tenant string         `json:"tenant"` //租户ID
	List   []Concentrator `json:"list"`   //灯控列表
}

func (a ReqImportConcentrator) Validate() error {
	return validation.ValidateStruct(&a,
		validation.Field(&a.Tenant, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.List, validation.Required),
	)
}

// CreateConcentrator @Title 创建长和zigbee集控器
// @Description 创建长和zigbee集控器
// @Param    body controllers.ReqConcentrator true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/create [post]
func (o *ConcentratorController) CreateConcentrator() {
	var obj ReqConcentrator
	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
	}
	if err := obj.Validate(); err != nil {
		beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
		return
	}
	oo := models.DeviceConcentrator{
		ID:      obj.Code,    //设备ID，DID
		Name:    obj.Name,    //集控器名称
		GID:     obj.GID,     //网关ID
		Brand:   obj.Brand,   //品牌
		Model:   obj.Model,   //型号
		Netid:   obj.NetID,   //网络号，zigbee灯控专用，其他类型灯控置0
		Channel: obj.Channel, //通道号，zigbee灯控专用，其他类型灯控置0
		Tenant:  obj.Tenant,  //租户ID
		State:   obj.State,   //1启用，0禁用
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// UpdateConcentrator @Title 更新长和zigbee集控器
// @Description 更新长和zigbee集控器
// @Param   body body controllers.ReqConcentrator true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/update [post]
func (o *ConcentratorController) UpdateConcentrator() {
	var obj ReqConcentrator
	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
	}
	if err := obj.Validate(); err != nil {
		beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
		return
	}
	oo := models.DeviceConcentrator{
		ID:      obj.Code,    //设备ID，DID
		Name:    obj.Name,    //灯控名称
		GID:     obj.GID,     //网关ID
		Brand:   obj.Brand,   //品牌
		Model:   obj.Model,   //型号
		Netid:   obj.NetID,   //网络号，zigbee灯控专用，其他类型灯控置0
		Channel: obj.Channel, //通道号，zigbee灯控专用，其他类型灯控置0
		Tenant:  obj.Tenant,  //租户ID
		State:   obj.State,   //1启用，0禁用
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据更新失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// DeleteConcentrator @Title 删除长和zigbee集控器
// @Description 删除长和zigbee集控器
// @Param   code    query   string     true "设备ID"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/delete [post]
func (o *ConcentratorController) DeleteConcentrator() {
	code := strings.Trim(o.GetString("code"), " ")
	if err := validation.Validate(code, validation.Required, validation.Length(4, 100)); err != nil {
		beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
		return
	}
	c := models.DeviceConcentrator{
		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)
}

// ImportConcentrator @Title 导入长和zigbee集控器
// @Description 导入长和zigbee集控器
// @Param    body controllers.ReqImportConcentrator true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/import [post]
func (o *ConcentratorController) ImportConcentrator() {
	var obj ReqImportConcentrator
	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
	}
	if err := obj.Validate(); err != nil {
		beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
		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.DeviceConcentrator{
			ID:      v.Code,     //设备ID，DID
			Name:    v.Name,     //集控器名称
			GID:     v.GID,      //网关ID
			Brand:   v.Brand,    //品牌
			Model:   v.Model,    //型号
			Netid:   v.NetID,    //网络号，zigbee灯控专用，其他类型灯控置0
			Channel: v.Channel,  //通道号，zigbee灯控专用，其他类型灯控置0
			Tenant:  obj.Tenant, //租户ID
			State:   v.State,    //1启用，0禁用
		}
		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)
}
