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

namespace QCRPG.TSL.FM.Packets;

public class RdsTransmitProgramService : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TxControlEnum
		{
			STOP,
			START,
			InvalidTxControlValue
		}

		public static class TxControlInfo
		{
			public static TxControlEnum ValueToTxControl(uint value)
			{
				return value switch
				{
					0u => TxControlEnum.STOP, 
					1u => TxControlEnum.START, 
					_ => TxControlEnum.InvalidTxControlValue, 
				};
			}

			public static uint TxControlToValue(TxControlEnum enumItem)
			{
				return enumItem switch
				{
					TxControlEnum.STOP => 0u, 
					TxControlEnum.START => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TxControlToString(TxControlEnum enumItem)
			{
				return enumItem switch
				{
					TxControlEnum.STOP => "Stop Program Service table transmit", 
					TxControlEnum.START => "Start Program Service table transmit", 
					_ => "Invalid TxControl Value", 
				};
			}

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

		private const uint _ReadOnly = 0u;

		private const uint _Undefined1 = 0u;

		private const uint _Undefined2 = 0u;

		private uint _NumberOfProgramServiceNames;

		private uint _ProgramType;

		private uint _ProgramIdentification;

		private uint _TxControl;

		private uint _TxRepeat;

		private List<ProgramServiceNameListSubField> _ProgramServiceNameListList = new List<ProgramServiceNameListSubField>();

		public uint ReadOnly => 0u;

		public uint NumberOfProgramServiceNames
		{
			get
			{
				return _NumberOfProgramServiceNames;
			}
			set
			{
				switch (value)
				{
				default:
					throw new Exception("Maximum possible value for NumberOfProgramServiceNames is 0xF");
				case 13u:
				case 14u:
				case 15u:
					_NumberOfProgramServiceNames = 12u;
					break;
				case 0u:
				case 1u:
				case 2u:
				case 3u:
				case 4u:
				case 5u:
				case 6u:
				case 7u:
				case 8u:
				case 9u:
				case 10u:
				case 11u:
				case 12u:
					_NumberOfProgramServiceNames = value;
					break;
				}
				while (_ProgramServiceNameListList.Count < _NumberOfProgramServiceNames)
				{
					_ProgramServiceNameListList.Add(new ProgramServiceNameListSubField());
				}
				while (_ProgramServiceNameListList.Count > _NumberOfProgramServiceNames)
				{
					_ProgramServiceNameListList.RemoveAt(_ProgramServiceNameListList.Count - 1);
				}
			}
		}

		public uint Undefined1 => 0u;

		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 => 0u;

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

		public List<ProgramServiceNameListSubField> ProgramServiceNameListList => _ProgramServiceNameListList;

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

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

		public TxControlEnum GetTxControlAsTxControlEnum()
		{
			return TxControlInfo.ValueToTxControl(_TxControl);
		}

		public void SetTxControlAsTxControlEnum(TxControlEnum enumItem)
		{
			TxControl = TxControlInfo.TxControlToValue(enumItem);
		}
	}

	public class ProgramServiceNameListSubField
	{
		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.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.ReadOnly, 4);
		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.ProgramServiceNameListList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ProgramServiceNameListList[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;
	}
}
