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

namespace QCRPG.TSL.ZigBee.Packets;

public class McpsDataRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Srcaddrmode;

		private uint _Dstaddrmode;

		private uint _Dstpanid;

		private ulong _Dstaddr;

		private uint _Msdulength;

		private uint _Msduhandle;

		private uint _Txoptions;

		private uint _Securitylevel;

		private uint _Keyidmode;

		private ulong _Keysource;

		private uint _Keyindex;

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

		public uint Srcaddrmode
		{
			get
			{
				return _Srcaddrmode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Srcaddrmode is 0xFF");
				}
				_Srcaddrmode = 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 Msduhandle
		{
			get
			{
				return _Msduhandle;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Msduhandle is 0xFF");
				}
				_Msduhandle = value;
			}
		}

		public uint Txoptions
		{
			get
			{
				return _Txoptions;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Txoptions is 0xFF");
				}
				_Txoptions = 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 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 const uint _Packetcode = 1u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(McpsDataConfirmation) };

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint Packetcode => 1u;

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Srcaddrmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Dstaddrmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Dstpanid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Dstaddr = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Msdulength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Msduhandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Txoptions = bitBuilder.RemoveBitsAsUint(8);
		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(Packetcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Srcaddrmode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Dstaddrmode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Dstpanid, 16);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Dstaddr, 64);
		bitBuilder.AddBitsAsUint(UserFields.Msdulength, 8);
		bitBuilder.AddBitsAsUint(UserFields.Msduhandle, 8);
		bitBuilder.AddBitsAsUint(UserFields.Txoptions, 8);
		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.Srcaddrmode < 0)
		{
			list.Add("Minimum allowed value for SrcAddrMode is 0x0");
		}
		if (UserFields.Srcaddrmode > 3)
		{
			list.Add("Maximum allowed value for SrcAddrMode is 0x3");
		}
		if (UserFields.Dstaddrmode > 3)
		{
			list.Add("Maximum allowed value for DstAddrMode is 0x3");
		}
		if (UserFields.Dstpanid < 0)
		{
			list.Add("Minimum allowed value for DstPANId is 0x0");
		}
		if (UserFields.Dstpanid > 65535)
		{
			list.Add("Maximum allowed value for DstPANId is 0xFFFF");
		}
		if (UserFields.Msdulength > 255)
		{
			list.Add("Maximum allowed value for msduLength is 0xFF");
		}
		if (UserFields.Msduhandle < 0)
		{
			list.Add("Minimum allowed value for msduHandle is 0x0");
		}
		if (UserFields.Msduhandle > 255)
		{
			list.Add("Maximum allowed value for msduHandle 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;
	}
}
