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

namespace QCRPG.TSL.FM.Packets;

public class RdsTransmitRadioText : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ControlEnum
		{
			STOP,
			START,
			InvalidControlValue
		}

		public static class ControlInfo
		{
			public static ControlEnum ValueToControl(uint value)
			{
				return value switch
				{
					0u => ControlEnum.STOP, 
					1u => ControlEnum.START, 
					_ => ControlEnum.InvalidControlValue, 
				};
			}

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

			public static string ControlToString(ControlEnum enumItem)
			{
				return enumItem switch
				{
					ControlEnum.STOP => "Stop radio text transmit", 
					ControlEnum.START => "Start radio text transmit", 
					_ => "Invalid Control Value", 
				};
			}

			public static string ControlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Stop radio text transmit");
				stringBuilder.AppendLine("0x1 = Start radio text transmit");
				return stringBuilder.ToString();
			}
		}

		private const uint _Undefined1 = 0u;

		private const uint _Undefined2 = 0u;

		private uint _Length;

		private uint _Control;

		private uint _ProgramType;

		private uint _ProgramIdentification;

		private uint _TextAbFlag;

		private uint _TrafficProgramCode;

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

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Length is 0xFF");
				}
				if (value > 64)
				{
					_Length = 64u;
				}
				else
				{
					_Length = value;
				}
				while (_RadioTextList.Count < _Length)
				{
					_RadioTextList.Add(0u);
				}
				while (_RadioTextList.Count > _Length)
				{
					_RadioTextList.RemoveAt(_RadioTextList.Count - 1);
				}
			}
		}

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

		public uint Undefined1 => 0u;

		public uint ProgramType
		{
			get
			{
				return _ProgramType;
			}
			set
			{
				if (value > 31)
				{
					throw new Exception("Maximum possible value for ProgramType is 0x1F");
				}
				_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 Undefined2 => 0u;

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

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

		public List<uint> RadioTextList => _RadioTextList;

		public ControlEnum GetControlAsControlEnum()
		{
			return ControlInfo.ValueToControl(_Control);
		}

		public void SetControlAsControlEnum(ControlEnum enumItem)
		{
			Control = ControlInfo.ControlToValue(enumItem);
		}

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

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

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.Length = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Control = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(2) != UserFields.Undefined1)
		{
			return false;
		}
		UserFields.ProgramType = bitBuilder.RemoveBitsAsUint(5);
		UserFields.ProgramIdentification = bitBuilder.RemoveBitsAsUint(16);
		if (bitBuilder.RemoveBitsAsUint(6) != UserFields.Undefined2)
		{
			return false;
		}
		UserFields.TextAbFlag = bitBuilder.RemoveBitsAsUint(1);
		UserFields.TrafficProgramCode = bitBuilder.RemoveBitsAsUint(1);
		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(UserFields.Length, 8);
		bitBuilder.AddBitsAsUint(UserFields.Control, 1);
		bitBuilder.AddBitsAsUint(UserFields.Undefined1, 2);
		bitBuilder.AddBitsAsUint(UserFields.ProgramType, 5);
		bitBuilder.AddBitsAsUint(UserFields.ProgramIdentification, 16);
		bitBuilder.AddBitsAsUint(UserFields.Undefined2, 6);
		bitBuilder.AddBitsAsUint(UserFields.TextAbFlag, 1);
		bitBuilder.AddBitsAsUint(UserFields.TrafficProgramCode, 1);
		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);
		}
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Length > 64)
		{
			list.Add("Maximum allowed value for Length is 0x40");
		}
		return list;
	}
}
