using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class BurstBroadcastAudio : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableEnum
		{
			Disable,
			Enable,
			InvalidEnableValue
		}

		public static class EnableInfo
		{
			public static EnableEnum ValueToEnable(uint value)
			{
				return value switch
				{
					0u => EnableEnum.Disable, 
					1u => EnableEnum.Enable, 
					_ => EnableEnum.InvalidEnableValue, 
				};
			}

			public static uint EnableToValue(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable => 0u, 
					EnableEnum.Enable => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableToString(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable => "Disable", 
					EnableEnum.Enable => "Enable", 
					_ => "Invalid Enable Value", 
				};
			}

			public static string EnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable");
				stringBuilder.AppendLine("0x1 = Enable");
				return stringBuilder.ToString();
			}
		}

		public enum TotalNumberOfPacketsEnum
		{
			Total_No_Of_Packets,
			InvalidTotalNumberOfPacketsValue
		}

		public static class TotalNumberOfPacketsInfo
		{
			public static TotalNumberOfPacketsEnum ValueToTotalNumberOfPackets(uint value)
			{
				if (value >= 1 && value <= 255)
				{
					return TotalNumberOfPacketsEnum.Total_No_Of_Packets;
				}
				return TotalNumberOfPacketsEnum.InvalidTotalNumberOfPacketsValue;
			}

			public static uint TotalNumberOfPacketsToValue(TotalNumberOfPacketsEnum enumItem)
			{
				if (enumItem == TotalNumberOfPacketsEnum.Total_No_Of_Packets)
				{
					return 1u;
				}
				return uint.MaxValue;
			}

			public static string TotalNumberOfPacketsToString(TotalNumberOfPacketsEnum enumItem)
			{
				if (enumItem == TotalNumberOfPacketsEnum.Total_No_Of_Packets)
				{
					return "Total number of packets";
				}
				return "Invalid TotalNumberOfPackets Value";
			}

			public static string TotalNumberOfPacketsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1-0xFF = Total number of packets");
				return stringBuilder.ToString();
			}
		}

		public enum MinimumPacketsEnum
		{
			No_Of_Min_Packets,
			InvalidMinimumPacketsValue
		}

		public static class MinimumPacketsInfo
		{
			public static MinimumPacketsEnum ValueToMinimumPackets(uint value)
			{
				if (value >= 1 && value <= 255)
				{
					return MinimumPacketsEnum.No_Of_Min_Packets;
				}
				return MinimumPacketsEnum.InvalidMinimumPacketsValue;
			}

			public static uint MinimumPacketsToValue(MinimumPacketsEnum enumItem)
			{
				if (enumItem == MinimumPacketsEnum.No_Of_Min_Packets)
				{
					return 1u;
				}
				return uint.MaxValue;
			}

			public static string MinimumPacketsToString(MinimumPacketsEnum enumItem)
			{
				if (enumItem == MinimumPacketsEnum.No_Of_Min_Packets)
				{
					return "Number of min packets";
				}
				return "Invalid MinimumPackets Value";
			}

			public static string MinimumPacketsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1-0xFF = Number of min packets");
				return stringBuilder.ToString();
			}
		}

		public enum CsbPacketTypeEnum
		{
			BREDR_packets,
			QHS_packets,
			InvalidCsbPacketTypeValue
		}

		public static class CsbPacketTypeInfo
		{
			public static CsbPacketTypeEnum ValueToCsbPacketType(uint value)
			{
				return value switch
				{
					0u => CsbPacketTypeEnum.BREDR_packets, 
					1u => CsbPacketTypeEnum.QHS_packets, 
					_ => CsbPacketTypeEnum.InvalidCsbPacketTypeValue, 
				};
			}

			public static uint CsbPacketTypeToValue(CsbPacketTypeEnum enumItem)
			{
				return enumItem switch
				{
					CsbPacketTypeEnum.BREDR_packets => 0u, 
					CsbPacketTypeEnum.QHS_packets => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string CsbPacketTypeToString(CsbPacketTypeEnum enumItem)
			{
				return enumItem switch
				{
					CsbPacketTypeEnum.BREDR_packets => "BREDR packets", 
					CsbPacketTypeEnum.QHS_packets => "QHS packets", 
					_ => "Invalid CsbPacketType Value", 
				};
			}

			public static string CsbPacketTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = BREDR packets");
				stringBuilder.AppendLine("0x1 = QHS packets");
				return stringBuilder.ToString();
			}
		}

		private uint _Enable;

		private uint _TotalNumberOfPackets;

		private uint _MinimumPackets;

		private uint _CsbPacketType;

		private uint _PacketInterval;

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

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

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

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

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

		public EnableEnum GetEnableAsEnableEnum()
		{
			return EnableInfo.ValueToEnable(_Enable);
		}

		public void SetEnableAsEnableEnum(EnableEnum enumItem)
		{
			Enable = EnableInfo.EnableToValue(enumItem);
		}

		public TotalNumberOfPacketsEnum GetTotalNumberOfPacketsAsTotalNumberOfPacketsEnum()
		{
			return TotalNumberOfPacketsInfo.ValueToTotalNumberOfPackets(_TotalNumberOfPackets);
		}

		public void SetTotalNumberOfPacketsAsTotalNumberOfPacketsEnum(TotalNumberOfPacketsEnum enumItem)
		{
			TotalNumberOfPackets = TotalNumberOfPacketsInfo.TotalNumberOfPacketsToValue(enumItem);
		}

		public MinimumPacketsEnum GetMinimumPacketsAsMinimumPacketsEnum()
		{
			return MinimumPacketsInfo.ValueToMinimumPackets(_MinimumPackets);
		}

		public void SetMinimumPacketsAsMinimumPacketsEnum(MinimumPacketsEnum enumItem)
		{
			MinimumPackets = MinimumPacketsInfo.MinimumPacketsToValue(enumItem);
		}

		public CsbPacketTypeEnum GetCsbPacketTypeAsCsbPacketTypeEnum()
		{
			return CsbPacketTypeInfo.ValueToCsbPacketType(_CsbPacketType);
		}

		public void SetCsbPacketTypeAsCsbPacketTypeEnum(CsbPacketTypeEnum enumItem)
		{
			CsbPacketType = CsbPacketTypeInfo.CsbPacketTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64550u;

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

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

	public uint Opcode => 64550u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.TotalNumberOfPackets = 5u;
		UserFields.MinimumPackets = 2u;
	}

	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.Enable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TotalNumberOfPackets = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MinimumPackets = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CsbPacketType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketInterval = 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.AddBitsAsUint(UserFields.Enable, 8);
		bitBuilder.AddBitsAsUint(UserFields.TotalNumberOfPackets, 8);
		bitBuilder.AddBitsAsUint(UserFields.MinimumPackets, 8);
		bitBuilder.AddBitsAsUint(UserFields.CsbPacketType, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketInterval, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
