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

namespace QCRPG.TSL.FMHCI.Packets;

public class SetSpurFilteringTable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ModeEnum
		{
			START,
			START_AND_END,
			CONTINUE,
			END,
			InvalidModeValue
		}

		public static class ModeInfo
		{
			public static ModeEnum ValueToMode(uint value)
			{
				return value switch
				{
					0u => ModeEnum.START, 
					1u => ModeEnum.START_AND_END, 
					2u => ModeEnum.CONTINUE, 
					3u => ModeEnum.END, 
					_ => ModeEnum.InvalidModeValue, 
				};
			}

			public static uint ModeToValue(ModeEnum enumItem)
			{
				return enumItem switch
				{
					ModeEnum.START => 0u, 
					ModeEnum.START_AND_END => 1u, 
					ModeEnum.CONTINUE => 2u, 
					ModeEnum.END => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ModeToString(ModeEnum enumItem)
			{
				return enumItem switch
				{
					ModeEnum.START => "Clean up the existing table and Start a New Set Spur Filtering Table Command.  More to follow.", 
					ModeEnum.START_AND_END => "Clean up the existing table and Start a New Set Spur Filtering Table Command.  No more to follow.", 
					ModeEnum.CONTINUE => "Continue Set Spur Filtering Table Command.  More to follow.", 
					ModeEnum.END => "End of Set Spur Filtering Table Command.  No more to follow.", 
					_ => "Invalid Mode Value", 
				};
			}

			public static string ModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Clean up the existing table and Start a New Set Spur Filtering Table Command.  More to follow.");
				stringBuilder.AppendLine("0x1 = Clean up the existing table and Start a New Set Spur Filtering Table Command.  No more to follow.");
				stringBuilder.AppendLine("0x2 = Continue Set Spur Filtering Table Command.  More to follow.");
				stringBuilder.AppendLine("0x3 = End of Set Spur Filtering Table Command.  No more to follow.");
				return stringBuilder.ToString();
			}
		}

		private uint _Mode;

		private uint _NumberOfSpurFrequenciesToFollow;

		private uint _NumberOfSpurEntries;

		private List<SpurEntriesSubField> _SpurEntriesList = new List<SpurEntriesSubField>();

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

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

		public uint NumberOfSpurEntries
		{
			get
			{
				return _NumberOfSpurEntries;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfSpurEntries is 0xFF");
				}
				_NumberOfSpurEntries = value;
				while (_SpurEntriesList.Count < _NumberOfSpurEntries)
				{
					_SpurEntriesList.Add(new SpurEntriesSubField());
				}
				while (_SpurEntriesList.Count > _NumberOfSpurEntries)
				{
					_SpurEntriesList.RemoveAt(_SpurEntriesList.Count - 1);
				}
			}
		}

		public List<SpurEntriesSubField> SpurEntriesList => _SpurEntriesList;

		public ModeEnum GetModeAsModeEnum()
		{
			return ModeInfo.ValueToMode(_Mode);
		}

		public void SetModeAsModeEnum(ModeEnum enumItem)
		{
			Mode = ModeInfo.ModeToValue(enumItem);
		}
	}

	public class SpurEntriesSubField
	{
		private uint _SpurFrequency;

		private uint _NumberOfSpursToTrack;

		private uint _Spur0Entry;

		private uint _Spur1Entry;

		private uint _Spur2Entry;

		public uint SpurFrequency
		{
			get
			{
				return _SpurFrequency;
			}
			set
			{
				if (value > 16777215)
				{
					throw new Exception("Maximum possible value for SpurFrequency is 0xFFFFFF");
				}
				_SpurFrequency = value;
			}
		}

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

		public uint Spur0Entry
		{
			get
			{
				return _Spur0Entry;
			}
			set
			{
				_Spur0Entry = value;
			}
		}

		public uint Spur1Entry
		{
			get
			{
				return _Spur1Entry;
			}
			set
			{
				_Spur1Entry = value;
			}
		}

		public uint Spur2Entry
		{
			get
			{
				return _Spur2Entry;
			}
			set
			{
				_Spur2Entry = value;
			}
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			SpurFrequency = bitBuilder.RemoveBitsAsUintLittleEndian(24);
			NumberOfSpursToTrack = bitBuilder.RemoveBitsAsUint(8);
			Spur0Entry = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			Spur1Entry = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			Spur2Entry = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(SpurFrequency, 24);
			bitBuilder.AddBitsAsUint(NumberOfSpursToTrack, 8);
			bitBuilder.AddBitsAsUintLittleEndian(Spur0Entry, 32);
			bitBuilder.AddBitsAsUintLittleEndian(Spur1Entry, 32);
			bitBuilder.AddBitsAsUintLittleEndian(Spur2Entry, 32);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 21512u;

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

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

	public uint Opcode => 21512u;

	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.Mode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfSpurFrequenciesToFollow = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfSpurEntries = (uint)(bitBuilder.GetNumberOfBits() / 128);
		for (int i = 0; i < UserFields.SpurEntriesList.Count; i++)
		{
			UserFields.SpurEntriesList[i].FromBitBuilder(ref bitBuilder);
		}
		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.Mode, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfSpurFrequenciesToFollow, 8);
		for (int i = 0; i < UserFields.SpurEntriesList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.SpurEntriesList[i].ToBitString());
		}
		_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.Mode > 3)
		{
			list.Add("Maximum allowed value for Mode is 0x3");
		}
		if (UserFields.NumberOfSpurEntries > 15)
		{
			list.Add("Maximum allowed value for Number Of Spur Entries is 0xF");
		}
		return list;
	}
}
