package Adl200Impl

import (
	"encoding/binary"
	"tgk-touch/internal/library/ADL200"
	"time"
)

// ===== 时间写入 =====

// WriteYear 写入电表年份
func (a *Adl200Impl) WriteYear(commUid string, addr byte, year int) error {
	// 读取当前年月
	result, err := ADL200.H0012.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := result[0]
	// 高字节=年，低字节=月
	month := byte(value)
	data := uint16(year)<<8 | uint16(month)
	return ADL200.H0012.Write(commUid, addr, []uint16{data})
}

// WriteMonth 写入电表月份
func (a *Adl200Impl) WriteMonth(commUid string, addr byte, month int) error {
	// 读取当前年月
	result, err := ADL200.H0012.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := result[0]
	// 高字节=年，低字节=月
	year := byte(value >> 8)
	data := uint16(year)<<8 | uint16(month)
	return ADL200.H0012.Write(commUid, addr, []uint16{data})
}

// WriteDay 写入电表日期
func (a *Adl200Impl) WriteDay(commUid string, addr byte, day int) error {
	// 读取当前日时
	read, err := ADL200.H0013.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	// 高字节=时，低字节=日
	hour := byte(value >> 8)
	data := make([]byte, 2)
	data[0] = hour
	data[1] = byte(day)
	writeData := binary.BigEndian.Uint16(data)
	return ADL200.H0013.Write(commUid, addr, []uint16{writeData})
}

// WriteHour 写入电表小时
func (a *Adl200Impl) WriteHour(commUid string, addr byte, hour int) error {
	// 读取当前日时
	read, err := ADL200.H0013.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	// 高字节=时，低字节=日
	day := byte(value)
	data := make([]byte, 2)
	data[0] = byte(hour)
	data[1] = day
	writeData := binary.BigEndian.Uint16(data)
	return ADL200.H0013.Write(commUid, addr, []uint16{writeData})
}

// WriteMinute 写入电表分钟
func (a *Adl200Impl) WriteMinute(commUid string, addr byte, minute int) error {
	// 读取当前分秒
	read, err := ADL200.H0014.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	// 高字节=秒，低字节=分
	second := byte(value >> 8)
	data := make([]byte, 2)
	data[0] = second
	data[1] = byte(minute)
	writeData := binary.BigEndian.Uint16(data)
	return ADL200.H0014.Write(commUid, addr, []uint16{writeData})
}

// WriteSecond 写入电表秒数
func (a *Adl200Impl) WriteSecond(commUid string, addr byte, second int) error {
	// 读取当前分秒
	read, err := ADL200.H0014.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	// 高字节=秒，低字节=分
	minute := byte(value)
	data := make([]byte, 2)
	data[0] = byte(second)
	data[1] = minute
	writeData := binary.BigEndian.Uint16(data)
	return ADL200.H0014.Write(commUid, addr, []uint16{writeData})
}

// WriteTime 写入电表完整时间
func (a *Adl200Impl) WriteTime(commUid string, addr byte, t time.Time) error {
	err := a.WriteYear(commUid, addr, t.Year()-2000)
	if err != nil {
		return err
	}
	err = a.WriteMonth(commUid, addr, int(t.Month()))
	if err != nil {
		return err
	}
	err = a.WriteDay(commUid, addr, t.Day())
	if err != nil {
		return err
	}
	err = a.WriteHour(commUid, addr, t.Hour())
	if err != nil {
		return err
	}
	err = a.WriteMinute(commUid, addr, t.Minute())
	if err != nil {
		return err
	}
	err = a.WriteSecond(commUid, addr, t.Second())
	if err != nil {
		return err
	}
	return nil
}

// ===== 通信地址写入 =====

// WriteMsgAddr 写入通信地址
func (a *Adl200Impl) WriteMsgAddr(commUid string, addr byte, newAddr byte) error {
	// 读取当前值 (高8位=地址，低8位=波特率)
	read, err := ADL200.H0015.Read(commUid, addr)
	if err != nil {
		return err
	}
	currentValue := read[0]
	// 低8位保留波特率设置
	currentBaud := byte(currentValue)
	// 高8位=新地址，低8位=原波特率
	newValue := uint16(newAddr)<<8 | uint16(currentBaud)
	return ADL200.H0015.Write(commUid, addr, []uint16{newValue})
}

// ===== 额外写入方法 =====

// WriteBacklightTime 写入背光时间
// minute: 0-255分钟，0为常亮
func (a *Adl200Impl) WriteBacklightTime(commUid string, addr byte, minute byte) error {
	data := []uint16{uint16(minute)}
	return ADL200.H0016.Write(commUid, addr, data)
}

// WriteStatus 写入状态配置
// bit0: 费率类型 0-非复费率, 1-复费率
// bit1: 接线方向 0-上进下出, 1-下进上出
// bit3: 视在功率 0-PQS, 1-RMS
func (a *Adl200Impl) WriteStatus(commUid string, addr byte, status byte) error {
	return ADL200.H0045.Write(commUid, addr, []uint16{uint16(status)})
}
