package protocol

import (
	json2 "encoding/json"
	"time"
)

// 设置集中器超时时间
type CHZB_Waittime struct {
	Waittime uint `json:"waittime"` //集中器超时时间,毫秒为单位
}

type Pack_CHZB_Waittime struct {
	Header
	Data CHZB_Waittime `json:"data"`
}

func (o *Pack_CHZB_Waittime) EnCode(id, gid string, seq uint64, wait uint) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.Waittime = wait
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_Waittime) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 手动开/关灯,没有PoleIDs则广播开关灯
type CHZB_Switch struct {
	LampIDs    []uint32 `json:"lampids,omitempty"` //需要控制的灯，长和zigbee末端编号，如果没有编号，则广播控制
	Switch     uint8    `json:"switch"`            //1开，0关
	Brightness uint8    `json:"brightness"`        //亮度0~100
	Recovery   uint     `json:"recovery"`          //恢复时控时间，单位秒
}

type Pack_CHZB_Switch struct {
	Header
	Data CHZB_Switch `json:"data"`
}

func (o *Pack_CHZB_Switch) EnCode(id, gid string, seq uint64, Switch, brightness uint8,
	recovery uint, lampIDs []uint32) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.LampIDs = lampIDs
	o.Data.Switch = Switch
	o.Data.Brightness = brightness
	o.Data.Recovery = recovery
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_Switch) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 手动恢复时控,没有PoleIDs则广播开关灯
type CHZB_LampComm struct {
	LampIDs []uint32 `json:"lampids,omitempty"` //需要控制的灯，长和zigbee末端编号，如果没有编号，则广播控制
}

type Pack_CHZB_RecoveryTime struct {
	Header
	Data CHZB_LampComm `json:"data"`
}

func (o *Pack_CHZB_RecoveryTime) EnCode(id, gid string, seq uint64, lampIDs []uint32) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.LampIDs = lampIDs
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_RecoveryTime) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 开关灯时间
type CHZB_OnOffTime struct {
	OnHour     uint8 `json:"onhour"`
	OnMinite   uint8 `json:"onminite"`
	OffHour    uint8 `json:"offhour"`
	OffMinite  uint8 `json:"offminite"`
	Brightness uint8 `json:"brightness"` //亮度0~100
}

func (o *CHZB_OnOffTime) SetData(onhour, onminite, offhour, offminite, brightness uint8) {
	o.OnHour = onhour
	o.OnMinite = onminite
	o.OffHour = offhour
	o.OffMinite = offminite
	o.Brightness = brightness
}

func (o *CHZB_OnOffTime) InTimeRange(t time.Time) bool {
	h := uint8(t.Hour())
	m := uint8(t.Minute())

	if o.OnHour > o.OffHour { //跨日情况,例如19:30~5:30，开灯时间大于关灯时间
		if h > o.OnHour || h < o.OffHour || (h == o.OnHour && m >= o.OnMinite) ||
			(h == o.OffHour && m <= o.OffMinite) {
			return true
		}
	} else if o.OnHour < o.OffHour { //开灯时间小于关灯时间
		if (h > o.OnHour && h < o.OffHour) ||
			(h == o.OnHour && m >= o.OnMinite) ||
			(h == o.OffHour && m <= o.OffMinite) {
			return true
		}
	} else { //开灯时间等于关灯时间
		if h == o.OnHour && m >= o.OnMinite && m <= o.OffMinite {
			return true
		}
	}
	return false
}

// 设置开关灯时间
type CHZB_SetOnOffTime struct {
	LampIDs   []uint32         `json:"lampids,omitempty"` //需要控制的灯，长和zigbee末端编号，如果没有编号，则广播控制
	OnOffTime []CHZB_OnOffTime `json:"onofftime"`         //开关灯时间
}
type Pack_SetOnOffTime struct {
	Header
	Data CHZB_SetOnOffTime `json:"data"`
}

func (o *Pack_SetOnOffTime) EnCode(id, gid string, seq uint64, lampIDs []uint32, onofftime []CHZB_OnOffTime) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.LampIDs = lampIDs
	o.Data.OnOffTime = onofftime
	return json.MarshalToString(o)
}
func (o *Pack_SetOnOffTime) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 设置集中器超时时间
type CHZB_QueryOnOffTime struct {
	Poleid uint32 `json:"poleid"` //单灯ID
}

type Pack_CHZB_QueryOnOffTime struct {
	Header
	Data CHZB_QueryOnOffTime `json:"data"`
}

func (o *Pack_CHZB_QueryOnOffTime) EnCode(id, gid string, seq uint64, poleid uint32) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.Poleid = poleid
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_QueryOnOffTime) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type CHZB_QueryOnOffTimeAck struct {
	StateError
	LampID    uint32           `json:"lampid"`              //单灯ID
	OnOffTime []CHZB_OnOffTime `json:"onofftime,omitempty"` //开关灯时间
}

type Pack_CHZB_QueryOnOffTimeAck struct {
	Header
	Data CHZB_QueryOnOffTimeAck `json:"data"`
}

