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

namespace QCRPG.TSL.FMHCI.Packets;

public class CommandCompleteForGetTransmitterConfiguration : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EmphasisEnum
		{
			TIME_CONSTANT_75US,
			TIME_CONSTANT_50US,
			InvalidEmphasisValue
		}

		public static class EmphasisInfo
		{
			public static EmphasisEnum ValueToEmphasis(uint value)
			{
				return value switch
				{
					0u => EmphasisEnum.TIME_CONSTANT_75US, 
					1u => EmphasisEnum.TIME_CONSTANT_50US, 
					_ => EmphasisEnum.InvalidEmphasisValue, 
				};
			}

			public static uint EmphasisToValue(EmphasisEnum enumItem)
			{
				return enumItem switch
				{
					EmphasisEnum.TIME_CONSTANT_75US => 0u, 
					EmphasisEnum.TIME_CONSTANT_50US => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EmphasisToString(EmphasisEnum enumItem)
			{
				return enumItem switch
				{
					EmphasisEnum.TIME_CONSTANT_75US => "75us time constant (the pre-emphasis/de-emphasis timing typically used in the US and Japan)", 
					EmphasisEnum.TIME_CONSTANT_50US => "50us time constant (the pre-emphasis/de-emphasis timing typically used outside of the US and Japan)", 
					_ => "Invalid Emphasis Value", 
				};
			}

			public static string EmphasisToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = 75us time constant (the pre-emphasis/de-emphasis timing typically used in the US and Japan)");
				stringBuilder.AppendLine("0x1 = 50us time constant (the pre-emphasis/de-emphasis timing typically used outside of the US and Japan)");
				return stringBuilder.ToString();
			}
		}

		public enum RdsStandardEnum
		{
			RBDS_MODE,
			RDS_MODE,
			NO_RDS_RBDS_DECODING,
			InvalidRdsStandardValue
		}

		public static class RdsStandardInfo
		{
			public static RdsStandardEnum ValueToRdsStandard(uint value)
			{
				return value switch
				{
					0u => RdsStandardEnum.RBDS_MODE, 
					1u => RdsStandardEnum.RDS_MODE, 
					2u => RdsStandardEnum.NO_RDS_RBDS_DECODING, 
					_ => RdsStandardEnum.InvalidRdsStandardValue, 
				};
			}

			public static uint RdsStandardToValue(RdsStandardEnum enumItem)
			{
				return enumItem switch
				{
					RdsStandardEnum.RBDS_MODE => 0u, 
					RdsStandardEnum.RDS_MODE => 1u, 
					RdsStandardEnum.NO_RDS_RBDS_DECODING => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RdsStandardToString(RdsStandardEnum enumItem)
			{
				return enumItem switch
				{
					RdsStandardEnum.RBDS_MODE => "RBDS mode (typically used in the US)", 
					RdsStandardEnum.RDS_MODE => "RBDS mode (typically used outside of the US)", 
					RdsStandardEnum.NO_RDS_RBDS_DECODING => "No RDS/RBDS decoding", 
					_ => "Invalid RdsStandard Value", 
				};
			}

			public static string RdsStandardToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = RBDS mode (typically used in the US)");
				stringBuilder.AppendLine("0x1 = RBDS mode (typically used outside of the US)");
				stringBuilder.AppendLine("0x2 = No RDS/RBDS decoding");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 20483u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _Emphasis;

		private uint _RdsStandard;

		private uint _BandLowLimit;

		private uint _BandHighLimit;

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

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

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

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

		public uint BandLowLimit
		{
			get
			{
				return _BandLowLimit;
			}
			set
			{
				_BandLowLimit = value;
			}
		}

		public uint BandHighLimit
		{
			get
			{
				return _BandHighLimit;
			}
			set
			{
				_BandHighLimit = value;
			}
		}

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

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

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

		public EmphasisEnum GetEmphasisAsEmphasisEnum()
		{
			return EmphasisInfo.ValueToEmphasis(_Emphasis);
		}

		public void SetEmphasisAsEmphasisEnum(EmphasisEnum enumItem)
		{
			Emphasis = EmphasisInfo.EmphasisToValue(enumItem);
		}

		public RdsStandardEnum GetRdsStandardAsRdsStandardEnum()
		{
			return RdsStandardInfo.ValueToRdsStandard(_RdsStandard);
		}

		public void SetRdsStandardAsRdsStandardEnum(RdsStandardEnum enumItem)
		{
			RdsStandard = RdsStandardInfo.RdsStandardToValue(enumItem);
		}
	}

	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 CommandCompleteForGetTransmitterConfigurationInfo(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;
		UserFields.BandLowLimit = 87500u;
		UserFields.BandHighLimit = 108000u;
	}

	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.Emphasis = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RdsStandard = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BandLowLimit = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.BandHighLimit = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		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.Emphasis, 8);
		bitBuilder.AddBitsAsUint(UserFields.RdsStandard, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.BandLowLimit, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.BandHighLimit, 32);
		_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.Emphasis > 1)
		{
			list.Add("Maximum allowed value for Emphasis is 0x1");
		}
		if (UserFields.RdsStandard > 2)
		{
			list.Add("Maximum allowed value for RDS Standard is 0x2");
		}
		return list;
	}
}
