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

namespace QCRPG.TSL.BT.Packets;

public class RamDumpFragment : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SequenceNumberEnum
		{
			Last_fragment,
			InvalidSequenceNumberValue
		}

		public static class SequenceNumberInfo
		{
			public static SequenceNumberEnum ValueToSequenceNumber(uint value)
			{
				if (value == 65535)
				{
					return SequenceNumberEnum.Last_fragment;
				}
				return SequenceNumberEnum.InvalidSequenceNumberValue;
			}

			public static uint SequenceNumberToValue(SequenceNumberEnum enumItem)
			{
				if (enumItem == SequenceNumberEnum.Last_fragment)
				{
					return 65535u;
				}
				return uint.MaxValue;
			}

			public static string SequenceNumberToString(SequenceNumberEnum enumItem)
			{
				if (enumItem == SequenceNumberEnum.Last_fragment)
				{
					return "Last fragment";
				}
				return "Invalid SequenceNumber Value";
			}

			public static string SequenceNumberToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0xFFFF = Last fragment");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubEventCode = 1u;

		private const uint _MessageType = 8u;

		private const uint _Reserved = 0u;

		private uint _SequenceNumber;

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

		private uint _DumpFragmentLength = 1u;

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

		public uint SubEventCode => 1u;

		public uint MessageType => 8u;

		public uint SequenceNumber
		{
			get
			{
				return _SequenceNumber;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for SequenceNumber is 0xFFFF");
				}
				_SequenceNumber = value;
				int num = 0;
				if (SequenceNumber == 0)
				{
					num = 4;
				}
				while (_TotalLengthList.Count < num)
				{
					_TotalLengthList.Add(0u);
				}
				while (_TotalLengthList.Count > num)
				{
					_TotalLengthList.RemoveAt(_TotalLengthList.Count - 1);
				}
			}
		}

		public uint Reserved => 0u;

		public List<uint> TotalLengthList => _TotalLengthList;

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

		public List<uint> DumpFragmentList => _DumpFragmentList;

		public SequenceNumberEnum GetSequenceNumberAsSequenceNumberEnum()
		{
			if (_SequenceNumber != 65535)
			{
				throw new Exception("Sequence Number is not 0xFFFF, can not get Sequence Number as SequenceNumberEnum");
			}
			return SequenceNumberInfo.ValueToSequenceNumber(_SequenceNumber);
		}

		public void SetSequenceNumberAsSequenceNumberEnum(SequenceNumberEnum enumItem)
		{
			SequenceNumber = SequenceNumberInfo.SequenceNumberToValue(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 RamDumpFragmentInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 255u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.SequenceNumber = 0u;
		UserFields.DumpFragmentLength = 1u;
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.MessageType)
		{
			return false;
		}
		UserFields.SequenceNumber = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Reserved)
		{
			return false;
		}
		for (int num = UserFields.TotalLengthList.Count - 1; num >= 0; num--)
		{
			UserFields.TotalLengthList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		UserFields.DumpFragmentLength = (uint)(bitBuilder.GetNumberOfBits() / 8);
		for (int i = 0; i < UserFields.DumpFragmentList.Count; i++)
		{
			UserFields.DumpFragmentList[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.MessageType, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SequenceNumber, 16);
		bitBuilder.AddBitsAsUint(UserFields.Reserved, 8);
		for (int num = UserFields.TotalLengthList.Count - 1; num >= 0; num--)
		{
			if (UserFields.TotalLengthList[num] > 255)
			{
				throw new Exception("UserFields.TotalLengthList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.TotalLengthList[num], 8);
		}
		for (int i = 0; i < UserFields.DumpFragmentList.Count; i++)
		{
			if (UserFields.DumpFragmentList[i] > 255)
			{
				throw new Exception("UserFields.DumpFragmentList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.DumpFragmentList[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();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.DumpFragmentLength < 1)
		{
			list.Add("Minimum allowed value for Dump Fragment Length is 0x1");
		}
		if (UserFields.DumpFragmentLength > 248)
		{
			list.Add("Maximum allowed value for Dump Fragment Length is 0xF8");
		}
		return list;
	}
}
