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

namespace QCRPG.TSL.BT.Packets;

public class LeTransmitterTest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PacketPayloadEnum
		{
			PSEUDO_RANDOM_BIT_SEQUENCE_9,
			ALTERNATING_BITS_11110000,
			ALTERNATING_BITS_10101010,
			PSEUDO_RANDOM_BIT_SEQUENCE_15,
			ALL_ONE_BITS,
			ALL_ZERO_BITS,
			ALTERNATING_BITS_00001111,
			ALTERNATING_BITS_0101,
			RESERVED,
			InvalidPacketPayloadValue
		}

		public static class PacketPayloadInfo
		{
			public static PacketPayloadEnum ValueToPacketPayload(uint value)
			{
				switch (value)
				{
				case 0u:
					return PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_9;
				case 1u:
					return PacketPayloadEnum.ALTERNATING_BITS_11110000;
				case 2u:
					return PacketPayloadEnum.ALTERNATING_BITS_10101010;
				case 3u:
					return PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_15;
				case 4u:
					return PacketPayloadEnum.ALL_ONE_BITS;
				case 5u:
					return PacketPayloadEnum.ALL_ZERO_BITS;
				case 6u:
					return PacketPayloadEnum.ALTERNATING_BITS_00001111;
				case 7u:
					return PacketPayloadEnum.ALTERNATING_BITS_0101;
				default:
					if (value >= 8 && value <= 255)
					{
						return PacketPayloadEnum.RESERVED;
					}
					return PacketPayloadEnum.InvalidPacketPayloadValue;
				}
			}

			public static uint PacketPayloadToValue(PacketPayloadEnum enumItem)
			{
				return enumItem switch
				{
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_9 => 0u, 
					PacketPayloadEnum.ALTERNATING_BITS_11110000 => 1u, 
					PacketPayloadEnum.ALTERNATING_BITS_10101010 => 2u, 
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_15 => 3u, 
					PacketPayloadEnum.ALL_ONE_BITS => 4u, 
					PacketPayloadEnum.ALL_ZERO_BITS => 5u, 
					PacketPayloadEnum.ALTERNATING_BITS_00001111 => 6u, 
					PacketPayloadEnum.ALTERNATING_BITS_0101 => 7u, 
					PacketPayloadEnum.RESERVED => 8u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketPayloadToString(PacketPayloadEnum enumItem)
			{
				return enumItem switch
				{
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_9 => "PRBS9 sequence '11111111100000111101...'", 
					PacketPayloadEnum.ALTERNATING_BITS_11110000 => "Repeated '11110000'", 
					PacketPayloadEnum.ALTERNATING_BITS_10101010 => "Repeated '10101010'", 
					PacketPayloadEnum.PSEUDO_RANDOM_BIT_SEQUENCE_15 => "PRBS15 sequence", 
					PacketPayloadEnum.ALL_ONE_BITS => "Repeated '11111111'", 
					PacketPayloadEnum.ALL_ZERO_BITS => "Repeated '00000000'", 
					PacketPayloadEnum.ALTERNATING_BITS_00001111 => "Repeated '00001111'", 
					PacketPayloadEnum.ALTERNATING_BITS_0101 => "Repeated '01010101'", 
					PacketPayloadEnum.RESERVED => "Reserved for future use", 
					_ => "Invalid PacketPayload Value", 
				};
			}

			public static string PacketPayloadToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = PRBS9 sequence '11111111100000111101...'");
				stringBuilder.AppendLine("0x1 = Repeated '11110000'");
				stringBuilder.AppendLine("0x2 = Repeated '10101010'");
				stringBuilder.AppendLine("0x3 = PRBS15 sequence");
				stringBuilder.AppendLine("0x4 = Repeated '11111111'");
				stringBuilder.AppendLine("0x5 = Repeated '00000000'");
				stringBuilder.AppendLine("0x6 = Repeated '00001111'");
				stringBuilder.AppendLine("0x7 = Repeated '01010101'");
				stringBuilder.AppendLine("0x8-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _TxFrequency;

		private uint _LengthOfTestData;

		private uint _PacketPayload;

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

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

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

		public PacketPayloadEnum GetPacketPayloadAsPacketPayloadEnum()
		{
			return PacketPayloadInfo.ValueToPacketPayload(_PacketPayload);
		}

		public void SetPacketPayloadAsPacketPayloadEnum(PacketPayloadEnum enumItem)
		{
			PacketPayload = PacketPayloadInfo.PacketPayloadToValue(enumItem);
		}
	}

	private const uint _Opcode = 8222u;

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

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

	public uint Opcode => 8222u;

	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;
		}
		UserFields.TxFrequency = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LengthOfTestData = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketPayload = 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.TxFrequency, 8);
		bitBuilder.AddBitsAsUint(UserFields.LengthOfTestData, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketPayload, 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.TxFrequency > 39)
		{
			list.Add("Maximum allowed value for TX Frequency is 0x27");
		}
		return list;
	}
}
