package modbus

import (
	"golib/v2/gnet"
)

func parseBit(order gnet.BinaryOrder, reg Register, v []byte, valueMap map[string]interface{}) {
	spit := order.BitSplit(v)
	for i, enum := range reg.Enum {
		switch reg.Type {
		case TypeUInt:
			check := uint8(0)
			if spit.Is1(uint64(i)) {
				check = 1
			}
			valueMap[enum.Key] = check
		case TypeBool:
			check := false
			if spit.Is1(uint64(i)) {
				check = true
			}
			valueMap[enum.Key] = check
		}
	}
}

func parseByteValue(order gnet.BinaryOrder, reg Register, b []byte) any {
	switch reg.Len {
	case 1:
		switch reg.Type {
		case TypeUInt:
			return b[0]
		case TypeInt:
			return gnet.NegativeCovert(int64(b[0]))
		case TypeBool:
			return b[0] > 0
		default:
			return 0
		}
	case 2:
		switch reg.Type {
		case TypeUInt:
			return order.Uint16(b)
		case TypeInt:
			return int16(gnet.NegativeCovert(int64(order.Int16(b))))
		default:
			return 0
		}
	case 4:
		switch reg.Type {
		case TypeUInt:
			return order.Uint32(b)
		case TypeInt:
			return int32(gnet.NegativeCovert(int64(order.Int32(b))))
		case TypeFloat:
			return order.Float32(b)
		default:
			return 0
		}
	case 8:
		switch reg.Type {
		case TypeUInt:
			return order.Uint64(b)
		case TypeInt:
			return gnet.NegativeCovert(order.Int64(b))
		case TypeFloat:
			return order.Float64(b)
		default:
			return 0
		}
	default:
		return 0
	}
}
