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

namespace QCRPG.TSL.FM.Packets;

public class ReadRdsProgramServiceResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _DecoderInformation;

		private uint _MusicSpeechSwitch;

		private uint _TrafficAnnouncement;

		private uint _TrafficProgram;

		private uint _NumberOfProgramServiceNames;

		private uint _Undefined1;

		private uint _ProgramType;

		private uint _ProgramIdentification;

		private uint _TxControl;

		private uint _Undefined2;

		private uint _TxRepeat;

		private List<ProgramServiceNamesSubField> _ProgramServiceNamesList = new List<ProgramServiceNamesSubField>();

		public uint DecoderInformation
		{
			get
			{
				return _DecoderInformation;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for DecoderInformation is 0x1");
				}
				_DecoderInformation = value;
			}
		}

		public uint MusicSpeechSwitch
		{
			get
			{
				return _MusicSpeechSwitch;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for MusicSpeechSwitch is 0x1");
				}
				_MusicSpeechSwitch = value;
			}
		}

		public uint TrafficAnnouncement
		{
			get
			{
				return _TrafficAnnouncement;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for TrafficAnnouncement is 0x1");
				}
				_TrafficAnnouncement = value;
			}
		}

		public uint TrafficProgram
		{
			get
			{
				return _TrafficProgram;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for TrafficProgram is 0x1");
				}
				_TrafficProgram = value;
			}
		}

		public uint NumberOfProgramServiceNames
		{
			get
			{
				return _NumberOfProgramServiceNames;
			}
			set
			{
				if (value > 15)
				{
					throw new Exception("Maximum possible value for NumberOfProgramServiceNames is 0xF");
				}
				_NumberOfProgramServiceNames = value;
				while (_ProgramServiceNamesList.Count < _NumberOfProgramServiceNames)
				{
					_ProgramServiceNamesList.Add(new ProgramServiceNamesSubField());
				}
				while (_ProgramServiceNamesList.Count > _NumberOfProgramServiceNames)
				{
					_ProgramServiceNamesList.RemoveAt(_ProgramServiceNamesList.Count - 1);
				}
			}
		}

		public uint Undefined1
		{
			get
			{
				return _Undefined1;
			}
			set
			{
				if (value > 15)
				{
					throw new Exception("Maximum possible value for Undefined1 is 0xF");
				}
				_Undefined1 = value;
			}
		}

		public uint ProgramType
		{
			get
			{
				return _ProgramType;
			}
			set
			{
				if (value > 15)
				{
					throw new Exception("Maximum possible value for ProgramType is 0xF");
				}
				_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 uint TxControl
		{
			get
			{
				return _TxControl;
			}
			set
			{
				if (value > 1)
				{
					throw new Exception("Maximum possible value for TxControl is 0x1");
				}
				_TxControl = value;
			}
		}

		public uint Undefined2
		{
			get
			{
				return _Undefined2;
			}
			set
			{
				if (value > 7)
				{
					throw new Exception("Maximum possible value for Undefined2 is 0x7");
				}
				_Undefined2 = value;
			}
		}

		public uint TxRepeat
		{
			get
			{
				return _TxRepeat;
			}
			set
			{
				if (value > 15)
				{
					throw new Exception("Maximum possible value for TxRepeat is 0xF");
				}
				_TxRepeat = value;
			}
		}

		public List<ProgramServiceNamesSubField> ProgramServiceNamesList => _ProgramServiceNamesList;

		public ProgramTypeEnum GetProgramTypeAsProgramTypeEnum()
		{
			return ProgramTypeInfo.ValueToProgramType(_ProgramType);
		}

		public void SetProgramTypeAsProgramTypeEnum(ProgramTypeEnum enumItem)
		{
			ProgramType = ProgramTypeInfo.ProgramTypeToValue(enumItem);
		}
	}

	public class ProgramServiceNamesSubField
	{
		private ulong _ProgramServiceName;

		public ulong ProgramServiceName
		{
			get
			{
				return _ProgramServiceName;
			}
			set
			{
				_ProgramServiceName = value;
			}
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ProgramServiceName = bitBuilder.RemoveBitsAsUlong(64);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUlong(ProgramServiceName, 64);
			return bitBuilder.ToString();
		}
	}

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

	private PacketInfoBase info;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.DecoderInformation = bitBuilder.RemoveBitsAsUint(1);
		UserFields.MusicSpeechSwitch = bitBuilder.RemoveBitsAsUint(1);
		UserFields.TrafficAnnouncement = bitBuilder.RemoveBitsAsUint(1);
		UserFields.TrafficProgram = bitBuilder.RemoveBitsAsUint(1);
		UserFields.NumberOfProgramServiceNames = bitBuilder.RemoveBitsAsUint(4);
		UserFields.Undefined1 = bitBuilder.RemoveBitsAsUint(4);
		UserFields.ProgramType = bitBuilder.RemoveBitsAsUint(4);
		UserFields.ProgramIdentification = bitBuilder.RemoveBitsAsUint(16);
		UserFields.TxControl = bitBuilder.RemoveBitsAsUint(1);
		UserFields.Undefined2 = bitBuilder.RemoveBitsAsUint(3);
		UserFields.TxRepeat = bitBuilder.RemoveBitsAsUint(4);
		for (int i = 0; i < UserFields.ProgramServiceNamesList.Count; i++)
		{
			UserFields.ProgramServiceNamesList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.DecoderInformation, 1);
		bitBuilder.AddBitsAsUint(UserFields.MusicSpeechSwitch, 1);
		bitBuilder.AddBitsAsUint(UserFields.TrafficAnnouncement, 1);
		bitBuilder.AddBitsAsUint(UserFields.TrafficProgram, 1);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfProgramServiceNames, 4);
		bitBuilder.AddBitsAsUint(UserFields.Undefined1, 4);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 4);
		bitBuilder.AddBitsAsUint(UserFields.ProgramIdentification, 16);
		bitBuilder.AddBitsAsUint(UserFields.TxControl, 1);
		bitBuilder.AddBitsAsUint(UserFields.Undefined2, 3);
		bitBuilder.AddBitsAsUint(UserFields.TxRepeat, 4);
		for (int i = 0; i < UserFields.ProgramServiceNamesList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ProgramServiceNamesList[i].ToBitString());
		}
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.NumberOfProgramServiceNames > 12)
		{
			list.Add("Maximum allowed value for Number Of Program Service Names is 0xC");
		}
		return list;
	}
}
