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

namespace QCRPG.TSL.FMHCI.Packets;

public class SearchRdsStations : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SearchModeEnum
		{
			TUNE_TO_NEXT_MATCHING_PROGRAM_TYPE,
			TUNE_TO_ALL_MATCHING_PROGRAM_TYPE,
			TUNE_TO_NEXT_MATCHING_SIGNAL_AND_PROGRAM_IDENTIFICATION,
			TUNE_TO_ALTERNATIVE_IF_STRONGER,
			InvalidSearchModeValue
		}

		public static class SearchModeInfo
		{
			public static SearchModeEnum ValueToSearchMode(uint value)
			{
				return value switch
				{
					0u => SearchModeEnum.TUNE_TO_NEXT_MATCHING_PROGRAM_TYPE, 
					1u => SearchModeEnum.TUNE_TO_ALL_MATCHING_PROGRAM_TYPE, 
					2u => SearchModeEnum.TUNE_TO_NEXT_MATCHING_SIGNAL_AND_PROGRAM_IDENTIFICATION, 
					3u => SearchModeEnum.TUNE_TO_ALTERNATIVE_IF_STRONGER, 
					_ => SearchModeEnum.InvalidSearchModeValue, 
				};
			}

			public static uint SearchModeToValue(SearchModeEnum enumItem)
			{
				return enumItem switch
				{
					SearchModeEnum.TUNE_TO_NEXT_MATCHING_PROGRAM_TYPE => 0u, 
					SearchModeEnum.TUNE_TO_ALL_MATCHING_PROGRAM_TYPE => 1u, 
					SearchModeEnum.TUNE_TO_NEXT_MATCHING_SIGNAL_AND_PROGRAM_IDENTIFICATION => 2u, 
					SearchModeEnum.TUNE_TO_ALTERNATIVE_IF_STRONGER => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SearchModeToString(SearchModeEnum enumItem)
			{
				return enumItem switch
				{
					SearchModeEnum.TUNE_TO_NEXT_MATCHING_PROGRAM_TYPE => "FM driver will tune/seek to the next station that meets a signal-level requirement and matches the specified RDS/RBDS program type (Example: 'News')", 
					SearchModeEnum.TUNE_TO_ALL_MATCHING_PROGRAM_TYPE => "FM driver will tune to all stations that meet a signal-level requirement and match the specified RDS/RBDS program type (Example: 'News')", 
					SearchModeEnum.TUNE_TO_NEXT_MATCHING_SIGNAL_AND_PROGRAM_IDENTIFICATION => "FM driver will tune to the next station that meets a signal-level requirement and matches the specified RDS/RBDS program identification", 
					SearchModeEnum.TUNE_TO_ALTERNATIVE_IF_STRONGER => "If the known alternative frequency is stronger than the current station, the FM driver will tune to this alternative frequency", 
					_ => "Invalid SearchMode Value", 
				};
			}

			public static string SearchModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = FM driver will tune/seek to the next station that meets a signal-level requirement and matches the specified RDS/RBDS program type (Example: 'News')");
				stringBuilder.AppendLine("0x1 = FM driver will tune to all stations that meet a signal-level requirement and match the specified RDS/RBDS program type (Example: 'News')");
				stringBuilder.AppendLine("0x2 = FM driver will tune to the next station that meets a signal-level requirement and matches the specified RDS/RBDS program identification");
				stringBuilder.AppendLine("0x3 = If the known alternative frequency is stronger than the current station, the FM driver will tune to this alternative frequency");
				return stringBuilder.ToString();
			}
		}

		public enum ScanTimeEnum
		{
			NO_DWELL,
			TUNE_FOR_1_SECOND,
			TUNE_FOR_2_SECONDS,
			TUNE_FOR_3_SECONDS,
			TUNE_FOR_4_SECONDS,
			TUNE_FOR_5_SECONDS,
			TUNE_FOR_6_SECONDS,
			TUNE_FOR_7_SECONDS,
			TUNE_FOR_8_SECONDS,
			TUNE_FOR_9_SECONDS,
			TUNE_FOR_10_SECONDS,
			TUNE_FOR_11_SECONDS,
			TUNE_FOR_12_SECONDS,
			TUNE_FOR_13_SECONDS,
			TUNE_FOR_14_SECONDS,
			TUNE_FOR_15_SECONDS,
			InvalidScanTimeValue
		}

		public static class ScanTimeInfo
		{
			public static ScanTimeEnum ValueToScanTime(uint value)
			{
				return value switch
				{
					0u => ScanTimeEnum.NO_DWELL, 
					1u => ScanTimeEnum.TUNE_FOR_1_SECOND, 
					2u => ScanTimeEnum.TUNE_FOR_2_SECONDS, 
					3u => ScanTimeEnum.TUNE_FOR_3_SECONDS, 
					4u => ScanTimeEnum.TUNE_FOR_4_SECONDS, 
					5u => ScanTimeEnum.TUNE_FOR_5_SECONDS, 
					6u => ScanTimeEnum.TUNE_FOR_6_SECONDS, 
					7u => ScanTimeEnum.TUNE_FOR_7_SECONDS, 
					8u => ScanTimeEnum.TUNE_FOR_8_SECONDS, 
					9u => ScanTimeEnum.TUNE_FOR_9_SECONDS, 
					10u => ScanTimeEnum.TUNE_FOR_10_SECONDS, 
					11u => ScanTimeEnum.TUNE_FOR_11_SECONDS, 
					12u => ScanTimeEnum.TUNE_FOR_12_SECONDS, 
					13u => ScanTimeEnum.TUNE_FOR_13_SECONDS, 
					14u => ScanTimeEnum.TUNE_FOR_14_SECONDS, 
					15u => ScanTimeEnum.TUNE_FOR_15_SECONDS, 
					_ => ScanTimeEnum.InvalidScanTimeValue, 
				};
			}

			public static uint ScanTimeToValue(ScanTimeEnum enumItem)
			{
				return enumItem switch
				{
					ScanTimeEnum.NO_DWELL => 0u, 
					ScanTimeEnum.TUNE_FOR_1_SECOND => 1u, 
					ScanTimeEnum.TUNE_FOR_2_SECONDS => 2u, 
					ScanTimeEnum.TUNE_FOR_3_SECONDS => 3u, 
					ScanTimeEnum.TUNE_FOR_4_SECONDS => 4u, 
					ScanTimeEnum.TUNE_FOR_5_SECONDS => 5u, 
					ScanTimeEnum.TUNE_FOR_6_SECONDS => 6u, 
					ScanTimeEnum.TUNE_FOR_7_SECONDS => 7u, 
					ScanTimeEnum.TUNE_FOR_8_SECONDS => 8u, 
					ScanTimeEnum.TUNE_FOR_9_SECONDS => 9u, 
					ScanTimeEnum.TUNE_FOR_10_SECONDS => 10u, 
					ScanTimeEnum.TUNE_FOR_11_SECONDS => 11u, 
					ScanTimeEnum.TUNE_FOR_12_SECONDS => 12u, 
					ScanTimeEnum.TUNE_FOR_13_SECONDS => 13u, 
					ScanTimeEnum.TUNE_FOR_14_SECONDS => 14u, 
					ScanTimeEnum.TUNE_FOR_15_SECONDS => 15u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ScanTimeToString(ScanTimeEnum enumItem)
			{
				return enumItem switch
				{
					ScanTimeEnum.NO_DWELL => "FM driver will not dwell during a scan", 
					ScanTimeEnum.TUNE_FOR_1_SECOND => "FM driver will remain tuned to a station for 1 second during a scan", 
					ScanTimeEnum.TUNE_FOR_2_SECONDS => "FM driver will remain tuned to a station for 2 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_3_SECONDS => "FM driver will remain tuned to a station for 3 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_4_SECONDS => "FM driver will remain tuned to a station for 4 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_5_SECONDS => "FM driver will remain tuned to a station for 5 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_6_SECONDS => "FM driver will remain tuned to a station for 6 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_7_SECONDS => "FM driver will remain tuned to a station for 7 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_8_SECONDS => "FM driver will remain tuned to a station for 8 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_9_SECONDS => "FM driver will remain tuned to a station for 9 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_10_SECONDS => "FM driver will remain tuned to a station for 10 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_11_SECONDS => "FM driver will remain tuned to a station for 11 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_12_SECONDS => "FM driver will remain tuned to a station for 12 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_13_SECONDS => "FM driver will remain tuned to a station for 13 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_14_SECONDS => "FM driver will remain tuned to a station for 14 seconds during a scan", 
					ScanTimeEnum.TUNE_FOR_15_SECONDS => "FM driver will remain tuned to a station for 15 seconds during a scan", 
					_ => "Invalid ScanTime Value", 
				};
			}

			public static string ScanTimeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = FM driver will not dwell during a scan");
				stringBuilder.AppendLine("0x1 = FM driver will remain tuned to a station for 1 second during a scan");
				stringBuilder.AppendLine("0x2 = FM driver will remain tuned to a station for 2 seconds during a scan");
				stringBuilder.AppendLine("0x3 = FM driver will remain tuned to a station for 3 seconds during a scan");
				stringBuilder.AppendLine("0x4 = FM driver will remain tuned to a station for 4 seconds during a scan");
				stringBuilder.AppendLine("0x5 = FM driver will remain tuned to a station for 5 seconds during a scan");
				stringBuilder.AppendLine("0x6 = FM driver will remain tuned to a station for 6 seconds during a scan");
				stringBuilder.AppendLine("0x7 = FM driver will remain tuned to a station for 7 seconds during a scan");
				stringBuilder.AppendLine("0x8 = FM driver will remain tuned to a station for 8 seconds during a scan");
				stringBuilder.AppendLine("0x9 = FM driver will remain tuned to a station for 9 seconds during a scan");
				stringBuilder.AppendLine("0xA = FM driver will remain tuned to a station for 10 seconds during a scan");
				stringBuilder.AppendLine("0xB = FM driver will remain tuned to a station for 11 seconds during a scan");
				stringBuilder.AppendLine("0xC = FM driver will remain tuned to a station for 12 seconds during a scan");
				stringBuilder.AppendLine("0xD = FM driver will remain tuned to a station for 13 seconds during a scan");
				stringBuilder.AppendLine("0xE = FM driver will remain tuned to a station for 14 seconds during a scan");
				stringBuilder.AppendLine("0xF = FM driver will remain tuned to a station for 15 seconds during a scan");
				return stringBuilder.ToString();
			}
		}

		public enum SearchDirectionEnum
		{
			UP,
			DOWN,
			InvalidSearchDirectionValue
		}

		public static class SearchDirectionInfo
		{
			public static SearchDirectionEnum ValueToSearchDirection(uint value)
			{
				return value switch
				{
					0u => SearchDirectionEnum.UP, 
					1u => SearchDirectionEnum.DOWN, 
					_ => SearchDirectionEnum.InvalidSearchDirectionValue, 
				};
			}

			public static uint SearchDirectionToValue(SearchDirectionEnum enumItem)
			{
				return enumItem switch
				{
					SearchDirectionEnum.UP => 0u, 
					SearchDirectionEnum.DOWN => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SearchDirectionToString(SearchDirectionEnum enumItem)
			{
				return enumItem switch
				{
					SearchDirectionEnum.UP => "Each frequency searched will be greater than the previous frequency (except when wrapping around the band 88.1 MHz -> 88.3 MHz)", 
					SearchDirectionEnum.DOWN => "Each frequency searched will be less than the previous frequency (except when wrapping around the band 88.1 MHz -> 87.9 MHz)", 
					_ => "Invalid SearchDirection Value", 
				};
			}

			public static string SearchDirectionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Each frequency searched will be greater than the previous frequency (except when wrapping around the band 88.1 MHz -> 88.3 MHz)");
				stringBuilder.AppendLine("0x1 = Each frequency searched will be less than the previous frequency (except when wrapping around the band 88.1 MHz -> 87.9 MHz)");
				return stringBuilder.ToString();
			}
		}

		private uint _SearchMode;

		private uint _ScanTime;

		private uint _SearchDirection;

		private uint _ProgramType;

		private uint _ProgramIdentification;

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

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

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

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

		public uint ProgramIdentification
		{
			get
			{
				return _ProgramIdentification;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for ProgramIdentification is 0xFFFF");
				}
				_ProgramIdentification = value;
			}
		}

		public SearchModeEnum GetSearchModeAsSearchModeEnum()
		{
			return SearchModeInfo.ValueToSearchMode(_SearchMode);
		}

		public void SetSearchModeAsSearchModeEnum(SearchModeEnum enumItem)
		{
			SearchMode = SearchModeInfo.SearchModeToValue(enumItem);
		}

		public ScanTimeEnum GetScanTimeAsScanTimeEnum()
		{
			return ScanTimeInfo.ValueToScanTime(_ScanTime);
		}

		public void SetScanTimeAsScanTimeEnum(ScanTimeEnum enumItem)
		{
			ScanTime = ScanTimeInfo.ScanTimeToValue(enumItem);
		}

		public SearchDirectionEnum GetSearchDirectionAsSearchDirectionEnum()
		{
			return SearchDirectionInfo.ValueToSearchDirection(_SearchDirection);
		}

		public void SetSearchDirectionAsSearchDirectionEnum(SearchDirectionEnum enumItem)
		{
			SearchDirection = SearchDirectionInfo.SearchDirectionToValue(enumItem);
		}
	}

	private const uint _Opcode = 19471u;

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

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

	public uint Opcode => 19471u;

	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.SearchMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ScanTime = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SearchDirection = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ProgramType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ProgramIdentification = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.SearchMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.ScanTime, 8);
		bitBuilder.AddBitsAsUint(UserFields.SearchDirection, 8);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ProgramIdentification, 16);
		_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.SearchMode > 3)
		{
			list.Add("Maximum allowed value for Search Mode is 0x3");
		}
		if (UserFields.ScanTime > 15)
		{
			list.Add("Maximum allowed value for Scan Time is 0xF");
		}
		if (UserFields.SearchDirection > 1)
		{
			list.Add("Maximum allowed value for Search Direction is 0x1");
		}
		return list;
	}
}
