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

namespace QCRPG.TSL.BT.Packets;

public class LtLeCleTest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum CleTestModeEnum
		{
			SET_PARA,
			CLE_SEND,
			CLE_RX,
			CLE_NORMAL,
			PAST_RX,
			InvalidCleTestModeValue
		}

		public static class CleTestModeInfo
		{
			public static CleTestModeEnum ValueToCleTestMode(uint value)
			{
				return value switch
				{
					0u => CleTestModeEnum.SET_PARA, 
					1u => CleTestModeEnum.CLE_SEND, 
					2u => CleTestModeEnum.CLE_RX, 
					3u => CleTestModeEnum.CLE_NORMAL, 
					4u => CleTestModeEnum.PAST_RX, 
					_ => CleTestModeEnum.InvalidCleTestModeValue, 
				};
			}

			public static uint CleTestModeToValue(CleTestModeEnum enumItem)
			{
				return enumItem switch
				{
					CleTestModeEnum.SET_PARA => 0u, 
					CleTestModeEnum.CLE_SEND => 1u, 
					CleTestModeEnum.CLE_RX => 2u, 
					CleTestModeEnum.CLE_NORMAL => 3u, 
					CleTestModeEnum.PAST_RX => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string CleTestModeToString(CleTestModeEnum enumItem)
			{
				return enumItem switch
				{
					CleTestModeEnum.SET_PARA => "Set CLE test parameters", 
					CleTestModeEnum.CLE_SEND => "Trigger a CLE packet sent", 
					CleTestModeEnum.CLE_RX => "Start CLE packets Rx test mode", 
					CleTestModeEnum.CLE_NORMAL => "Start normal rx mode", 
					CleTestModeEnum.PAST_RX => "Start PAST Rx test mode", 
					_ => "Invalid CleTestMode Value", 
				};
			}

			public static string CleTestModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Set CLE test parameters");
				stringBuilder.AppendLine("0x1 = Trigger a CLE packet sent");
				stringBuilder.AppendLine("0x2 = Start CLE packets Rx test mode");
				stringBuilder.AppendLine("0x3 = Start normal rx mode");
				stringBuilder.AppendLine("0x4 = Start PAST Rx test mode");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 15u;

		private uint _CleTestMode;

		private uint _ClePara1;

		private uint _ClePara2;

		public uint SubOpcode => 15u;

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

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

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

		public CleTestModeEnum GetCleTestModeAsCleTestModeEnum()
		{
			return CleTestModeInfo.ValueToCleTestMode(_CleTestMode);
		}

		public void SetCleTestModeAsCleTestModeEnum(CleTestModeEnum enumItem)
		{
			CleTestMode = CleTestModeInfo.CleTestModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64536u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLowerTester),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64536u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ClePara1 = 0u;
		UserFields.ClePara2 = 0u;
	}

	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.CleTestMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ClePara1 = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ClePara2 = 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.AddBitsAsUint(UserFields.CleTestMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.ClePara1, 8);
		bitBuilder.AddBitsAsUint(UserFields.ClePara2, 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.ClePara1 < 0)
		{
			list.Add("Minimum allowed value for CLE_Para1 is 0x0");
		}
		if (UserFields.ClePara1 > 251)
		{
			list.Add("Maximum allowed value for CLE_Para1 is 0xFB");
		}
		if (UserFields.ClePara2 < 0)
		{
			list.Add("Minimum allowed value for CLE_Para2 is 0x0");
		}
		if (UserFields.ClePara2 > 251)
		{
			list.Add("Maximum allowed value for CLE_Para2 is 0xFB");
		}
		return list;
	}
}
