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

namespace QCRPG.TSL.BT.Packets;

public class Inquiry : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum NumResponsesEnum
		{
			UNLIMITED_NUMBER_OF_RESPONSES,
			MAXIMUM_NUMBER_OF_RESPONSES_BEFORE_INQUIRY_IS_HALTED,
			InvalidNumResponsesValue
		}

		public static class NumResponsesInfo
		{
			public static NumResponsesEnum ValueToNumResponses(uint value)
			{
				if (value == 0)
				{
					return NumResponsesEnum.UNLIMITED_NUMBER_OF_RESPONSES;
				}
				if (value >= 1 && value <= 255)
				{
					return NumResponsesEnum.MAXIMUM_NUMBER_OF_RESPONSES_BEFORE_INQUIRY_IS_HALTED;
				}
				return NumResponsesEnum.InvalidNumResponsesValue;
			}

			public static uint NumResponsesToValue(NumResponsesEnum enumItem)
			{
				return enumItem switch
				{
					NumResponsesEnum.UNLIMITED_NUMBER_OF_RESPONSES => 0u, 
					NumResponsesEnum.MAXIMUM_NUMBER_OF_RESPONSES_BEFORE_INQUIRY_IS_HALTED => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NumResponsesToString(NumResponsesEnum enumItem)
			{
				return enumItem switch
				{
					NumResponsesEnum.UNLIMITED_NUMBER_OF_RESPONSES => "Unlimited number of responses", 
					NumResponsesEnum.MAXIMUM_NUMBER_OF_RESPONSES_BEFORE_INQUIRY_IS_HALTED => "Maximum number of responses before inquiry is halted", 
					_ => "Invalid NumResponses Value", 
				};
			}

			public static string NumResponsesToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Unlimited number of responses");
				stringBuilder.AppendLine("0x1-0xFF = Maximum number of responses before inquiry is halted");
				return stringBuilder.ToString();
			}
		}

		private uint _Lap = 10390272u;

		private uint _InquiryLength = 1u;

		private uint _NumResponses;

		public uint Lap
		{
			get
			{
				return _Lap;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for Lap is 0xFFFFFF");
				}
				_Lap = value;
			}
		}

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

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

		public NumResponsesEnum GetNumResponsesAsNumResponsesEnum()
		{
			return NumResponsesInfo.ValueToNumResponses(_NumResponses);
		}

		public void SetNumResponsesAsNumResponsesEnum(NumResponsesEnum enumItem)
		{
			NumResponses = NumResponsesInfo.NumResponsesToValue(enumItem);
		}
	}

	private const uint _Opcode = 1025u;

	private Type[] expectedResponseTypes = new Type[3]
	{
		typeof(CommandStatusForInquiry),
		typeof(InquiryComplete),
		typeof(InquiryResultWithRssi)
	};

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

	public uint Opcode => 1025u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Lap = 10390323u;
		UserFields.InquiryLength = 5u;
		UserFields.NumResponses = 64u;
	}

	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.Lap = bitBuilder.RemoveBitsAsUintLittleEndian(24);
		UserFields.InquiryLength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumResponses = bitBuilder.RemoveBitsAsUint(8);
		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.AddBitsAsUintLittleEndian(UserFields.Lap, 24);
		bitBuilder.AddBitsAsUint(UserFields.InquiryLength, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumResponses, 8);
		_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.Lap < 10390272)
		{
			list.Add("Minimum allowed value for LAP is 0x9E8B00");
		}
		if (UserFields.Lap > 10390335)
		{
			list.Add("Maximum allowed value for LAP is 0x9E8B3F");
		}
		if (UserFields.InquiryLength < 1)
		{
			list.Add("Minimum allowed value for Inquiry Length is 0x1");
		}
		if (UserFields.InquiryLength > 48)
		{
			list.Add("Maximum allowed value for Inquiry Length is 0x30");
		}
		return list;
	}
}
