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

namespace QCRPG.TSL.BT.Packets;

public class TwsEscoSetup : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum MicrophonePathEnum
		{
			Left,
			Right,
			InvalidMicrophonePathValue
		}

		public static class MicrophonePathInfo
		{
			public static MicrophonePathEnum ValueToMicrophonePath(uint value)
			{
				return value switch
				{
					0u => MicrophonePathEnum.Left, 
					1u => MicrophonePathEnum.Right, 
					_ => MicrophonePathEnum.InvalidMicrophonePathValue, 
				};
			}

			public static uint MicrophonePathToValue(MicrophonePathEnum enumItem)
			{
				return enumItem switch
				{
					MicrophonePathEnum.Left => 0u, 
					MicrophonePathEnum.Right => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MicrophonePathToString(MicrophonePathEnum enumItem)
			{
				return enumItem switch
				{
					MicrophonePathEnum.Left => "Use left microphone path", 
					MicrophonePathEnum.Right => "Use right microphone path", 
					_ => "Invalid MicrophonePath Value", 
				};
			}

			public static string MicrophonePathToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Use left microphone path");
				stringBuilder.AppendLine("0x1 = Use right microphone path");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 0u;

		private ulong _BdAddrLeft;

		private ulong _BdAddrRight;

		private uint _MicrophonePath;

		public uint SubOpcode => 0u;

		public ulong BdAddrLeft
		{
			get
			{
				return _BdAddrLeft;
			}
			set
			{
				_BdAddrLeft = value;
			}
		}

		public ulong BdAddrRight
		{
			get
			{
				return _BdAddrRight;
			}
			set
			{
				_BdAddrRight = value;
			}
		}

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

		public MicrophonePathEnum GetMicrophonePathAsMicrophonePathEnum()
		{
			return MicrophonePathInfo.ValueToMicrophonePath(_MicrophonePath);
		}

		public void SetMicrophonePathAsMicrophonePathEnum(MicrophonePathEnum enumItem)
		{
			MicrophonePath = MicrophonePathInfo.MicrophonePathToValue(enumItem);
		}
	}

	private const uint _Opcode = 64525u;

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

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

	public uint Opcode => 64525u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.BdAddrLeft = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.BdAddrRight = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.MicrophonePath = 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.SubOpcode, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BdAddrLeft, 48);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BdAddrRight, 48);
		bitBuilder.AddBitsAsUint(UserFields.MicrophonePath, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
