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

namespace QCRPG.TSL.FMHCI.Packets;

public class GpioConfiguration : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum GpioConfigEnum
		{
			GPIO_NOT_USED,
			ETP_READY_OR_TEST_RESULT,
			COMMAND_STATUS,
			RDS_ENABLE,
			InvalidGpioConfigValue
		}

		public static class GpioConfigInfo
		{
			public static GpioConfigEnum ValueToGpioConfig(uint value)
			{
				return value switch
				{
					0u => GpioConfigEnum.GPIO_NOT_USED, 
					1u => GpioConfigEnum.ETP_READY_OR_TEST_RESULT, 
					2u => GpioConfigEnum.COMMAND_STATUS, 
					3u => GpioConfigEnum.RDS_ENABLE, 
					_ => GpioConfigEnum.InvalidGpioConfigValue, 
				};
			}

			public static uint GpioConfigToValue(GpioConfigEnum enumItem)
			{
				return enumItem switch
				{
					GpioConfigEnum.GPIO_NOT_USED => 0u, 
					GpioConfigEnum.ETP_READY_OR_TEST_RESULT => 1u, 
					GpioConfigEnum.COMMAND_STATUS => 2u, 
					GpioConfigEnum.RDS_ENABLE => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string GpioConfigToString(GpioConfigEnum enumItem)
			{
				return enumItem switch
				{
					GpioConfigEnum.GPIO_NOT_USED => "GPIO not used", 
					GpioConfigEnum.ETP_READY_OR_TEST_RESULT => "ETP Ready / Test Result", 
					GpioConfigEnum.COMMAND_STATUS => "Command Status", 
					GpioConfigEnum.RDS_ENABLE => "RDS Enable", 
					_ => "Invalid GpioConfig Value", 
				};
			}

			public static string GpioConfigToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = GPIO not used");
				stringBuilder.AppendLine("0x1 = ETP Ready / Test Result");
				stringBuilder.AppendLine("0x2 = Command Status");
				stringBuilder.AppendLine("0x3 = RDS Enable");
				return stringBuilder.ToString();
			}
		}

		private uint _GpioNumber;

		private uint _GpioConfig;

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

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

		public GpioConfigEnum GetGpioConfigAsGpioConfigEnum()
		{
			return GpioConfigInfo.ValueToGpioConfig(_GpioConfig);
		}

		public void SetGpioConfigAsGpioConfigEnum(GpioConfigEnum enumItem)
		{
			GpioConfig = GpioConfigInfo.GpioConfigToValue(enumItem);
		}
	}

	private const uint _Opcode = 23561u;

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

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

	public uint Opcode => 23561u;

	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.GpioNumber = bitBuilder.RemoveBitsAsUint(8);
		UserFields.GpioConfig = 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.GpioNumber, 8);
		bitBuilder.AddBitsAsUint(UserFields.GpioConfig, 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.GpioConfig > 3)
		{
			list.Add("Maximum allowed value for GPIO Config is 0x3");
		}
		return list;
	}
}
