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

namespace QCRPG.TSL.FMHCI.Packets;

public class TxRdsProgramService : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ProgramServiceControlEnum
		{
			STOP_TRANSMIT,
			START_CONTINUOUS_TRANSMIT,
			InvalidProgramServiceControlValue
		}

		public static class ProgramServiceControlInfo
		{
			public static ProgramServiceControlEnum ValueToProgramServiceControl(uint value)
			{
				return value switch
				{
					0u => ProgramServiceControlEnum.STOP_TRANSMIT, 
					1u => ProgramServiceControlEnum.START_CONTINUOUS_TRANSMIT, 
					_ => ProgramServiceControlEnum.InvalidProgramServiceControlValue, 
				};
			}

			public static uint ProgramServiceControlToValue(ProgramServiceControlEnum enumItem)
			{
				return enumItem switch
				{
					ProgramServiceControlEnum.STOP_TRANSMIT => 0u, 
					ProgramServiceControlEnum.START_CONTINUOUS_TRANSMIT => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ProgramServiceControlToString(ProgramServiceControlEnum enumItem)
			{
				return enumItem switch
				{
					ProgramServiceControlEnum.STOP_TRANSMIT => "Stop Program Service Transmit", 
					ProgramServiceControlEnum.START_CONTINUOUS_TRANSMIT => "Start Program Service Transmit", 
					_ => "Invalid ProgramServiceControl Value", 
				};
			}

			public static string ProgramServiceControlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Stop Program Service Transmit");
				stringBuilder.AppendLine("0x1 = Start Program Service Transmit");
				return stringBuilder.ToString();
			}
		}

		private uint _ProgramServiceControl;

		private uint _ProgramIdentification;

		private uint _ProgramType;

		private uint _RepeatCount;

		private uint _NumberOfValidProgramServiceStrings;

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

		public uint ProgramServiceControl
		{
			get
			{
				return _ProgramServiceControl;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for ProgramServiceControl is 0xFF");
				}
				_ProgramServiceControl = 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 ProgramType
		{
			get
			{
				return _ProgramType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for ProgramType is 0xFF");
				}
				_ProgramType = value;
			}
		}

		public uint RepeatCount
		{
			get
			{
				return _RepeatCount;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for RepeatCount is 0xFF");
				}
				_RepeatCount = 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 ProgramServiceControlEnum GetProgramServiceControlAsProgramServiceControlEnum()
		{
			return ProgramServiceControlInfo.ValueToProgramServiceControl(_ProgramServiceControl);
		}

		public void SetProgramServiceControlAsProgramServiceControlEnum(ProgramServiceControlEnum enumItem)
		{
			ProgramServiceControl = ProgramServiceControlInfo.ProgramServiceControlToValue(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 _Opcode = 20489u;

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

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

	public uint Opcode => 20489u;

	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.ProgramServiceControl = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ProgramIdentification = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ProgramType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RepeatCount = 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.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.ProgramServiceControl, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ProgramIdentification, 16);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 8);
		bitBuilder.AddBitsAsUint(UserFields.RepeatCount, 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.ProgramServiceControl > 1)
		{
			list.Add("Maximum allowed value for Program Service Control is 0x1");
		}
		if (UserFields.RepeatCount > 15)
		{
			list.Add("Maximum allowed value for Repeat Count is 0xF");
		}
		if (UserFields.NumberOfValidProgramServiceStrings > 12)
		{
			list.Add("Maximum allowed value for Number Of Valid Program Service Strings is 0xC");
		}
		return list;
	}
}
