#nullable enable
namespace InABox.DigitalMatter
{
    public class DMSensorNodeBluetoothTag : DMBluetoothTag
    {
        public uint SerialNumber { get; set; }

        public sbyte TxPower { get; set; }

        public byte BatteryVoltage { get; set; }

        public sbyte InternalTemperature { get; set; }
        
        public short Probe1 { get; set; }
        
        public short Probe2 { get; set; }
        
        public short TemperatureProbe { get; set; }

        public short HumidityProbe { get; set; }
        
        public bool Digital1 { get; set; }
        
        public bool Digital2 { get; set; }
        
        public ushort Analog1 { get; set; }
        
        public ushort Analog2 { get; set; }
        
        public override string ID()
        {
            return SerialNumber.ToString();
        }

        public override string Type()
        {
            return "SensorNode";
        }

        protected override void DoDecode(IDMReadBuffer buffer)
        {
            SerialNumber = buffer.TakeUInt32();
            TxPower = buffer.TakeInt8();
            BatteryVoltage = buffer.TakeByte();
            InternalTemperature = buffer.TakeInt8();
            Probe1 = buffer.TakeInt16();
            Probe2 = buffer.TakeInt16();
            TemperatureProbe = buffer.TakeInt16();
            HumidityProbe = buffer.TakeInt16();

            var digital = buffer.TakeByte();
            Digital1 = (digital & (0x1 << 0)) != 0;
            Digital2 = (digital & (0x1 << 1)) != 0;

            Analog1 = buffer.TakeUInt16();
            Analog2 = buffer.TakeUInt16();

            var unused_reserved = buffer.TakeByte();
            
        }

        protected override void DoEncode(IDMWriteBuffer buffer)
        {
            buffer.AddUInt32(SerialNumber);
            buffer.AddInt8(TxPower);
            buffer.AddByte(BatteryVoltage);
            buffer.AddInt8(InternalTemperature);
            
            buffer.AddInt16(Probe1);
            buffer.AddInt16(Probe2);
            buffer.AddInt16(TemperatureProbe);
            buffer.AddInt16(HumidityProbe);

            byte[] digital = new byte[1];
            digital = IDMBuffer.SetBits(digital, 0, new bool[] { Digital1, Digital2 });
            buffer.AddByte(digital[0]);
            
            buffer.AddUInt16(Analog1);
            buffer.AddUInt16(Analog2);

            buffer.AddByte(0);
        }
    }
}