using System;
using System.Collections.Generic;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee.Packets;

public class McpsDataIndication : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _EventCode = 1u;

		private uint _Packetlength;

		private uint _Protocolid;

		private uint _Length;

		private uint _Srcaddrmode;

		private uint _Srcpanid;

		private ulong _Srcaddr;

		private uint _Dstaddrmode;

		private uint _Dstpanid;

		private ulong _Dstaddr;

		private uint _Msdulength;

		private uint _Mpdulinkquality;

		private uint _Dsn;

		private uint _Timestamp;

		private uint _Securitylevel;

		private uint _Keyidmode;

		private ulong _Keysource;

		private uint _Keyindex;

		private List<uint> _MsdudataList = new List<uint>();

		public uint Packetlength
		{
			get
			{
				return _Packetlength;
			}
			set
			{
				_Packetlength = value;
			}
		}

		public uint Protocolid
		{
			get
			{
				return _Protocolid;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Protocolid is 0xFF");
				}
				_Protocolid = value;
			}
		}

		public uint EventCode => 1u;

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Length is 0xFFFF");
				}
				_Length = value;
			}
		}

		public uint Srcaddrmode
		{
			get
			{
				return _Srcaddrmode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Srcaddrmode is 0xFF");
				}
				_Srcaddrmode = value;
			}
		}

		public uint Srcpanid
		{
			get
			{
				return _Srcpanid;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Srcpanid is 0xFFFF");
				}
				_Srcpanid = value;
			}
		}

		public ulong Srcaddr
		{
			get
			{
				return _Srcaddr;
			}
			set
			{
				_Srcaddr = value;
			}
		}

		public uint Dstaddrmode
		{
			get
			{
				return _Dstaddrmode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Dstaddrmode is 0xFF");
				}
				_Dstaddrmode = value;
			}
		}

		public uint Dstpanid
		{
			get
			{
				return _Dstpanid;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Dstpanid is 0xFFFF");
				}
				_Dstpanid = value;
			}
		}

		public ulong Dstaddr
		{
			get
			{
				return _Dstaddr;
			}
			set
			{
				_Dstaddr = value;
			}
		}

		public uint Msdulength
		{
			get
			{
				return _Msdulength;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Msdulength is 0xFF");
				}
				_Msdulength = value;
				while (_MsdudataList.Count < _Msdulength)
				{
					_MsdudataList.Add(0u);
				}
				while (_MsdudataList.Count > _Msdulength)
				{
					_MsdudataList.RemoveAt(_MsdudataList.Count - 1);
				}
			}
		}

		public uint Mpdulinkquality
		{
			get
			{
				return _Mpdulinkquality;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Mpdulinkquality is 0xFFFF");
				}
				_Mpdulinkquality = value;
			}
		}

		public uint Dsn
		{
			get
			{
				return _Dsn;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Dsn is 0xFF");
				}
				_Dsn = value;
			}
		}

		public uint Timestamp
		{
			get
			{
				return _Timestamp;
			}
			set
			{
				_Timestamp = value;
			}
		}

		public uint Securitylevel
		{
			get
			{
				return _Securitylevel;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Securitylevel is 0xFF");
				}
				_Securitylevel = value;
			}
		}

		public uint Keyidmode
		{
			get
			{
				return _Keyidmode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Keyidmode is 0xFF");
				}
				_Keyidmode = value;
			}
		}

		public ulong Keysource
		{
			get
			{
				return _Keysource;
			}
			set
			{
				_Keysource = value;
			}
		}

		public uint Keyindex
		{
			get
			{
				return _Keyindex;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Keyindex is 0xFF");
				}
				_Keyindex = value;
			}
		}

		public List<uint> MsdudataList => _MsdudataList;

		public ProtocolIdEnum GetProtocolidAsProtocolIdEnum()
		{
			return ProtocolIdInfo.ValueToProtocolId(_Protocolid);
		}

		public void SetProtocolidAsProtocolIdEnum(ProtocolIdEnum enumItem)
		{
			Protocolid = ProtocolIdInfo.ProtocolIdToValue(enumItem);
		}

		public CommandOpcodeEnum GetEventCodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(1u);
		}

		public DutaddressmodeEnum GetSrcaddrmodeAsDutaddressmodeEnum()
		{
			return DutaddressmodeInfo.ValueToDutaddressmode(_Srcaddrmode);
		}

		public void SetSrcaddrmodeAsDutaddressmodeEnum(DutaddressmodeEnum enumItem)
		{
			Srcaddrmode = DutaddressmodeInfo.DutaddressmodeToValue(enumItem);
		}

		public DutaddressmodeEnum GetDstaddrmodeAsDutaddressmodeEnum()
		{
			return DutaddressmodeInfo.ValueToDutaddressmode(_Dstaddrmode);
		}

		public void SetDstaddrmodeAsDutaddressmodeEnum(DutaddressmodeEnum enumItem)
		{
			Dstaddrmode = DutaddressmodeInfo.DutaddressmodeToValue(enumItem);
		}

		public SecurityLevelOptionsEnum GetSecuritylevelAsSecurityLevelOptionsEnum()
		{
			return SecurityLevelOptionsInfo.ValueToSecurityLevelOptions(_Securitylevel);
		}

		public void SetSecuritylevelAsSecurityLevelOptionsEnum(SecurityLevelOptionsEnum enumItem)
		{
			Securitylevel = SecurityLevelOptionsInfo.SecurityLevelOptionsToValue(enumItem);
		}
	}

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Indication;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new McpsDataIndicationInfo(this);
			}
			return info;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Securitylevel = 0u;
		UserFields.Keyidmode = 0u;
		UserFields.Keysource = 1312068706972498023uL;
		UserFields.Keyindex = 1u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.Packetlength = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Protocolid = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.EventCode)
		{
			return false;
		}
		UserFields.Length = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Srcaddrmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Srcpanid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Srcaddr = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Dstaddrmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Dstpanid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Dstaddr = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Msdulength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Mpdulinkquality = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Dsn = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Timestamp = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Securitylevel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Keyidmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Keysource = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Keyindex = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.MsdudataList.Count; i++)
		{
			UserFields.MsdudataList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Packetlength, 32);
		bitBuilder.AddBitsAsUint(UserFields.Protocolid, 8);
		bitBuilder.AddBitsAsUint(UserFields.EventCode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Length, 16);
		bitBuilder.AddBitsAsUint(UserFields.Srcaddrmode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Srcpanid, 16);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Srcaddr, 64);
		bitBuilder.AddBitsAsUint(UserFields.Dstaddrmode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Dstpanid, 16);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Dstaddr, 64);
		bitBuilder.AddBitsAsUint(UserFields.Msdulength, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Mpdulinkquality, 16);
		bitBuilder.AddBitsAsUint(UserFields.Dsn, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Timestamp, 32);
		bitBuilder.AddBitsAsUint(UserFields.Securitylevel, 8);
		bitBuilder.AddBitsAsUint(UserFields.Keyidmode, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Keysource, 64);
		bitBuilder.AddBitsAsUint(UserFields.Keyindex, 8);
		for (int i = 0; i < UserFields.MsdudataList.Count; i++)
		{
			if (UserFields.MsdudataList[i] > 255)
			{
				throw new Exception("UserFields.MsdudataList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.MsdudataList[i], 8);
		}
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Msdulength > 255)
		{
			list.Add("Maximum allowed value for msduLength is 0xFF");
		}
		if (UserFields.Securitylevel < 0)
		{
			list.Add("Minimum allowed value for SecurityLevel is 0x0");
		}
		if (UserFields.Securitylevel > 7)
		{
			list.Add("Maximum allowed value for SecurityLevel is 0x7");
		}
		if (UserFields.Keyidmode < 0)
		{
			list.Add("Minimum allowed value for KeyIdMode is 0x0");
		}
		if (UserFields.Keyidmode > 3)
		{
			list.Add("Maximum allowed value for KeyIdMode is 0x3");
		}
		return list;
	}
}
