package Adl400Impl

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

func (a *Adl400Impl) WriteYear(commUid string, addr byte, year int) error {
	// 1. 先读取当前月份（假设月份在低字节，年份在高字节）
	result, err := ADL400.H003E.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := result[0] // 低字节为月份
	bytes := []byte{byte(value >> 8), byte(value)}
	month, yearRead := int(bytes[0]), int(bytes[1])
	yearRead = year
	data := uint16(month)<<8 | uint16(yearRead)
	// 3. 写入合并后的数据
	return ADL400.H003E.Write(commUid, addr, []uint16{data})
}
func (a *Adl400Impl) WriteMonth(commUid string, addr byte, month int) error {
	// 1. 先读取当前月份（假设月份在低字节，年份在高字节）
	result, err := ADL400.H003E.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := result[0] // 低字节为月份
	bytes := []byte{byte(value >> 8), byte(value)}
	monthRead, year := int(bytes[0]), int(bytes[1])
	monthRead = month
	data := uint16(monthRead)<<8 | uint16(year)
	// 3. 写入合并后的数据
	return ADL400.H003E.Write(commUid, addr, []uint16{data})
}
func (a *Adl400Impl) WriteDay(commUid string, addr byte, day int) error {
	read, err := ADL400.H003D.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	hour := int(value >> 8) // 高字节=小时（9）
	data := make([]byte, 2)
	data[0] = byte(hour) // 高字节 = 时
	data[1] = byte(day)  // 低字节 = 日
	writeData := binary.BigEndian.Uint16(data)
	err = ADL400.H003D.Write(commUid, addr, []uint16{writeData})
	if err != nil {
		return err
	}
	return nil
}
func (a *Adl400Impl) WriteHour(commUid string, addr byte, hour int) error {
	read, err := ADL400.H003D.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	day := value & 0xFF // 高字节=小时（9）
	data := make([]byte, 2)
	data[0] = byte(hour) // 高字节 = 时
	data[1] = byte(day)  // 低字节 = 日
	writeData := binary.BigEndian.Uint16(data)
	err = ADL400.H003D.Write(commUid, addr, []uint16{writeData})
	if err != nil {
		return err
	}
	return nil
}
func (a *Adl400Impl) WriteMinute(commUid string, addr byte, minute int) error {
	read, err := ADL400.H003C.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	//log.Printf("value = %04X", value)

	second := value >> 8 // 高字节=小时（9）
	//minuteRead := value & 0xFF // 低字节=日（27）
	data := make([]byte, 2)
	data[0] = byte(second) // 高字节 = 时
	data[1] = byte(minute) // 低字节 = 日
	writeData := binary.BigEndian.Uint16(data)
	//log.Printf("writeData = %04X", writeData)
	return ADL400.H003C.Write(commUid, addr, []uint16{writeData})

}
func (a *Adl400Impl) WriteSecond(commUid string, addr byte, second int) error {
	read, err := ADL400.H003C.Read(commUid, addr)
	if err != nil {
		return err
	}
	value := read[0]
	//second := value >> 8  // 高字节=小时（9）
	minute := value & 0xFF // 低字节=日（27）
	data := make([]byte, 2)
	data[0] = byte(second) // 高字节 = 时
	data[1] = byte(minute) // 低字节 = 日
	writeData := binary.BigEndian.Uint16(data)
	err = ADL400.H003C.Write(commUid, addr, []uint16{writeData})
	if err != nil {
		return err
	}
	return nil
}
func (a *Adl400Impl) 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.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
}

func (a *Adl400Impl) WriteMsgAddr(commUid string, addr byte, newAddr byte) error {

	// 1. 先读取当前003FH寄存器的完整值
	read, err := ADL400.H003F.Read(commUid, addr)
	if err != nil {
		return err
	}

	// 2. 解析当前值（假设返回uint16）
	currentValue := read[0]
	currentBaud := byte(currentValue & 0xFF) // 低字节=波特率

	// 3. 构建新值：新地址(高8位) + 原波特率(低8位)
	newValue := uint16(newAddr)<<8 | uint16(currentBaud)

	// 4. 写入新值
	writeData := []uint16{newValue}
	return ADL400.H003F.Write(commUid, addr, writeData)

}
