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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForReadAfhChannelMap : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum AfhModeEnum
		{
			AFH_disabled,
			AFH_enabled,
			Reserved_for_future_use,
			InvalidAfhModeValue
		}

		public static class AfhModeInfo
		{
			public static AfhModeEnum ValueToAfhMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return AfhModeEnum.AFH_disabled;
				case 1u:
					return AfhModeEnum.AFH_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return AfhModeEnum.Reserved_for_future_use;
					}
					return AfhModeEnum.InvalidAfhModeValue;
				}
			}

			public static uint AfhModeToValue(AfhModeEnum enumItem)
			{
				return enumItem switch
				{
					AfhModeEnum.AFH_disabled => 0u, 
					AfhModeEnum.AFH_enabled => 1u, 
					AfhModeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AfhModeToString(AfhModeEnum enumItem)
			{
				return enumItem switch
				{
					AfhModeEnum.AFH_disabled => "AFH disabled", 
					AfhModeEnum.AFH_enabled => "AFH enabled", 
					AfhModeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid AfhMode Value", 
				};
			}

			public static string AfhModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = AFH disabled");
				stringBuilder.AppendLine("0x1 = AFH enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 5126u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _AfhMode;

		private List<uint> _AfhChannelMapList = new List<uint>(new uint[10]);

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

		public uint CommandOpcode => 5126u;

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

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

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

		public List<uint> AfhChannelMapList => _AfhChannelMapList;

		public CommandOpcodeEnum GetCommandOpcodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(5126u);
		}

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

		public AfhModeEnum GetAfhModeAsAfhModeEnum()
		{
			return AfhModeInfo.ValueToAfhMode(_AfhMode);
		}

		public void SetAfhModeAsAfhModeEnum(AfhModeEnum enumItem)
		{
			AfhMode = AfhModeInfo.AfhModeToValue(enumItem);
		}
	}

	private const uint _EventCode = 14u;

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

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint EventCode => 14u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumHciCommandPackets = 1u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.NumHciCommandPackets = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.CommandOpcode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AfhMode = bitBuilder.RemoveBitsAsUint(8);
		for (int num = UserFields.AfhChannelMapList.Count - 1; num >= 0; num--)
		{
			UserFields.AfhChannelMapList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumHciCommandPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CommandOpcode, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.AfhMode, 8);
		for (int num = UserFields.AfhChannelMapList.Count - 1; num >= 0; num--)
		{
			if (UserFields.AfhChannelMapList[num] > 255)
			{
				throw new Exception("UserFields.AfhChannelMapList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.AfhChannelMapList[num], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
