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

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeAssociateResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		private ulong _Deviceaddress;

		private uint _Assocshortaddress;

		private uint _Status;

		private uint _Securitylevel;

		private uint _Keyidmode;

		private ulong _Keysource;

		private uint _Keyindex;

		public ulong Deviceaddress
		{
			get
			{
				return _Deviceaddress;
			}
			set
			{
				_Deviceaddress = value;
			}
		}

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

		public uint Status
		{
			get
			{
				return _Status;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Status is 0xFF");
				}
				_Status = 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 AssociationStatusEnum GetStatusAsAssociationStatusEnum()
		{
			return AssociationStatusInfo.ValueToAssociationStatus(_Status);
		}

		public void SetStatusAsAssociationStatusEnum(AssociationStatusEnum enumItem)
		{
			Status = AssociationStatusInfo.AssociationStatusToValue(enumItem);
		}

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

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

	private const uint _Packetcode = 32u;

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

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.ResponseAsCommand;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint Packetcode => 32u;

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Assocshortaddress = 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.Deviceaddress = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Assocshortaddress = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Securitylevel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Keyidmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Keysource = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Keyindex = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Deviceaddress, 64);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Assocshortaddress, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.Securitylevel, 8);
		bitBuilder.AddBitsAsUint(UserFields.Keyidmode, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Keysource, 64);
		bitBuilder.AddBitsAsUint(UserFields.Keyindex, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Assocshortaddress < 0)
		{
			list.Add("Minimum allowed value for AssocShortAddress is 0x0");
		}
		if (UserFields.Assocshortaddress > 65535)
		{
			list.Add("Maximum allowed value for AssocShortAddress is 0xFFFF");
		}
		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;
	}
}
