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

namespace QCRPG.TSL.FMHCI.Packets;

public class ProgramService : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TrafficProgramCodeFlagEnum
		{
			TP_CODE_NOT_PRESENT,
			TP_CODE_PRESENT,
			InvalidTrafficProgramCodeFlagValue
		}

		public static class TrafficProgramCodeFlagInfo
		{
			public static TrafficProgramCodeFlagEnum ValueToTrafficProgramCodeFlag(uint value)
			{
				return value switch
				{
					0u => TrafficProgramCodeFlagEnum.TP_CODE_NOT_PRESENT, 
					1u => TrafficProgramCodeFlagEnum.TP_CODE_PRESENT, 
					_ => TrafficProgramCodeFlagEnum.InvalidTrafficProgramCodeFlagValue, 
				};
			}

			public static uint TrafficProgramCodeFlagToValue(TrafficProgramCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					TrafficProgramCodeFlagEnum.TP_CODE_NOT_PRESENT => 0u, 
					TrafficProgramCodeFlagEnum.TP_CODE_PRESENT => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TrafficProgramCodeFlagToString(TrafficProgramCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					TrafficProgramCodeFlagEnum.TP_CODE_NOT_PRESENT => "The RDS/RBDS Traffic Program code not present", 
					TrafficProgramCodeFlagEnum.TP_CODE_PRESENT => "The RDS/RBDS Traffic Program code present", 
					_ => "Invalid TrafficProgramCodeFlag Value", 
				};
			}

			public static string TrafficProgramCodeFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The RDS/RBDS Traffic Program code not present");
				stringBuilder.AppendLine("0x1 = The RDS/RBDS Traffic Program code present");
				return stringBuilder.ToString();
			}
		}

		public enum TrafficAnnouncementCodeFlagEnum
		{
			TA_CODE_NOT_PRESENT,
			TA_CODE_PRESENT,
			InvalidTrafficAnnouncementCodeFlagValue
		}

		public static class TrafficAnnouncementCodeFlagInfo
		{
			public static TrafficAnnouncementCodeFlagEnum ValueToTrafficAnnouncementCodeFlag(uint value)
			{
				return value switch
				{
					0u => TrafficAnnouncementCodeFlagEnum.TA_CODE_NOT_PRESENT, 
					1u => TrafficAnnouncementCodeFlagEnum.TA_CODE_PRESENT, 
					_ => TrafficAnnouncementCodeFlagEnum.InvalidTrafficAnnouncementCodeFlagValue, 
				};
			}

			public static uint TrafficAnnouncementCodeFlagToValue(TrafficAnnouncementCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					TrafficAnnouncementCodeFlagEnum.TA_CODE_NOT_PRESENT => 0u, 
					TrafficAnnouncementCodeFlagEnum.TA_CODE_PRESENT => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TrafficAnnouncementCodeFlagToString(TrafficAnnouncementCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					TrafficAnnouncementCodeFlagEnum.TA_CODE_NOT_PRESENT => "The RDS/RBDS Traffic Announcement code not present", 
					TrafficAnnouncementCodeFlagEnum.TA_CODE_PRESENT => "The RDS/RBDS Traffic Announcement code present", 
					_ => "Invalid TrafficAnnouncementCodeFlag Value", 
				};
			}

			public static string TrafficAnnouncementCodeFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The RDS/RBDS Traffic Announcement code not present");
				stringBuilder.AppendLine("0x1 = The RDS/RBDS Traffic Announcement code present");
				return stringBuilder.ToString();
			}
		}

		public enum MusicSpeechSwitchCodeFlagEnum
		{
			MSS_CODE_NOT_PRESENT,
			MSS_CODE_PRESENT,
			InvalidMusicSpeechSwitchCodeFlagValue
		}

		public static class MusicSpeechSwitchCodeFlagInfo
		{
			public static MusicSpeechSwitchCodeFlagEnum ValueToMusicSpeechSwitchCodeFlag(uint value)
			{
				return value switch
				{
					0u => MusicSpeechSwitchCodeFlagEnum.MSS_CODE_NOT_PRESENT, 
					1u => MusicSpeechSwitchCodeFlagEnum.MSS_CODE_PRESENT, 
					_ => MusicSpeechSwitchCodeFlagEnum.InvalidMusicSpeechSwitchCodeFlagValue, 
				};
			}

			public static uint MusicSpeechSwitchCodeFlagToValue(MusicSpeechSwitchCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					MusicSpeechSwitchCodeFlagEnum.MSS_CODE_NOT_PRESENT => 0u, 
					MusicSpeechSwitchCodeFlagEnum.MSS_CODE_PRESENT => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MusicSpeechSwitchCodeFlagToString(MusicSpeechSwitchCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					MusicSpeechSwitchCodeFlagEnum.MSS_CODE_NOT_PRESENT => "The RDS/RBDS Music-Speech Switch code not present", 
					MusicSpeechSwitchCodeFlagEnum.MSS_CODE_PRESENT => "The RDS/RBDS Music-Speech Switch code present", 
					_ => "Invalid MusicSpeechSwitchCodeFlag Value", 
				};
			}

			public static string MusicSpeechSwitchCodeFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The RDS/RBDS Music-Speech Switch code not present");
				stringBuilder.AppendLine("0x1 = The RDS/RBDS Music-Speech Switch code present");
				return stringBuilder.ToString();
			}
		}

		public enum DecoderIdentificationCodeFlagEnum
		{
			DI_CODE_NOT_PRESENT,
			DI_CODE_PRESENT,
			InvalidDecoderIdentificationCodeFlagValue
		}

		public static class DecoderIdentificationCodeFlagInfo
		{
			public static DecoderIdentificationCodeFlagEnum ValueToDecoderIdentificationCodeFlag(uint value)
			{
				return value switch
				{
					0u => DecoderIdentificationCodeFlagEnum.DI_CODE_NOT_PRESENT, 
					1u => DecoderIdentificationCodeFlagEnum.DI_CODE_PRESENT, 
					_ => DecoderIdentificationCodeFlagEnum.InvalidDecoderIdentificationCodeFlagValue, 
				};
			}

			public static uint DecoderIdentificationCodeFlagToValue(DecoderIdentificationCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					DecoderIdentificationCodeFlagEnum.DI_CODE_NOT_PRESENT => 0u, 
					DecoderIdentificationCodeFlagEnum.DI_CODE_PRESENT => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string DecoderIdentificationCodeFlagToString(DecoderIdentificationCodeFlagEnum enumItem)
			{
				return enumItem switch
				{
					DecoderIdentificationCodeFlagEnum.DI_CODE_NOT_PRESENT => "The RDS/RBDS Decoder Identification code not present", 
					DecoderIdentificationCodeFlagEnum.DI_CODE_PRESENT => "The RDS/RBDS Decoder Identification code present", 
					_ => "Invalid DecoderIdentificationCodeFlag Value", 
				};
			}

			public static string DecoderIdentificationCodeFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The RDS/RBDS Decoder Identification code not present");
				stringBuilder.AppendLine("0x1 = The RDS/RBDS Decoder Identification code present");
				return stringBuilder.ToString();
			}
		}

		private uint _ProgramIdentification;

		private uint _ProgramType;

		private uint _TrafficProgramCodeFlag;

		private uint _TrafficAnnouncementCodeFlag;

		private uint _MusicSpeechSwitchCodeFlag;

		private uint _DecoderIdentificationCodeFlag;

		private uint _NumberOfValidProgramServiceStrings;

		private List<ProgramServiceNameSubField> _ProgramServiceNameList = new List<ProgramServiceNameSubField>();

		public uint ProgramIdentification
		{
			get
			{
				return _ProgramIdentification;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for ProgramIdentification is 0xFFFF");
				}
				_ProgramIdentification = 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 TrafficProgramCodeFlag
		{
			get
			{
				return _TrafficProgramCodeFlag;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for TrafficProgramCodeFlag is 0xFF");
				}
				_TrafficProgramCodeFlag = value;
			}
		}

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

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

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

		public uint NumberOfValidProgramServiceStrings
		{
			get
			{
				return _NumberOfValidProgramServiceStrings;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfValidProgramServiceStrings is 0xFF");
				}
				_NumberOfValidProgramServiceStrings = value;
				while (_ProgramServiceNameList.Count < _NumberOfValidProgramServiceStrings)
				{
					_ProgramServiceNameList.Add(new ProgramServiceNameSubField());
				}
				while (_ProgramServiceNameList.Count > _NumberOfValidProgramServiceStrings)
				{
					_ProgramServiceNameList.RemoveAt(_ProgramServiceNameList.Count - 1);
				}
			}
		}

		public List<ProgramServiceNameSubField> ProgramServiceNameList => _ProgramServiceNameList;

		public TrafficProgramCodeFlagEnum GetTrafficProgramCodeFlagAsTrafficProgramCodeFlagEnum()
		{
			return TrafficProgramCodeFlagInfo.ValueToTrafficProgramCodeFlag(_TrafficProgramCodeFlag);
		}

		public void SetTrafficProgramCodeFlagAsTrafficProgramCodeFlagEnum(TrafficProgramCodeFlagEnum enumItem)
		{
			TrafficProgramCodeFlag = TrafficProgramCodeFlagInfo.TrafficProgramCodeFlagToValue(enumItem);
		}

		public TrafficAnnouncementCodeFlagEnum GetTrafficAnnouncementCodeFlagAsTrafficAnnouncementCodeFlagEnum()
		{
			return TrafficAnnouncementCodeFlagInfo.ValueToTrafficAnnouncementCodeFlag(_TrafficAnnouncementCodeFlag);
		}

		public void SetTrafficAnnouncementCodeFlagAsTrafficAnnouncementCodeFlagEnum(TrafficAnnouncementCodeFlagEnum enumItem)
		{
			TrafficAnnouncementCodeFlag = TrafficAnnouncementCodeFlagInfo.TrafficAnnouncementCodeFlagToValue(enumItem);
		}

		public MusicSpeechSwitchCodeFlagEnum GetMusicSpeechSwitchCodeFlagAsMusicSpeechSwitchCodeFlagEnum()
		{
			return MusicSpeechSwitchCodeFlagInfo.ValueToMusicSpeechSwitchCodeFlag(_MusicSpeechSwitchCodeFlag);
		}

		public void SetMusicSpeechSwitchCodeFlagAsMusicSpeechSwitchCodeFlagEnum(MusicSpeechSwitchCodeFlagEnum enumItem)
		{
			MusicSpeechSwitchCodeFlag = MusicSpeechSwitchCodeFlagInfo.MusicSpeechSwitchCodeFlagToValue(enumItem);
		}

		public DecoderIdentificationCodeFlagEnum GetDecoderIdentificationCodeFlagAsDecoderIdentificationCodeFlagEnum()
		{
			return DecoderIdentificationCodeFlagInfo.ValueToDecoderIdentificationCodeFlag(_DecoderIdentificationCodeFlag);
		}

		public void SetDecoderIdentificationCodeFlagAsDecoderIdentificationCodeFlagEnum(DecoderIdentificationCodeFlagEnum enumItem)
		{
			DecoderIdentificationCodeFlag = DecoderIdentificationCodeFlagInfo.DecoderIdentificationCodeFlagToValue(enumItem);
		}
	}

	public class ProgramServiceNameSubField
	{
		private ulong _NameString;

		private uint _NullTerminator;

		public ulong NameString
		{
			get
			{
				return _NameString;
			}
			set
			{
				_NameString = value;
			}
		}

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			NameString = bitBuilder.RemoveBitsAsUlong(64);
			NullTerminator = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUlong(NameString, 64);
			bitBuilder.AddBitsAsUint(NullTerminator, 8);
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 9u;

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

	public uint EventCode => 9u;

	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.ProgramIdentification = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ProgramType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TrafficProgramCodeFlag = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TrafficAnnouncementCodeFlag = bitBuilder.RemoveBitsAsUint(8);
		UserFields.MusicSpeechSwitchCodeFlag = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DecoderIdentificationCodeFlag = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfValidProgramServiceStrings = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.ProgramServiceNameList.Count; i++)
		{
			UserFields.ProgramServiceNameList[i].FromBitBuilder(ref bitBuilder);
		}
		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.AddBitsAsUintLittleEndian(UserFields.ProgramIdentification, 16);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 8);
		bitBuilder.AddBitsAsUint(UserFields.TrafficProgramCodeFlag, 8);
		bitBuilder.AddBitsAsUint(UserFields.TrafficAnnouncementCodeFlag, 8);
		bitBuilder.AddBitsAsUint(UserFields.MusicSpeechSwitchCodeFlag, 8);
		bitBuilder.AddBitsAsUint(UserFields.DecoderIdentificationCodeFlag, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfValidProgramServiceStrings, 8);
		for (int i = 0; i < UserFields.ProgramServiceNameList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ProgramServiceNameList[i].ToBitString());
		}
		_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.TrafficProgramCodeFlag > 1)
		{
			list.Add("Maximum allowed value for Traffic Program Code Flag is 0x1");
		}
		if (UserFields.TrafficAnnouncementCodeFlag > 1)
		{
			list.Add("Maximum allowed value for Traffic Announcement Code Flag is 0x1");
		}
		if (UserFields.MusicSpeechSwitchCodeFlag > 1)
		{
			list.Add("Maximum allowed value for Music Speech Switch Code Flag is 0x1");
		}
		if (UserFields.DecoderIdentificationCodeFlag > 1)
		{
			list.Add("Maximum allowed value for Decoder Identification Code Flag is 0x1");
		}
		if (UserFields.NumberOfValidProgramServiceStrings > 12)
		{
			list.Add("Maximum allowed value for Number Of Valid Program Service Strings is 0xC");
		}
		return list;
	}
}
