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

namespace QCRPG.TSL.FMHCI.Packets;

public class TxRdsRadioText : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum RadioTextControlEnum
		{
			STOP_TRANSMIT,
			START_CONTINUOUS_TRANSMIT,
			InvalidRadioTextControlValue
		}

		public static class RadioTextControlInfo
		{
			public static RadioTextControlEnum ValueToRadioTextControl(uint value)
			{
				return value switch
				{
					0u => RadioTextControlEnum.STOP_TRANSMIT, 
					1u => RadioTextControlEnum.START_CONTINUOUS_TRANSMIT, 
					_ => RadioTextControlEnum.InvalidRadioTextControlValue, 
				};
			}

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

			public static string RadioTextControlToString(RadioTextControlEnum enumItem)
			{
				return enumItem switch
				{
					RadioTextControlEnum.STOP_TRANSMIT => "Stop Radio Text Transmit", 
					RadioTextControlEnum.START_CONTINUOUS_TRANSMIT => "Start Radio Text Transmit", 
					_ => "Invalid RadioTextControl Value", 
				};
			}

			public static string RadioTextControlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Stop Radio Text Transmit");
				stringBuilder.AppendLine("0x1 = Start Radio Text Transmit");
				return stringBuilder.ToString();
			}
		}

		private uint _RadioTextControl;

		private uint _ProgramIdentification;

		private uint _ProgramType;

		private uint _RadioTextLength;

		private List<uint> _RadioTextList = new List<uint>();

		public uint RadioTextControl
		{
			get
			{
				return _RadioTextControl;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for RadioTextControl is 0xFF");
				}
				_RadioTextControl = 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 RadioTextLength
		{
			get
			{
				return _RadioTextLength;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for RadioTextLength is 0xFF");
				}
				_RadioTextLength = value;
				while (_RadioTextList.Count < _RadioTextLength)
				{
					_RadioTextList.Add(0u);
				}
				while (_RadioTextList.Count > _RadioTextLength)
				{
					_RadioTextList.RemoveAt(_RadioTextList.Count - 1);
				}
			}
		}

		public List<uint> RadioTextList => _RadioTextList;

		public RadioTextControlEnum GetRadioTextControlAsRadioTextControlEnum()
		{
			return RadioTextControlInfo.ValueToRadioTextControl(_RadioTextControl);
		}

		public void SetRadioTextControlAsRadioTextControlEnum(RadioTextControlEnum enumItem)
		{
			RadioTextControl = RadioTextControlInfo.RadioTextControlToValue(enumItem);
		}
	}

	private const uint _Opcode = 20488u;

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

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

	public uint Opcode => 20488u;

	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.RadioTextControl = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ProgramIdentification = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ProgramType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RadioTextLength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.RadioTextList.Count; i++)
		{
			UserFields.RadioTextList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		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.RadioTextControl, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ProgramIdentification, 16);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 8);
		bitBuilder.AddBitsAsUint(UserFields.RadioTextLength, 8);
		for (int i = 0; i < UserFields.RadioTextList.Count; i++)
		{
			if (UserFields.RadioTextList[i] > 255)
			{
				throw new Exception("UserFields.RadioTextList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.RadioTextList[i], 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.RadioTextControl > 2)
		{
			list.Add("Maximum allowed value for Radio Text Control is 0x2");
		}
		if (UserFields.RadioTextLength > 64)
		{
			list.Add("Maximum allowed value for Radio Text Length is 0x40");
		}
		return list;
	}
}
