package ym485

import "github.com/valyala/bytebufferpool"

// PackingTurnOnOff 1E，单灯控制器按路遥控开关灯
//1E，单灯控制器按组遥控开关灯
type PackingTurnOnOff struct {
	Dp       DataPack
	Mark     [4]byte
	Senabled byte //各组是否有效
	Kenabled byte //各组开灯/关灯
}

func (o *PackingTurnOnOff) EnCode(Addr string, Mark [4]byte, Senabled, Kenabled byte) (*bytebufferpool.ByteBuffer, error) {
	copy(o.Mark[0:], Mark[0:])
	o.Senabled = Senabled
	o.Kenabled = Kenabled

	dataBuf := make([]byte, 0, 6)
	dataBuf = append(dataBuf, o.Mark[0:]...)
	dataBuf = append(dataBuf, o.Senabled, o.Kenabled)

	return o.Dp.EnCode(Addr, 0x1E, dataBuf)
}

func (o *PackingTurnOnOff) DeCode(data []byte) error {
	if err := o.Dp.DeCode(data); err == nil {
		len0 := len(o.Dp.Data)
		if len0 >= 6 {
			copy(o.Mark[0:], o.Dp.Data[0:4])
			o.Senabled = o.Dp.Data[4]
			o.Kenabled = o.Dp.Data[5]
		}
		return nil
	} else {
		return err
	}
}

// PackingBrightnessColor 1E，单灯控制器按路遥控调光/色温
//1E，单灯控制器按组遥控调光/色温
type PackingBrightnessColor struct {
	Dp       DataPack
	Mark     [4]byte
	Senabled byte   //各路/组是否有效
	Sn       []byte //路n调光值/色温值
}

func (o *PackingBrightnessColor) EnCode(Addr string, Mark [4]byte, Senabled byte, Sn []byte) (*bytebufferpool.ByteBuffer, error) {
	copy(o.Mark[0:], Mark[0:])
	o.Senabled = Senabled
	o.Sn = Sn
	dataBuf := make([]byte, 0, 5+len(o.Sn))
	dataBuf = append(dataBuf, o.Mark[0:]...)
	dataBuf = append(dataBuf, o.Senabled)
	dataBuf = append(dataBuf, o.Sn...)
	return o.Dp.EnCode(Addr, 0x1E, dataBuf)
}

func (o *PackingBrightnessColor) DeCode(data []byte) error {
	if err := o.Dp.DeCode(data); err == nil {
		len0 := len(o.Dp.Data)
		if len0 >= 5 {
			copy(o.Mark[0:], o.Dp.Data[0:4])
			o.Senabled = o.Dp.Data[4]
			if len0 > 5 {
				copy(o.Sn, o.Dp.Data[5:])
			}
		}
		return nil
	} else {
		return err
	}
}
