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

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeDisassociateConfirmation : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum DeviceaddrmodeEnum
		{
			ShortAddress_16_BIT,
			ExtendedAddress_64_BIT,
			InvalidDeviceaddrmodeValue
		}

		public static class DeviceaddrmodeInfo
		{
			public static DeviceaddrmodeEnum ValueToDeviceaddrmode(uint value)
			{
				return value switch
				{
					2u => DeviceaddrmodeEnum.ShortAddress_16_BIT, 
					3u => DeviceaddrmodeEnum.ExtendedAddress_64_BIT, 
					_ => DeviceaddrmodeEnum.InvalidDeviceaddrmodeValue, 
				};
			}

			public static uint DeviceaddrmodeToValue(DeviceaddrmodeEnum enumItem)
			{
				return enumItem switch
				{
					DeviceaddrmodeEnum.ShortAddress_16_BIT => 2u, 
					DeviceaddrmodeEnum.ExtendedAddress_64_BIT => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string DeviceaddrmodeToString(DeviceaddrmodeEnum enumItem)
			{
				return enumItem switch
				{
					DeviceaddrmodeEnum.ShortAddress_16_BIT => "16-bit short address", 
					DeviceaddrmodeEnum.ExtendedAddress_64_BIT => "64-bit extended address", 
					_ => "Invalid Deviceaddrmode Value", 
				};
			}

			public static string DeviceaddrmodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x2 = 16-bit short address");
				stringBuilder.AppendLine("0x3 = 64-bit extended address");
				return stringBuilder.ToString();
			}
		}

		private const uint _EventCode = 33u;

		private uint _Packetlength;

		private uint _Protocolid;

		private uint _Length;

		private uint _Status;

		private uint _Deviceaddrmode = 2u;

		private uint _Devicepanid;

		private ulong _Deviceaddress;

		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 => 33u;

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Length is 0xFFFF");
				}
				_Length = 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 Deviceaddrmode
		{
			get
			{
				return _Deviceaddrmode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Deviceaddrmode is 0xFF");
				}
				_Deviceaddrmode = value;
			}
		}

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

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

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

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

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

		public AssociationStatusEnum GetStatusAsAssociationStatusEnum()
		{
			return AssociationStatusInfo.ValueToAssociationStatus(_Status);
		}

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

		public DeviceaddrmodeEnum GetDeviceaddrmodeAsDeviceaddrmodeEnum()
		{
			return DeviceaddrmodeInfo.ValueToDeviceaddrmode(_Deviceaddrmode);
		}

		public void SetDeviceaddrmodeAsDeviceaddrmodeEnum(DeviceaddrmodeEnum enumItem)
		{
			Deviceaddrmode = DeviceaddrmodeInfo.DeviceaddrmodeToValue(enumItem);
		}
	}

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

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Response;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Deviceaddrmode = 2u;
		UserFields.Devicepanid = 0u;
	}

	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.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Deviceaddrmode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Devicepanid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Deviceaddress = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		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.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.Deviceaddrmode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Devicepanid, 16);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Deviceaddress, 64);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Deviceaddrmode < 2)
		{
			list.Add("Minimum allowed value for DeviceAddrMode is 0x2");
		}
		if (UserFields.Deviceaddrmode > 3)
		{
			list.Add("Maximum allowed value for DeviceAddrMode is 0x3");
		}
		if (UserFields.Devicepanid < 0)
		{
			list.Add("Minimum allowed value for DevicePANId is 0x0");
		}
		if (UserFields.Devicepanid > 65535)
		{
			list.Add("Maximum allowed value for DevicePANId is 0xFFFF");
		}
		return list;
	}
}
