package ADL400

import "fmt"

var (
	H0000 = MeterModusCmd{startAddr: 0x00, quantity: 2}
	H0002 = MeterModusCmd{startAddr: 0x02, quantity: 2}
	H0004 = MeterModusCmd{startAddr: 0x04, quantity: 2}
	H0006 = MeterModusCmd{startAddr: 0x06, quantity: 2}
	H0008 = MeterModusCmd{startAddr: 0x08, quantity: 2}
	H000A = MeterModusCmd{startAddr: 0x0A, quantity: 2, detail: "当前正向总有功电能"}
	H000C = MeterModusCmd{startAddr: 0x0C, quantity: 2}
	H000E = MeterModusCmd{startAddr: 0x0E, quantity: 2}
	H0010 = MeterModusCmd{startAddr: 0x10, quantity: 2}
	H0012 = MeterModusCmd{startAddr: 0x12, quantity: 2}
	H0014 = MeterModusCmd{startAddr: 0x14, quantity: 2}
	H0016 = MeterModusCmd{startAddr: 0x16, quantity: 2}
	H0018 = MeterModusCmd{startAddr: 0x18, quantity: 2}
	H001A = MeterModusCmd{startAddr: 0x1A, quantity: 2}
	H001C = MeterModusCmd{startAddr: 0x1C, quantity: 2}
	H001E = MeterModusCmd{startAddr: 0x1E, quantity: 2}
	H0020 = MeterModusCmd{startAddr: 0x20, quantity: 2}
	H0022 = MeterModusCmd{startAddr: 0x22, quantity: 2}
	H0024 = MeterModusCmd{startAddr: 0x24, quantity: 2}
	H0026 = MeterModusCmd{startAddr: 0x26, quantity: 2}
	H0028 = MeterModusCmd{startAddr: 0x28, quantity: 2}
	H002A = MeterModusCmd{startAddr: 0x2A, quantity: 2}
	H002C = MeterModusCmd{startAddr: 0x2C, quantity: 2}
	H002E = MeterModusCmd{startAddr: 0x2E, quantity: 2}
	H0030 = MeterModusCmd{startAddr: 0x30, quantity: 2}
	H0032 = MeterModusCmd{startAddr: 0x32, quantity: 2}
	H0034 = MeterModusCmd{startAddr: 0x34, quantity: 2}
	H0036 = MeterModusCmd{startAddr: 0x36, quantity: 2}
	H0038 = MeterModusCmd{startAddr: 0x38, quantity: 2}
	H003A = MeterModusCmd{startAddr: 0x3A, quantity: 2}

	H003C = MeterModusCmd{startAddr: 0x3C, quantity: 1, cmdType: 1, detail: "秒、分"}
	H003D = MeterModusCmd{startAddr: 0x3D, quantity: 1, cmdType: 1, detail: "时、日"}
	H003E = MeterModusCmd{startAddr: 0x3E, quantity: 1, cmdType: 1, detail: "月、年"}
	H003F = MeterModusCmd{startAddr: 0x3F, quantity: 1, cmdType: 1, detail: "通信地址"}

	H0040 = MeterModusCmd{startAddr: 0x40, quantity: 1}

	H0041 = MeterModusCmd{startAddr: 0x41, quantity: 1, cmdType: 1}
	H0042 = MeterModusCmd{startAddr: 0x42, quantity: 1, cmdType: 1}
	H0043 = MeterModusCmd{startAddr: 0x43, quantity: 1, cmdType: 1}
	H0044 = MeterModusCmd{startAddr: 0x44, quantity: 1, cmdType: 1}
	H0045 = MeterModusCmd{startAddr: 0x45, quantity: 1, cmdType: 1}
	H0046 = MeterModusCmd{startAddr: 0x46, quantity: 1, cmdType: 1}
	H0061 = MeterModusCmd{startAddr: 0x61, quantity: 1, detail: "A相电压"}
	H0062 = MeterModusCmd{startAddr: 0x62, quantity: 1, detail: "B相电压"}
	H0063 = MeterModusCmd{startAddr: 0x63, quantity: 1, detail: "C相电压"}
	H0064 = MeterModusCmd{startAddr: 0x64, quantity: 1, detail: "A相电流"}
	H0065 = MeterModusCmd{startAddr: 0x65, quantity: 1, detail: "B相电流"}
	H0066 = MeterModusCmd{startAddr: 0x66, quantity: 1, detail: "C相电流"}
	H0067 = MeterModusCmd{startAddr: 0x67, quantity: 1, detail: "A相有功功率"}
	H0068 = MeterModusCmd{startAddr: 0x68, quantity: 1, detail: "B相有功功率"}
	H0069 = MeterModusCmd{startAddr: 0x69, quantity: 1, detail: "C相有功功率"}
	H006A = MeterModusCmd{startAddr: 0x6A, quantity: 1, detail: "总有功功率"}
	H0087 = MeterModusCmd{startAddr: 0x87, quantity: 2, detail: "A相正向有功电能"}
	H0089 = MeterModusCmd{startAddr: 0x89, quantity: 2, detail: "B相正向有功电能"}
	H008B = MeterModusCmd{startAddr: 0x8B, quantity: 2, detail: "C相正向有功电能"}
	H0073 = MeterModusCmd{startAddr: 0x73, quantity: 2, detail: "A相功率因数"}
	H0074 = MeterModusCmd{startAddr: 0x74, quantity: 2, detail: "B相功率因数"}
	H0075 = MeterModusCmd{startAddr: 0x75, quantity: 2, detail: "C相功率因数"}
	H0076 = MeterModusCmd{startAddr: 0x76, quantity: 2, detail: "总功率因数"}
	H0121 = MeterModusCmd{startAddr: 0x0121, quantity: 1, cmdType: 1, detail: "日冻结时间 时"}
	H0122 = MeterModusCmd{startAddr: 0x0121, quantity: 1, cmdType: 1, detail: "月冻结 日时"}
	H6000 = MeterModusCmd{startAddr: 0x6000, quantity: 34}
	H6001 = MeterModusCmd{startAddr: 0x6001, quantity: 1}
	H6022 = MeterModusCmd{startAddr: 0x6022, quantity: 34}
)

