using System.Text;

namespace QCRPG.TSL.BT.Packets;

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

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

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

	public static string PayloadPatternToDescription()
	{
		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();
	}
}
