package bx

import (
	"bytes"
	"encoding/binary"
)

//定时开关机

type CmdTimingSwitch struct {
	baseBxCmd
	onOffSet [][2]uint64
}

func NewCmdTimingSwitch(onOffSet [][2]uint64) CmdTimingSwitch {
	return CmdTimingSwitch{
		baseBxCmd: newBaseCmd(CMD_TIMING_SWITCH.group, CMD_TIMING_SWITCH.code),
		onOffSet:  onOffSet,
	}
}

func (cmd CmdTimingSwitch) Build() []byte {
	w := bytes.NewBuffer(make([]byte, 0, 1024))
	binary.Write(w, binary.LittleEndian, cmd.Group())
	binary.Write(w, binary.LittleEndian, cmd.Cmd())
	binary.Write(w, binary.LittleEndian, cmd.ReqResp())
	//r0 r1
	binary.Write(w, binary.LittleEndian, byte(0x00))
	binary.Write(w, binary.LittleEndian, byte(0x00))
	if len(cmd.onOffSet) == 0 || len(cmd.onOffSet) > 256 {
		return nil
	}
	binary.Write(w, binary.LittleEndian, byte(len(cmd.onOffSet)))
	for i, v := range cmd.onOffSet {
		if i > 2 {
			break
		}
		binary.Write(w, binary.BigEndian, Uint2BCD(uint64(v[0]), true)) //开机
		binary.Write(w, binary.BigEndian, Uint2BCD(uint64(v[1]), true)) //关机
	}
	return w.Bytes()
}
