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

namespace QCRPG.TSL.FMHCI.Packets;

public class CommandCompleteForPinConnectivityTest : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _CommandOpcode = 64521u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _NumberOfTestsDone;

		private List<TestResultsSubField> _TestResultsList = new List<TestResultsSubField>();

		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 => 64521u;

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

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

		public List<TestResultsSubField> TestResultsList => _TestResultsList;

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

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

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

	public class TestResultsSubField
	{
		public enum TestModuleIdEnum
		{
			BT,
			WLAN,
			FM,
			InvalidTestModuleIdValue
		}

		public static class TestModuleIdInfo
		{
			public static TestModuleIdEnum ValueToTestModuleId(uint value)
			{
				return value switch
				{
					2u => TestModuleIdEnum.BT, 
					3u => TestModuleIdEnum.WLAN, 
					4u => TestModuleIdEnum.FM, 
					_ => TestModuleIdEnum.InvalidTestModuleIdValue, 
				};
			}

			public static uint TestModuleIdToValue(TestModuleIdEnum enumItem)
			{
				return enumItem switch
				{
					TestModuleIdEnum.BT => 2u, 
					TestModuleIdEnum.WLAN => 3u, 
					TestModuleIdEnum.FM => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TestModuleIdToString(TestModuleIdEnum enumItem)
			{
				return enumItem switch
				{
					TestModuleIdEnum.BT => "Test ID for BT Module", 
					TestModuleIdEnum.WLAN => "Test ID for WLAN Module", 
					TestModuleIdEnum.FM => "Test ID for FM Module", 
					_ => "Invalid TestModuleId Value", 
				};
			}

			public static string TestModuleIdToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x2 = Test ID for BT Module");
				stringBuilder.AppendLine("0x3 = Test ID for WLAN Module");
				stringBuilder.AppendLine("0x4 = Test ID for FM Module");
				return stringBuilder.ToString();
			}
		}

		private uint _TestModuleId;

		private uint _TestResult;

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

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

		public TestModuleIdEnum GetTestModuleIdAsTestModuleIdEnum()
		{
			return TestModuleIdInfo.ValueToTestModuleId(_TestModuleId);
		}

		public void SetTestModuleIdAsTestModuleIdEnum(TestModuleIdEnum enumItem)
		{
			TestModuleId = TestModuleIdInfo.TestModuleIdToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			TestModuleId = bitBuilder.RemoveBitsAsUint(8);
			TestResult = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(TestModuleId, 8);
			bitBuilder.AddBitsAsUintLittleEndian(TestResult, 16);
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 15u;

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

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Response;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint EventCode => 15u;

	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.NumberOfTestsDone = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.TestResultsList.Count; i++)
		{
			UserFields.TestResultsList[i].FromBitBuilder(ref bitBuilder);
		}
		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.AddBitsAsUint(UserFields.NumberOfTestsDone, 8);
		for (int i = 0; i < UserFields.TestResultsList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.TestResultsList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
