using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class RdsTransmitGroups : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum AddGroupEnum
		{
			ADD_GROUP_TO_BUFFER,
			DO_NOT_ADD_TO_GROUP_BUFFER,
			InvalidAddGroupValue
		}

		public static class AddGroupInfo
		{
			public static AddGroupEnum ValueToAddGroup(uint value)
			{
				return value switch
				{
					0u => AddGroupEnum.ADD_GROUP_TO_BUFFER, 
					1u => AddGroupEnum.DO_NOT_ADD_TO_GROUP_BUFFER, 
					_ => AddGroupEnum.InvalidAddGroupValue, 
				};
			}

			public static uint AddGroupToValue(AddGroupEnum enumItem)
			{
				return enumItem switch
				{
					AddGroupEnum.ADD_GROUP_TO_BUFFER => 0u, 
					AddGroupEnum.DO_NOT_ADD_TO_GROUP_BUFFER => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AddGroupToString(AddGroupEnum enumItem)
			{
				return enumItem switch
				{
					AddGroupEnum.ADD_GROUP_TO_BUFFER => "Add group to buffer", 
					AddGroupEnum.DO_NOT_ADD_TO_GROUP_BUFFER => "Do not add to group buffer", 
					_ => "Invalid AddGroup Value", 
				};
			}

			public static string AddGroupToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Add group to buffer");
				stringBuilder.AppendLine("0x1 = Do not add to group buffer");
				return stringBuilder.ToString();
			}
		}

		public enum ControlEnum
		{
			STOP_RDS_BUFFER_TRANSMIT,
			CONTINUOUSLY_TRANSMIT_RDS_BUFFER,
			TRANSMIT_RDS_BUFFER_ONCE,
			CLEAR_RDS_BUFFER,
			InvalidControlValue
		}

		public static class ControlInfo
		{
			public static ControlEnum ValueToControl(uint value)
			{
				return value switch
				{
					0u => ControlEnum.STOP_RDS_BUFFER_TRANSMIT, 
					1u => ControlEnum.CONTINUOUSLY_TRANSMIT_RDS_BUFFER, 
					2u => ControlEnum.TRANSMIT_RDS_BUFFER_ONCE, 
					3u => ControlEnum.CLEAR_RDS_BUFFER, 
					_ => ControlEnum.InvalidControlValue, 
				};
			}

			public static uint ControlToValue(ControlEnum enumItem)
			{
				return enumItem switch
				{
					ControlEnum.STOP_RDS_BUFFER_TRANSMIT => 0u, 
					ControlEnum.CONTINUOUSLY_TRANSMIT_RDS_BUFFER => 1u, 
					ControlEnum.TRANSMIT_RDS_BUFFER_ONCE => 2u, 
					ControlEnum.CLEAR_RDS_BUFFER => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ControlToString(ControlEnum enumItem)
			{
				return enumItem switch
				{
					ControlEnum.STOP_RDS_BUFFER_TRANSMIT => "Stop RDS buffer transmit", 
					ControlEnum.CONTINUOUSLY_TRANSMIT_RDS_BUFFER => "Continuously transmit RDS buffer", 
					ControlEnum.TRANSMIT_RDS_BUFFER_ONCE => "Transmit RDS buffer once", 
					ControlEnum.CLEAR_RDS_BUFFER => "Clear RDS buffer", 
					_ => "Invalid Control Value", 
				};
			}

			public static string ControlToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Stop RDS buffer transmit");
				stringBuilder.AppendLine("0x1 = Continuously transmit RDS buffer");
				stringBuilder.AppendLine("0x2 = Transmit RDS buffer once");
				stringBuilder.AppendLine("0x3 = Clear RDS buffer");
				return stringBuilder.ToString();
			}
		}

		private const uint _RegisterAddress = 31u;

		private const uint _XfrMode = 144u;

		private const uint _Undefined = 0u;

		private uint _AddGroup;

		private uint _Control;

		private ulong _RdsGroup;

		public uint RegisterAddress => 31u;

		public uint XfrMode => 144u;

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

		public uint Undefined => 0u;

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

		public ulong RdsGroup
		{
			get
			{
				return _RdsGroup;
			}
			set
			{
				_RdsGroup = value;
			}
		}

		public RegisterMapEnum GetRegisterAddressAsRegisterMapEnum()
		{
			return RegisterMapInfo.ValueToRegisterMap(31u);
		}

		public XfrModeMapEnum GetXfrModeAsXfrModeMapEnum()
		{
			return XfrModeMapInfo.ValueToXfrModeMap(144u);
		}

		public AddGroupEnum GetAddGroupAsAddGroupEnum()
		{
			return AddGroupInfo.ValueToAddGroup(_AddGroup);
		}

		public void SetAddGroupAsAddGroupEnum(AddGroupEnum enumItem)
		{
			AddGroup = AddGroupInfo.AddGroupToValue(enumItem);
		}

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

		public void SetControlAsControlEnum(ControlEnum enumItem)
		{
			Control = ControlInfo.ControlToValue(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 RdsTransmitGroupsInfo(this);
			}
			return info;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.RegisterAddress)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.XfrMode)
		{
			return false;
		}
		UserFields.AddGroup = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(5) != UserFields.Undefined)
		{
			return false;
		}
		UserFields.Control = bitBuilder.RemoveBitsAsUint(2);
		UserFields.RdsGroup = bitBuilder.RemoveBitsAsUlong(64);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.RegisterAddress, 8);
		bitBuilder.AddBitsAsUint(UserFields.XfrMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.AddGroup, 1);
		bitBuilder.AddBitsAsUint(UserFields.Undefined, 5);
		bitBuilder.AddBitsAsUint(UserFields.Control, 2);
		bitBuilder.AddBitsAsUlong(UserFields.RdsGroup, 64);
		return bitBuilder.ToString();
	}
}
