package zigbee

import (
	"github.com/valyala/bytebufferpool"
)

var (
	CmdReadDldy                   uint8 = 0x01
	CmdReadBrightness             uint8 = 0x02
	CmdSetAuto                    uint8 = 0x08
	CmdSetBrightness              uint8 = 0x12
	CmdReadGroup                  uint8 = 0x05
	CmdSetGroup                   uint8 = 0x15
	CmdSetEnabled                 uint8 = 0x16
	CmdSetCorrectiontime          uint8 = 0x23
	CmdSetOnofftime               uint8 = 0x26
	CmdReadOnofftime              uint8 = 0x27
	CmdSetOnofftime2              uint8 = 0x28
	CmdReadOnofftime2             uint8 = 0x29
	CmdReadTime                   uint8 = 0x30
	CmdSetBroadcastCorrectiontime uint8 = 0x70
	CmdReadOnofftime12            uint8 = 0x2F
	CmdSetBroadcastOn             uint8 = 0x84
	CmdSetBroadcastOff            uint8 = 0x85
	CmdSetBroadcastAuto           uint8 = 0x86
)

// PackGeneralFuncCommand /////////////////////////////////////////////////////////////////////////////////////////////
//公共方法
///////////////////////////////////////////////////////////////////////////////////////////////
type PackGeneralFuncCommand struct {
	Cmd    uint8
	Seq    uint8
	PoleID uint32
	Data   []byte
}

func (o *PackGeneralFuncCommand) SetData(PoleID uint32, Cmd, Seq uint8, Data []byte) {
	o.Cmd = Cmd
	o.PoleID = PoleID
	o.Data = Data
	o.Seq = Seq
}
func (o *PackGeneralFuncCommand) EnCode() (*bytebufferpool.ByteBuffer, error) {
	var psd PackSendData
	var gf GeneralFunc
	psd.Body1.Data.SetDody(&gf)
	psd.Body1.Data.SetData(o.PoleID, o.Seq)
	psd.Body1.Data.Body2.SetData(0, 0, uint16(o.Cmd), o.Data)
	return psd.EnCode()
}
func (o *PackGeneralFuncCommand) DeCode(data []byte) error {
	var psd PackSendData
	var gf GeneralFunc
	psd.Body1.Data.SetDody(&gf)
	err := psd.DeCode(data)
	if err == nil {
		o.Cmd = gf.Cmd
		o.Seq = psd.Body1.Data.Seq
		o.PoleID = psd.Body1.Data.PoleID
		o.Data = make([]byte, len(gf.Data))
		copy(o.Data, gf.Data)
	}
	return err
}

type PackUpgradeFuncCommand struct {
	Cmd    uint8
	Seq    uint8
	PoleID uint32
	Data   []byte
}

func (o *PackUpgradeFuncCommand) SetData(PoleID uint32, Cmd, Seq uint8, Data []byte) {
	o.Cmd = Cmd
	o.PoleID = PoleID
	o.Data = Data
	o.Seq = Seq
}
func (o *PackUpgradeFuncCommand) EnCode() (*bytebufferpool.ByteBuffer, error) {
	var psd PackSendData
	var up UpgradeFunc
	psd.Body1.Data.SetDody(&up)
	psd.Body1.Data.SetData(o.PoleID, o.Seq)
	psd.Body1.Data.Body2.SetData(0, 0, uint16(o.Cmd), o.Data)
	return psd.EnCode()
}
func (o *PackUpgradeFuncCommand) DeCode(data []byte) error {
	var psd PackSendData
	var uf UpgradeFunc
	psd.Body1.Data.SetDody(&uf)
	err := psd.DeCode(data)
	if err == nil {
		o.Cmd = uf.Cmd
		o.Seq = psd.Body1.Data.Seq
		o.PoleID = psd.Body1.Data.PoleID
		o.Data = make([]byte, len(uf.Data))
		copy(o.Data, uf.Data)
	}
	return err
}
