#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()
        {
            SerialNumber = TakeUInt32();
            TxPower = TakeInt8();
            BatteryVoltage = TakeByte();
            InternalTemperature = TakeInt8();
            Probe1 = TakeInt16();
            Probe2 = TakeInt16();
            TemperatureProbe = TakeInt16();
            HumidityProbe = TakeInt16();

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

            Analog1 = TakeUInt16();
            Analog2 = TakeUInt16();

            var unused_reserved = TakeByte();
            
        }

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

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

            AddByte(0);
            
            
        }
    }
}