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

namespace QCRPG.TSL.FMHCI.Packets;

public class SearchStationList : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SearchListModeEnum
		{
			RETURN_ALL_MEETING_SIGNAL_LEVEL,
			RETURN_ALL_NOT_MEETING_SIGNAL_LEVEL,
			RETURN_STRONGEST_MEETING_SIGNAL_LEVEL,
			RETURN_WEAKEST_MEETING_SIGNAL_LEVEL,
			RETURN_ALL_MEETING_SIGNAL_LEVEL_AND_MATCHING_PROGRAM_TYPE,
			InvalidSearchListModeValue
		}

		public static class SearchListModeInfo
		{
			public static SearchListModeEnum ValueToSearchListMode(uint value)
			{
				return value switch
				{
					0u => SearchListModeEnum.RETURN_ALL_MEETING_SIGNAL_LEVEL, 
					1u => SearchListModeEnum.RETURN_ALL_NOT_MEETING_SIGNAL_LEVEL, 
					2u => SearchListModeEnum.RETURN_STRONGEST_MEETING_SIGNAL_LEVEL, 
					3u => SearchListModeEnum.RETURN_WEAKEST_MEETING_SIGNAL_LEVEL, 
					4u => SearchListModeEnum.RETURN_ALL_MEETING_SIGNAL_LEVEL_AND_MATCHING_PROGRAM_TYPE, 
					_ => SearchListModeEnum.InvalidSearchListModeValue, 
				};
			}

			public static uint SearchListModeToValue(SearchListModeEnum enumItem)
			{
				return enumItem switch
				{
					SearchListModeEnum.RETURN_ALL_MEETING_SIGNAL_LEVEL => 0u, 
					SearchListModeEnum.RETURN_ALL_NOT_MEETING_SIGNAL_LEVEL => 1u, 
					SearchListModeEnum.RETURN_STRONGEST_MEETING_SIGNAL_LEVEL => 2u, 
					SearchListModeEnum.RETURN_WEAKEST_MEETING_SIGNAL_LEVEL => 3u, 
					SearchListModeEnum.RETURN_ALL_MEETING_SIGNAL_LEVEL_AND_MATCHING_PROGRAM_TYPE => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SearchListModeToString(SearchListModeEnum enumItem)
			{
				return enumItem switch
				{
					SearchListModeEnum.RETURN_ALL_MEETING_SIGNAL_LEVEL => "FM driver will return a list of stations that meet a signal-level requirement (arranged in descending order by strength)", 
					SearchListModeEnum.RETURN_ALL_NOT_MEETING_SIGNAL_LEVEL => "FM driver will return a list of stations that meet a signal-level requirement (arranged in descending order by strength)", 
					SearchListModeEnum.RETURN_STRONGEST_MEETING_SIGNAL_LEVEL => "FM driver will return a list of the strongest stations in the band that meet a signal-level requirement (arranged in descending order by strength)", 
					SearchListModeEnum.RETURN_WEAKEST_MEETING_SIGNAL_LEVEL => "FM driver will return a list of the weakest stations in the band that meet a signal-level requirement (arranged in ascending order by strength)", 
					SearchListModeEnum.RETURN_ALL_MEETING_SIGNAL_LEVEL_AND_MATCHING_PROGRAM_TYPE => "FM driver will return a list of stations in the band that meet a signal-level requirement and are broadcasting the specified RDS/RBDS program type (arranged in descending order by strength)", 
					_ => "Invalid SearchListMode Value", 
				};
			}

			public static string SearchListModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = FM driver will return a list of stations that meet a signal-level requirement (arranged in descending order by strength)");
				stringBuilder.AppendLine("0x1 = FM driver will return a list of stations that meet a signal-level requirement (arranged in descending order by strength)");
				stringBuilder.AppendLine("0x2 = FM driver will return a list of the strongest stations in the band that meet a signal-level requirement (arranged in descending order by strength)");
				stringBuilder.AppendLine("0x3 = FM driver will return a list of the weakest stations in the band that meet a signal-level requirement (arranged in ascending order by strength)");
				stringBuilder.AppendLine("0x4 = FM driver will return a list of stations in the band that meet a signal-level requirement and are broadcasting the specified RDS/RBDS program type (arranged in descending order by strength)");
				return stringBuilder.ToString();
			}
		}

		public enum SearchListDirectionEnum
		{
			UP,
			DOWN,
			InvalidSearchListDirectionValue
		}

		public static class SearchListDirectionInfo
		{
			public static SearchListDirectionEnum ValueToSearchListDirection(uint value)
			{
				return value switch
				{
					0u => SearchListDirectionEnum.UP, 
					1u => SearchListDirectionEnum.DOWN, 
					_ => SearchListDirectionEnum.InvalidSearchListDirectionValue, 
				};
			}

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

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

			public static string SearchListDirectionToDescription()
			{
				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 _SearchListMode;

		private uint _SearchListDirection;

		private uint _SearchListMax;

		private uint _ProgramType;

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

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

		public uint SearchListMax
		{
			get
			{
				return _SearchListMax;
			}
			set
			{
				_SearchListMax = value;
			}
		}

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

		public SearchListModeEnum GetSearchListModeAsSearchListModeEnum()
		{
			return SearchListModeInfo.ValueToSearchListMode(_SearchListMode);
		}

		public void SetSearchListModeAsSearchListModeEnum(SearchListModeEnum enumItem)
		{
			SearchListMode = SearchListModeInfo.SearchListModeToValue(enumItem);
		}

		public SearchListDirectionEnum GetSearchListDirectionAsSearchListDirectionEnum()
		{
			return SearchListDirectionInfo.ValueToSearchListDirection(_SearchListDirection);
		}

		public void SetSearchListDirectionAsSearchListDirectionEnum(SearchListDirectionEnum enumItem)
		{
			SearchListDirection = SearchListDirectionInfo.SearchListDirectionToValue(enumItem);
		}
	}

	private const uint _Opcode = 19472u;

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

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

	public uint Opcode => 19472u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.SearchListMax = 20u;
	}

	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.SearchListMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SearchListDirection = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SearchListMax = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.ProgramType = 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.SearchListMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.SearchListDirection, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SearchListMax, 32);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 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.SearchListMode > 4)
		{
			list.Add("Maximum allowed value for Search List Mode is 0x4");
		}
		if (UserFields.SearchListDirection > 1)
		{
			list.Add("Maximum allowed value for Search List Direction is 0x1");
		}
		if (UserFields.SearchListMax > 20)
		{
			list.Add("Maximum allowed value for Search List Max is 0x14");
		}
		return list;
	}
}