type MeterModusCmd struct {
	startAddr uint16
	quantity  uint16
	cmdType   int // 0 R , 1 R/W
	detail    string
}

func (mcc *MeterModusCmd) SetStartAddr(s uint16) {
	mcc.startAddr = s
}
func (mcc *MeterModusCmd) GetStartAddr() uint16 {
	return mcc.startAddr
}
func (mcc *MeterModusCmd) SetQuantity(q uint16) {
	mcc.quantity = q
}
func (mcc *MeterModusCmd) GetQuantity() uint16 {
	return mcc.quantity
}
func (mcc *MeterModusCmd) SetCmdType(t int) {
	mcc.cmdType = t
}
func (mcc *MeterModusCmd) GetCmdType() int {
	return mcc.cmdType
}

func (mmc *MeterModusCmd) Read(commUid string, addr byte) ([]uint16, error) {

	return readCmd(commUid, addr, mmc.startAddr, mmc.quantity, "读取"+mmc.detail)

}
func (mmc *MeterModusCmd) Write(commUid string, addr byte, data []uint16) error {
	if mmc.cmdType == 0 {
		return nil
	}
	return writeCmd(commUid, addr, mmc.startAddr, data, "写入"+mmc.detail)
}

// 获取日冻结数据块地址
func GetDailyBlockAddress(day int) (*MeterModusCmd, error) {
	if day < 1 || day > 90 {
		return nil, fmt.Errorf("invalid day value: %d (valid range 1-90)", day)
	}
	MMC := &MeterModusCmd{startAddr: 0x6000 + uint16(34*(day-1)), quantity: 34}
	return MMC, nil
}

// 获取月冻结数据块地址
func GetMonthlyBlockAddress(month int) (*MeterModusCmd, error) {
	if month < 1 || month > 48 {
		return nil, fmt.Errorf("invalid month value: %d (valid range 1-48)", month)
	}
	MMC := &MeterModusCmd{startAddr: 0x7000 + uint16(34*(month-1)), quantity: 34}

	return MMC, nil
}
