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

namespace QCRPG.TSL.BT.Packets;

public class ControllerDebugInfo : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LastBlockEnum
		{
			More_debug_info_present,
			Last_binary_block_no_more_debug_data,
			InvalidLastBlockValue
		}

		public static class LastBlockInfo
		{
			public static LastBlockEnum ValueToLastBlock(uint value)
			{
				return value switch
				{
					0u => LastBlockEnum.More_debug_info_present, 
					1u => LastBlockEnum.Last_binary_block_no_more_debug_data, 
					_ => LastBlockEnum.InvalidLastBlockValue, 
				};
			}

			public static uint LastBlockToValue(LastBlockEnum enumItem)
			{
				return enumItem switch
				{
					LastBlockEnum.More_debug_info_present => 0u, 
					LastBlockEnum.Last_binary_block_no_more_debug_data => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LastBlockToString(LastBlockEnum enumItem)
			{
				return enumItem switch
				{
					LastBlockEnum.More_debug_info_present => "More debug info present", 
					LastBlockEnum.Last_binary_block_no_more_debug_data => "Last binary block; no more debug data.", 
					_ => "Invalid LastBlock Value", 
				};
			}

			public static string LastBlockToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = More debug info present");
				stringBuilder.AppendLine("0x1 = Last binary block; no more debug data.");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubEventCode = 87u;

		private uint _DebugBlockByteOffsetStart;

		private uint _LastBlock;

		private uint _CurPayLoadSz;

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

		public uint SubEventCode => 87u;

		public uint DebugBlockByteOffsetStart
		{
			get
			{
				return _DebugBlockByteOffsetStart;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for DebugBlockByteOffsetStart is 0xFFFF");
				}
				_DebugBlockByteOffsetStart = value;
			}
		}

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

		public uint CurPayLoadSz
		{
			get
			{
				return _CurPayLoadSz;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for CurPayLoadSz is 0xFFFF");
				}
				_CurPayLoadSz = value;
				while (_DebugDataList.Count < _CurPayLoadSz)
				{
					_DebugDataList.Add(0u);
				}
				while (_DebugDataList.Count > _CurPayLoadSz)
				{
					_DebugDataList.RemoveAt(_DebugDataList.Count - 1);
				}
			}
		}

		public List<uint> DebugDataList => _DebugDataList;

		public LastBlockEnum GetLastBlockAsLastBlockEnum()
		{
			return LastBlockInfo.ValueToLastBlock(_LastBlock);
		}

		public void SetLastBlockAsLastBlockEnum(LastBlockEnum enumItem)
		{
			LastBlock = LastBlockInfo.LastBlockToValue(enumItem);
		}
	}

	private const uint _EventCode = 255u;

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

	public uint EventCode => 255u;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubEventCode)
		{
			return false;
		}
		UserFields.DebugBlockByteOffsetStart = bitBuilder.RemoveBitsAsUint(16);
		UserFields.LastBlock = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CurPayLoadSz = bitBuilder.RemoveBitsAsUint(16);
		for (int i = 0; i < UserFields.DebugDataList.Count; i++)
		{
			UserFields.DebugDataList[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.AddBitsAsUint(UserFields.SubEventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.DebugBlockByteOffsetStart, 16);
		bitBuilder.AddBitsAsUint(UserFields.LastBlock, 8);
		bitBuilder.AddBitsAsUint(UserFields.CurPayLoadSz, 16);
		for (int i = 0; i < UserFields.DebugDataList.Count; i++)
		{
			if (UserFields.DebugDataList[i] > 255)
			{
				throw new Exception("UserFields.DebugDataList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.DebugDataList[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();
	}
}
