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

namespace QCRPG.TSL.FMHCI.Packets;

public class TuneStatus : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubEventEnum
		{
			HOST_TUNE,
			SCAN_TUNE,
			SEARCH_COMPLETE,
			AF_JUMP,
			UNSOLICITED,
			InvalidSubEventValue
		}

		public static class SubEventInfo
		{
			public static SubEventEnum ValueToSubEvent(uint value)
			{
				return value switch
				{
					0u => SubEventEnum.HOST_TUNE, 
					1u => SubEventEnum.SCAN_TUNE, 
					2u => SubEventEnum.SEARCH_COMPLETE, 
					3u => SubEventEnum.AF_JUMP, 
					4u => SubEventEnum.UNSOLICITED, 
					_ => SubEventEnum.InvalidSubEventValue, 
				};
			}

			public static uint SubEventToValue(SubEventEnum enumItem)
			{
				return enumItem switch
				{
					SubEventEnum.HOST_TUNE => 0u, 
					SubEventEnum.SCAN_TUNE => 1u, 
					SubEventEnum.SEARCH_COMPLETE => 2u, 
					SubEventEnum.AF_JUMP => 3u, 
					SubEventEnum.UNSOLICITED => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SubEventToString(SubEventEnum enumItem)
			{
				return enumItem switch
				{
					SubEventEnum.HOST_TUNE => "HOST_TUNE", 
					SubEventEnum.SCAN_TUNE => "SCAN_TUNE", 
					SubEventEnum.SEARCH_COMPLETE => "SEARCH_COMPLETE", 
					SubEventEnum.AF_JUMP => "AF_JUMP", 
					SubEventEnum.UNSOLICITED => "UNSOLICITED", 
					_ => "Invalid SubEvent Value", 
				};
			}

			public static string SubEventToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = HOST_TUNE");
				stringBuilder.AppendLine("0x1 = SCAN_TUNE");
				stringBuilder.AppendLine("0x2 = SEARCH_COMPLETE");
				stringBuilder.AppendLine("0x3 = AF_JUMP");
				stringBuilder.AppendLine("0x4 = UNSOLICITED");
				return stringBuilder.ToString();
			}
		}

		public enum ServiceAvailableEnum
		{
			NO_STATIONS_FOUND,
			AT_LEAST_ONE_STATION_FOUND,
			InvalidServiceAvailableValue
		}

		public static class ServiceAvailableInfo
		{
			public static ServiceAvailableEnum ValueToServiceAvailable(uint value)
			{
				return value switch
				{
					0u => ServiceAvailableEnum.NO_STATIONS_FOUND, 
					1u => ServiceAvailableEnum.AT_LEAST_ONE_STATION_FOUND, 
					_ => ServiceAvailableEnum.InvalidServiceAvailableValue, 
				};
			}

			public static uint ServiceAvailableToValue(ServiceAvailableEnum enumItem)
			{
				return enumItem switch
				{
					ServiceAvailableEnum.NO_STATIONS_FOUND => 0u, 
					ServiceAvailableEnum.AT_LEAST_ONE_STATION_FOUND => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ServiceAvailableToString(ServiceAvailableEnum enumItem)
			{
				return enumItem switch
				{
					ServiceAvailableEnum.NO_STATIONS_FOUND => "No stations were found in the FM band", 
					ServiceAvailableEnum.AT_LEAST_ONE_STATION_FOUND => "At least one station was found in the FM band", 
					_ => "Invalid ServiceAvailable Value", 
				};
			}

			public static string ServiceAvailableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No stations were found in the FM band");
				stringBuilder.AppendLine("0x1 = At least one station was found in the FM band");
				return stringBuilder.ToString();
			}
		}

		public enum StereoProgramEnum
		{
			MONO,
			STEREO,
			InvalidStereoProgramValue
		}

		public static class StereoProgramInfo
		{
			public static StereoProgramEnum ValueToStereoProgram(uint value)
			{
				return value switch
				{
					0u => StereoProgramEnum.MONO, 
					1u => StereoProgramEnum.STEREO, 
					_ => StereoProgramEnum.InvalidStereoProgramValue, 
				};
			}

			public static uint StereoProgramToValue(StereoProgramEnum enumItem)
			{
				return enumItem switch
				{
					StereoProgramEnum.MONO => 0u, 
					StereoProgramEnum.STEREO => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string StereoProgramToString(StereoProgramEnum enumItem)
			{
				return enumItem switch
				{
					StereoProgramEnum.MONO => "MONO", 
					StereoProgramEnum.STEREO => "STEREO", 
					_ => "Invalid StereoProgram Value", 
				};
			}

			public static string StereoProgramToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = MONO");
				stringBuilder.AppendLine("0x1 = STEREO");
				return stringBuilder.ToString();
			}
		}

		public enum RdsSyncStatusEnum
		{
			NO_RDS_RBDS_DECODING_POSSIBLE,
			RDS_RBDS_DECODING_POSSIBLE,
			InvalidRdsSyncStatusValue
		}

		public static class RdsSyncStatusInfo
		{
			public static RdsSyncStatusEnum ValueToRdsSyncStatus(uint value)
			{
				return value switch
				{
					0u => RdsSyncStatusEnum.NO_RDS_RBDS_DECODING_POSSIBLE, 
					1u => RdsSyncStatusEnum.RDS_RBDS_DECODING_POSSIBLE, 
					_ => RdsSyncStatusEnum.InvalidRdsSyncStatusValue, 
				};
			}

			public static uint RdsSyncStatusToValue(RdsSyncStatusEnum enumItem)
			{
				return enumItem switch
				{
					RdsSyncStatusEnum.NO_RDS_RBDS_DECODING_POSSIBLE => 0u, 
					RdsSyncStatusEnum.RDS_RBDS_DECODING_POSSIBLE => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RdsSyncStatusToString(RdsSyncStatusEnum enumItem)
			{
				return enumItem switch
				{
					RdsSyncStatusEnum.NO_RDS_RBDS_DECODING_POSSIBLE => "NO_RDS_RBDS_DECODING_POSSIBLE", 
					RdsSyncStatusEnum.RDS_RBDS_DECODING_POSSIBLE => "RDS_RBDS_DECODING_POSSIBLE", 
					_ => "Invalid RdsSyncStatus Value", 
				};
			}

			public static string RdsSyncStatusToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = NO_RDS_RBDS_DECODING_POSSIBLE");
				stringBuilder.AppendLine("0x1 = RDS_RBDS_DECODING_POSSIBLE");
				return stringBuilder.ToString();
			}
		}

		private uint _SubEvent;

		private uint _FrequencyTuned;

		private uint _ServiceAvailable;

		private uint _Rssi;

		private uint _StereoProgram;

		private uint _RdsSyncStatus;

		private uint _MuteMode;

		private uint _Sinr;

		private uint _InterferenceDetectorThreshold;

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

		public uint FrequencyTuned
		{
			get
			{
				return _FrequencyTuned;
			}
			set
			{
				_FrequencyTuned = value;
			}
		}

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

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

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

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

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

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

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

		public SubEventEnum GetSubEventAsSubEventEnum()
		{
			return SubEventInfo.ValueToSubEvent(_SubEvent);
		}

		public void SetSubEventAsSubEventEnum(SubEventEnum enumItem)
		{
			SubEvent = SubEventInfo.SubEventToValue(enumItem);
		}

		public ServiceAvailableEnum GetServiceAvailableAsServiceAvailableEnum()
		{
			return ServiceAvailableInfo.ValueToServiceAvailable(_ServiceAvailable);
		}

		public void SetServiceAvailableAsServiceAvailableEnum(ServiceAvailableEnum enumItem)
		{
			ServiceAvailable = ServiceAvailableInfo.ServiceAvailableToValue(enumItem);
		}

		public StereoProgramEnum GetStereoProgramAsStereoProgramEnum()
		{
			return StereoProgramInfo.ValueToStereoProgram(_StereoProgram);
		}

		public void SetStereoProgramAsStereoProgramEnum(StereoProgramEnum enumItem)
		{
			StereoProgram = StereoProgramInfo.StereoProgramToValue(enumItem);
		}

		public RdsSyncStatusEnum GetRdsSyncStatusAsRdsSyncStatusEnum()
		{
			return RdsSyncStatusInfo.ValueToRdsSyncStatus(_RdsSyncStatus);
		}

		public void SetRdsSyncStatusAsRdsSyncStatusEnum(RdsSyncStatusEnum enumItem)
		{
			RdsSyncStatus = RdsSyncStatusInfo.RdsSyncStatusToValue(enumItem);
		}

		public MuteModeEnum GetMuteModeAsMuteModeEnum()
		{
			return MuteModeInfo.ValueToMuteMode(_MuteMode);
		}

		public void SetMuteModeAsMuteModeEnum(MuteModeEnum enumItem)
		{
			MuteMode = MuteModeInfo.MuteModeToValue(enumItem);
		}
	}

	private const uint _EventCode = 1u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new TuneStatusInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 1u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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.SubEvent = bitBuilder.RemoveBitsAsUint(8);
		UserFields.FrequencyTuned = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.ServiceAvailable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Rssi = bitBuilder.RemoveBitsAsUint(8);
		UserFields.StereoProgram = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RdsSyncStatus = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MuteMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Sinr = bitBuilder.RemoveBitsAsUint(8);
		UserFields.InterferenceDetectorThreshold = bitBuilder.RemoveBitsAsUint(8);
		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.SubEvent, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.FrequencyTuned, 32);
		bitBuilder.AddBitsAsUint(UserFields.ServiceAvailable, 8);
		bitBuilder.AddBitsAsUint(UserFields.Rssi, 8);
		bitBuilder.AddBitsAsUint(UserFields.StereoProgram, 8);
		bitBuilder.AddBitsAsUint(UserFields.RdsSyncStatus, 8);
		bitBuilder.AddBitsAsUint(UserFields.MuteMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Sinr, 8);
		bitBuilder.AddBitsAsUint(UserFields.InterferenceDetectorThreshold, 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.SubEvent > 4)
		{
			list.Add("Maximum allowed value for Sub Event is 0x4");
		}
		if (UserFields.ServiceAvailable > 1)
		{
			list.Add("Maximum allowed value for Service Available is 0x1");
		}
		if (UserFields.StereoProgram > 1)
		{
			list.Add("Maximum allowed value for Stereo Program is 0x1");
		}
		if (UserFields.RdsSyncStatus > 1)
		{
			list.Add("Maximum allowed value for RDS Sync Status is 0x1");
		}
		return list;
	}
}
