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

namespace QCRPG.TSL.FMHCI.Packets;

public class TxGroupControl : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum BufferControlEnum
		{
			STOP_TRANSMIT,
			START_CONTINUOUS_TRANSMIT,
			START_ONE_SHOT_TRANSMIT,
			CLEAR_RDS_BUFFER,
			InvalidBufferControlValue
		}

		public static class BufferControlInfo
		{
			public static BufferControlEnum ValueToBufferControl(uint value)
			{
				return value switch
				{
					0u => BufferControlEnum.STOP_TRANSMIT, 
					1u => BufferControlEnum.START_CONTINUOUS_TRANSMIT, 
					2u => BufferControlEnum.START_ONE_SHOT_TRANSMIT, 
					3u => BufferControlEnum.CLEAR_RDS_BUFFER, 
					_ => BufferControlEnum.InvalidBufferControlValue, 
				};
			}

			public static uint BufferControlToValue(BufferControlEnum enumItem)
			{
				return enumItem switch
				{
					BufferControlEnum.STOP_TRANSMIT => 0u, 
					BufferControlEnum.START_CONTINUOUS_TRANSMIT => 1u, 
					BufferControlEnum.START_ONE_SHOT_TRANSMIT => 2u, 
					BufferControlEnum.CLEAR_RDS_BUFFER => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string BufferControlToString(BufferControlEnum enumItem)
			{
				return enumItem switch
				{
					BufferControlEnum.STOP_TRANSMIT => "Stop RDS Buffer Transmit", 
					BufferControlEnum.START_CONTINUOUS_TRANSMIT => "Start Continuous RDS Buffer Transmit", 
					BufferControlEnum.START_ONE_SHOT_TRANSMIT => "Start One Shot RDS Buffer Transmit", 
					BufferControlEnum.CLEAR_RDS_BUFFER => "Clear RDS Buffer", 
					_ => "Invalid BufferControl Value", 
				};
			}

			public static string BufferControlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Stop RDS Buffer Transmit");
				stringBuilder.AppendLine("0x1 = Start Continuous RDS Buffer Transmit");
				stringBuilder.AppendLine("0x2 = Start One Shot RDS Buffer Transmit");
				stringBuilder.AppendLine("0x3 = Clear RDS Buffer");
				return stringBuilder.ToString();
			}
		}

		private uint _BufferControl;

		private uint _BufferAvailableThreshold;

		public uint BufferControl
		{
			get
			{
				return _BufferControl;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for BufferControl is 0xFF");
				}
				_BufferControl = value;
			}
		}

		public uint BufferAvailableThreshold
		{
			get
			{
				return _BufferAvailableThreshold;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for BufferAvailableThreshold is 0xFF");
				}
				_BufferAvailableThreshold = value;
			}
		}

		public BufferControlEnum GetBufferControlAsBufferControlEnum()
		{
			return BufferControlInfo.ValueToBufferControl(_BufferControl);
		}

		public void SetBufferControlAsBufferControlEnum(BufferControlEnum enumItem)
		{
			BufferControl = BufferControlInfo.BufferControlToValue(enumItem);
		}
	}

	private const uint _Opcode = 20486u;

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

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

	public uint Opcode => 20486u;

	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.BufferControl = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BufferAvailableThreshold = 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.BufferControl, 8);
		bitBuilder.AddBitsAsUint(UserFields.BufferAvailableThreshold, 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.BufferControl > 3)
		{
			list.Add("Maximum allowed value for Buffer Control is 0x3");
		}
		return list;
	}
}
