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

namespace QCRPG.TSL.BT.Packets;

public class WriteExtendedInquiryResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _FecRequired;

		private uint _EirDataCount = 1u;

		private List<ExtendedInquiryResponseSubField> _ExtendedInquiryResponseList = new List<ExtendedInquiryResponseSubField>();

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

		public uint EirDataCount
		{
			get
			{
				return _EirDataCount;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for EirDataCount is 0xFF");
				}
				if (value > 120)
				{
					_EirDataCount = 120u;
				}
				else
				{
					_EirDataCount = value;
				}
				while (_ExtendedInquiryResponseList.Count < _EirDataCount)
				{
					_ExtendedInquiryResponseList.Add(new ExtendedInquiryResponseSubField());
				}
				while (_ExtendedInquiryResponseList.Count > _EirDataCount)
				{
					_ExtendedInquiryResponseList.RemoveAt(_ExtendedInquiryResponseList.Count - 1);
				}
			}
		}

		public List<ExtendedInquiryResponseSubField> ExtendedInquiryResponseList => _ExtendedInquiryResponseList;

		public FecRequiredEnum GetFecRequiredAsFecRequiredEnum()
		{
			return FecRequiredInfo.ValueToFecRequired(_FecRequired);
		}

		public void SetFecRequiredAsFecRequiredEnum(FecRequiredEnum enumItem)
		{
			FecRequired = FecRequiredInfo.FecRequiredToValue(enumItem);
		}
	}

	public class ExtendedInquiryResponseSubField
	{
		private uint _Length;

		private uint _DataType;

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

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Length is 0xFF");
				}
				_Length = value;
				if (_Length < 1)
				{
					_Length = 1u;
				}
				while (_DataList.Count < _Length - 1)
				{
					_DataList.Add(0u);
				}
				while (_DataList.Count > _Length - 1)
				{
					_DataList.RemoveAt(_DataList.Count - 1);
				}
			}
		}

		public uint DataType
		{
			get
			{
				return _DataType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for DataType is 0xFF");
				}
				if (GenericAccessProfileAssignedNumbersInfo.ValueToGenericAccessProfileAssignedNumbers(value) == GenericAccessProfileAssignedNumbersEnum.InvalidGenericAccessProfileAssignedNumbersValue)
				{
					throw new Exception("Invalid DataType value");
				}
				_DataType = value;
			}
		}

		public List<uint> DataList => _DataList;

		public void SetDefaultValues()
		{
			Length = 1u;
			DataType = 9u;
		}

		public GenericAccessProfileAssignedNumbersEnum GetDataTypeAsGenericAccessProfileAssignedNumbersEnum()
		{
			return GenericAccessProfileAssignedNumbersInfo.ValueToGenericAccessProfileAssignedNumbers(_DataType);
		}

		public void SetDataTypeAsGenericAccessProfileAssignedNumbersEnum(GenericAccessProfileAssignedNumbersEnum enumItem)
		{
			DataType = GenericAccessProfileAssignedNumbersInfo.GenericAccessProfileAssignedNumbersToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Length = bitBuilder.RemoveBitsAsUint(8);
			DataType = bitBuilder.RemoveBitsAsUint(8);
			for (int i = 0; i < DataList.Count; i++)
			{
				DataList[i] = bitBuilder.RemoveBitsAsUint(8);
			}
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(Length, 8);
			bitBuilder.AddBitsAsUint(DataType, 8);
			for (int i = 0; i < DataList.Count; i++)
			{
				if (DataList[i] > 255)
				{
					throw new Exception("DataList[i] is > 0xFF");
				}
				bitBuilder.AddBitsAsUint(DataList[i], 8);
			}
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 3154u;

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

	private PacketInfoBase info;

	private uint _PayloadLength;

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

	public uint Opcode => 3154u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.FecRequired = 0u;
		UserFields.EirDataCount = 1u;
		for (int i = 0; i < UserFields.ExtendedInquiryResponseList.Count; i++)
		{
			UserFields.ExtendedInquiryResponseList[i].SetDefaultValues();
		}
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.FecRequired = bitBuilder.RemoveBitsAsUint(8);
		UserFields.EirDataCount = 1u;
		int numberOfBits2 = bitBuilder.GetNumberOfBits();
		for (int i = 0; i < UserFields.ExtendedInquiryResponseList.Count; i++)
		{
			try
			{
				UserFields.ExtendedInquiryResponseList[i].FromBitBuilder(ref bitBuilder);
				UserFields.EirDataCount = (uint)(UserFields.ExtendedInquiryResponseList.Count + 1);
			}
			catch (Exception)
			{
				UserFields.EirDataCount = (uint)(UserFields.ExtendedInquiryResponseList.Count - 1);
			}
		}
		bitBuilder.RemoveBitsAsString(1920 - (numberOfBits2 - bitBuilder.GetNumberOfBits()));
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.FecRequired, 8);
		int numberOfBits2 = bitBuilder.GetNumberOfBits();
		for (int i = 0; i < UserFields.ExtendedInquiryResponseList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ExtendedInquiryResponseList[i].ToBitString());
		}
		bitBuilder.AddBitsAsUint(0u, 1920 - (bitBuilder.GetNumberOfBits() - numberOfBits2));
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.EirDataCount < 1)
		{
			list.Add("Minimum allowed value for EIR Data Count is 0x1");
		}
		if (UserFields.EirDataCount > 120)
		{
			list.Add("Maximum allowed value for EIR Data Count is 0x78");
		}
		return list;
	}
}
