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

namespace QCRPG.TSL.FMHCI.Packets;

public class RadioText : 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 TextABFlagEnum
		{
			TEXT_AB_CODE_NOT_SET,
			TEXT_AB_CODE_SET,
			InvalidTextABFlagValue
		}

		public static class TextABFlagInfo
		{
			public static TextABFlagEnum ValueToTextABFlag(uint value)
			{
				return value switch
				{
					0u => TextABFlagEnum.TEXT_AB_CODE_NOT_SET, 
					1u => TextABFlagEnum.TEXT_AB_CODE_SET, 
					_ => TextABFlagEnum.InvalidTextABFlagValue, 
				};
			}

			public static uint TextABFlagToValue(TextABFlagEnum enumItem)
			{
				return enumItem switch
				{
					TextABFlagEnum.TEXT_AB_CODE_NOT_SET => 0u, 
					TextABFlagEnum.TEXT_AB_CODE_SET => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TextABFlagToString(TextABFlagEnum enumItem)
			{
				return enumItem switch
				{
					TextABFlagEnum.TEXT_AB_CODE_NOT_SET => "The RDS/RBDS Text A/B code not set", 
					TextABFlagEnum.TEXT_AB_CODE_SET => "The RDS/RBDS Text A/B code set", 
					_ => "Invalid TextABFlag Value", 
				};
			}

			public static string TextABFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The RDS/RBDS Text A/B code not set");
				stringBuilder.AppendLine("0x1 = The RDS/RBDS Text A/B code set");
				return stringBuilder.ToString();
			}
		}

		private uint _ProgramIdentification;

		private uint _ProgramType;

		private uint _TrafficProgramCodeFlag;

		private uint _TextABFlag;

		private uint _RadioTextLength;

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

		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 TextABFlag
		{
			get
			{
				return _TextABFlag;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for TextABFlag is 0xFF");
				}
				_TextABFlag = 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 TrafficProgramCodeFlagEnum GetTrafficProgramCodeFlagAsTrafficProgramCodeFlagEnum()
		{
			return TrafficProgramCodeFlagInfo.ValueToTrafficProgramCodeFlag(_TrafficProgramCodeFlag);
		}

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

		public TextABFlagEnum GetTextABFlagAsTextABFlagEnum()
		{
			return TextABFlagInfo.ValueToTextABFlag(_TextABFlag);
		}

		public void SetTextABFlagAsTextABFlagEnum(TextABFlagEnum enumItem)
		{
			TextABFlag = TextABFlagInfo.TextABFlagToValue(enumItem);
		}
	}

	private const uint _EventCode = 10u;

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

	public uint EventCode => 10u;

	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.TextABFlag = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RadioTextLength = (uint)(bitBuilder.GetNumberOfBits() / 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.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.TextABFlag, 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.TrafficProgramCodeFlag > 1)
		{
			list.Add("Maximum allowed value for Traffic Program Code Flag is 0x1");
		}
		if (UserFields.TextABFlag > 1)
		{
			list.Add("Maximum allowed value for Text A B Flag is 0x1");
		}
		if (UserFields.RadioTextLength > 64)
		{
			list.Add("Maximum allowed value for Radio Text Length is 0x40");
		}
		return list;
	}
}
