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

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeAssociate : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum CapabilityinformationEnum
		{
			ALT_PAN_COORDINATOR,
			DEVICE_TYPE,
			POWER_SOURCE,
			RECEIVER_ON_WHEN_IDLE,
			RESERVED,
			RSVED,
			SECURITY_CAPABILITY,
			ALLOCATE_ADDRESS,
			InvalidCapabilityinformationValue
		}

		public static class CapabilityinformationInfo
		{
			public static CapabilityinformationEnum ValueToCapabilityinformation(uint value)
			{
				return value switch
				{
					128u => CapabilityinformationEnum.ALT_PAN_COORDINATOR, 
					64u => CapabilityinformationEnum.DEVICE_TYPE, 
					32u => CapabilityinformationEnum.POWER_SOURCE, 
					16u => CapabilityinformationEnum.RECEIVER_ON_WHEN_IDLE, 
					8u => CapabilityinformationEnum.RESERVED, 
					4u => CapabilityinformationEnum.RSVED, 
					2u => CapabilityinformationEnum.SECURITY_CAPABILITY, 
					1u => CapabilityinformationEnum.ALLOCATE_ADDRESS, 
					_ => CapabilityinformationEnum.InvalidCapabilityinformationValue, 
				};
			}

			public static uint CapabilityinformationToValue(CapabilityinformationEnum enumItem)
			{
				return enumItem switch
				{
					CapabilityinformationEnum.ALT_PAN_COORDINATOR => 128u, 
					CapabilityinformationEnum.DEVICE_TYPE => 64u, 
					CapabilityinformationEnum.POWER_SOURCE => 32u, 
					CapabilityinformationEnum.RECEIVER_ON_WHEN_IDLE => 16u, 
					CapabilityinformationEnum.RESERVED => 8u, 
					CapabilityinformationEnum.RSVED => 4u, 
					CapabilityinformationEnum.SECURITY_CAPABILITY => 2u, 
					CapabilityinformationEnum.ALLOCATE_ADDRESS => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string CapabilityinformationToString(CapabilityinformationEnum enumItem)
			{
				return enumItem switch
				{
					CapabilityinformationEnum.ALT_PAN_COORDINATOR => "ALT_PAN_COORDINATOR", 
					CapabilityinformationEnum.DEVICE_TYPE => "DEVICE_TYPE", 
					CapabilityinformationEnum.POWER_SOURCE => "POWER_SOURCE", 
					CapabilityinformationEnum.RECEIVER_ON_WHEN_IDLE => "RECEIVER_ON_WHEN_IDLE", 
					CapabilityinformationEnum.RESERVED => "RESERVED", 
					CapabilityinformationEnum.RSVED => "RESERVED", 
					CapabilityinformationEnum.SECURITY_CAPABILITY => "SECURITY_CAPABILITY", 
					CapabilityinformationEnum.ALLOCATE_ADDRESS => "ALLOCATE_ADDRESS", 
					_ => "Invalid Capabilityinformation Value", 
				};
			}

			public static string CapabilityinformationToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x80 = ALT_PAN_COORDINATOR");
				stringBuilder.AppendLine("0x40 = DEVICE_TYPE");
				stringBuilder.AppendLine("0x20 = POWER_SOURCE");
				stringBuilder.AppendLine("0x10 = RECEIVER_ON_WHEN_IDLE");
				stringBuilder.AppendLine("0x8 = RESERVED");
				stringBuilder.AppendLine("0x4 = RESERVED");
				stringBuilder.AppendLine("0x2 = SECURITY_CAPABILITY");
				stringBuilder.AppendLine("0x1 = ALLOCATE_ADDRESS");
				return stringBuilder.ToString();
			}
		}

		private uint _Logicalchannel;

		private uint _Channelpage;

		private uint _Coordaddrmode = 2u;

		private uint _Coordpanid;

		private ulong _Coordaddress;

		private uint _Capabilityinformation;

		private uint _Securitylevel;

		private uint _Keyidmode;

		private ulong _Keysource;

		private uint _Keyindex;

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

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

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

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

		public ulong Coordaddress
		{
			get
			{
				return _Coordaddress;
			}
			set
			{
				_Coordaddress = value;
			}
		}

		public uint Capabilityinformation
		{
			get
			{
				return _Capabilityinformation;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Capabilityinformation is 0xFF");
				}
				_Capabilityinformation = 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 DutaddressmodeEnum GetCoordaddrmodeAsDutaddressmodeEnum()
		{
			return DutaddressmodeInfo.ValueToDutaddressmode(_Coordaddrmode);
		}

		public void SetCoordaddrmodeAsDutaddressmodeEnum(DutaddressmodeEnum enumItem)
		{
			Coordaddrmode = DutaddressmodeInfo.DutaddressmodeToValue(enumItem);
		}

		public CapabilityinformationEnum GetCapabilityinformationAsCapabilityinformationEnum()
		{
			return CapabilityinformationInfo.ValueToCapabilityinformation(_Capabilityinformation);
		}

		public void SetCapabilityinformationAsCapabilityinformationEnum(CapabilityinformationEnum enumItem)
		{
			Capabilityinformation = CapabilityinformationInfo.CapabilityinformationToValue(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[1] { typeof(MlmeAssociateConfirmation) };

	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 MlmeAssociateInfo(this);
			}
			return info;
		}
	}

	public uint Packetcode => 32u;

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Coordaddrmode = 2u;
		UserFields.Coordpanid = 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.Logicalchannel = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Channelpage = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Coordaddrmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Coordpanid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Coordaddress = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Capabilityinformation = 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.AddBitsAsUint(UserFields.Logicalchannel, 8);
		bitBuilder.AddBitsAsUint(UserFields.Channelpage, 8);
		bitBuilder.AddBitsAsUint(UserFields.Coordaddrmode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Coordpanid, 16);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Coordaddress, 64);
		bitBuilder.AddBitsAsUint(UserFields.Capabilityinformation, 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.Coordaddrmode < 2)
		{
			list.Add("Minimum allowed value for CoordAddrMode is 0x2");
		}
		if (UserFields.Coordaddrmode > 3)
		{
			list.Add("Maximum allowed value for CoordAddrMode is 0x3");
		}
		if (UserFields.Coordpanid < 0)
		{
			list.Add("Minimum allowed value for CoordPANId is 0x0");
		}
		if (UserFields.Coordpanid > 65535)
		{
			list.Add("Maximum allowed value for CoordPANId 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;
	}
}
