package bx

import (
	"bytes"
	"encoding/binary"
)

type CmdLock struct {
	baseBxCmd
	StoreMode       byte
	LockFlag        byte
	ProgramFileName string
}

// NewCmdLock 锁定状态:0x00——解锁状态,0x01——锁定状态
func NewCmdLock(flag byte, name string) CmdLock {
	return CmdLock{
		baseBxCmd:       newBaseCmd(CMD_LOCK_UNLOCK.group, CMD_LOCK_UNLOCK.code),
		LockFlag:        flag,
		ProgramFileName: name,
	}
}

// SetStoreMode 锁定状态保存方式：0x00——掉电不保存,0x01——掉电保存
func (cmd *CmdLock) SetStoreMode(storeMode byte) {
	cmd.StoreMode = storeMode
}

func (cmd *CmdLock) Build() []byte {
	w := bytes.NewBuffer(make([]byte, 0, 32))
	binary.Write(w, binary.LittleEndian, cmd.Group())
	binary.Write(w, binary.LittleEndian, cmd.Cmd())
	binary.Write(w, binary.LittleEndian, byte(0x01))
	binary.Write(w, binary.LittleEndian, []byte{0x00, 0x00})
	binary.Write(w, binary.LittleEndian, cmd.StoreMode)
	binary.Write(w, binary.LittleEndian, cmd.LockFlag)
	binary.Write(w, binary.BigEndian, []byte(cmd.ProgramFileName))
	return w.Bytes()
}