func (o *Pack_CHZB_QueryOnOffTimeAck) EnCode(id, gid string, seq uint64, poleid uint32, err error, oot []CHZB_OnOffTime) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.StateError.SetStateErrorData(err)
	o.Data.LampID = poleid
	o.Data.OnOffTime = oot
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_QueryOnOffTimeAck) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type CHZB_Lamps struct {
	MapLamps map[uint32]string `json:"lamps,omitempty"` //编号到灯控did
}
type Pack_CHZB_LampIDs struct {
	Header
	Data CHZB_Lamps `json:"data"`
}

func (o *Pack_CHZB_LampIDs) EnCode(id, gid string, seq uint64, mapLamps map[uint32]string) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.MapLamps = mapLamps
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_LampIDs) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type CHZB_LampData struct {
	StateError
	Data map[uint16]float64 `json:"data,omitempty"`
}

type CHZB_UploadData struct {
	TID  uint16                    `json:"tid"`
	Data map[string]*CHZB_LampData `json:"data"`
}

type Pack_CHZB_UploadData struct {
	Header
	Data CHZB_UploadData `json:"data"`
}

func (o *Pack_CHZB_UploadData) EnCode(id, gid string, seq uint64, tid uint16, lampData map[string]*CHZB_LampData) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.TID = tid
	o.Data.Data = lampData
	marshal, err := json2.Marshal(o)
	return string(marshal), err
	//return json.MarshalToString(o)
}
func (o *Pack_CHZB_UploadData) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

type CHZB_SeqLampAck struct {
	MapLamp map[uint32]*StateError `json:"lamp,omitempty"`
}

type Pack_CHZB_SeqLampAck struct {
	Header
	Data CHZB_SeqLampAck `json:"data"`
}

func (o *Pack_CHZB_SeqLampAck) EnCode(id, gid string, seq uint64, maplamp map[uint32]*StateError) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.MapLamp = maplamp
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_SeqLampAck) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 手动恢复时控,没有PoleIDs则广播开关灯
type EmptyObject struct {
}

type Pack_CHZB_EmptyObject struct {
	Header
	Data EmptyObject `json:"data"`
}

func (o *Pack_CHZB_EmptyObject) EnCode(id, gid string, seq uint64) (string, error) {
	o.SetHeaderData(id, gid, seq)
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_EmptyObject) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 查询单灯当前时间
type CHZB_LampComm1 struct {
	LampID uint32 `json:"lampid"` //需要控制的灯，长和zigbee末端编号
}

type Pack_CHZB_QueryTime struct {
	Header
	Data CHZB_LampComm1 `json:"data"`
}

func (o *Pack_CHZB_QueryTime) EnCode(id, gid string, seq uint64, lampID uint32) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.LampID = lampID
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_QueryTime) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

// 开关灯时间
type CHZB_LampTime struct {
	Hour   uint8 `json:"hour"`
	Minite uint8 `json:"minite"`
	Second uint8 `json:"second"`
}
type CHZB_QueryTimeAck struct {
	StateError
	LampID   uint32         `json:"lampid"`             //单灯ID
	LampTime *CHZB_LampTime `json:"lamptime,omitempty"` //当前时间
}

type Pack_CHZB_QueryTimeAck struct {
	Header
	Data CHZB_QueryTimeAck `json:"data"`
}

func (o *Pack_CHZB_QueryTimeAck) EnCode(id, gid string, seq uint64, lampid uint32, err error, pt *CHZB_LampTime) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data.SetStateErrorData(err)
	o.Data.LampID = lampid
	o.Data.LampTime = pt
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_QueryTimeAck) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}

const (
	LE_UNKNOWN       = 0    //未知，初始值
	LE_OK            = 1000 //正常（正常开灯或熄灯状态）
	LE_ON            = 1001 //亮灯异常（本该关灯状态）
	LE_ON_BRIGHTNESS = 1002 //亮灯，但亮度异常（本该开灯状态，但开灯亮度不对）
	LE_OFF           = 1003 //熄灯异常（本该开灯，但熄灯了）
)

type LampAlarm struct {
	DID             string `json:"did"`             //灯控编码
	AlarmType       uint16 `json:"type"`            //告警类型
	StartTime       string `json:"tstart"`          //开始时间
	EndTime         string `json:"tend,omitempty"`  //结束时间
	AlarmBrightness uint8  `json:"alarmbrightness"` //告警亮度
	Brightness      uint8  `json:"brightness"`      //恢复亮度
}
type Pack_CHZB_LampAlarm struct {
	Header
	Data *LampAlarm `json:"data"`
}

func (o *Pack_CHZB_LampAlarm) EnCode(id, gid string, seq uint64, a *LampAlarm) (string, error) {
	o.SetHeaderData(id, gid, seq)
	o.Data = a
	return json.MarshalToString(o)
}

func (o *Pack_CHZB_LampAlarm) DeCode(message string) error {
	return json.UnmarshalFromString(message, o)
}
