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

namespace QCRPG.TSL.BT.Packets;

public class HardwareError : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum HardwareCodeEnum
		{
			Hardware_problem_code,
			InvalidHardwareCodeValue
		}

		public static class HardwareCodeInfo
		{
			public static HardwareCodeEnum ValueToHardwareCode(uint value)
			{
				if (value >= 0 && value <= 255)
				{
					return HardwareCodeEnum.Hardware_problem_code;
				}
				return HardwareCodeEnum.InvalidHardwareCodeValue;
			}

			public static uint HardwareCodeToValue(HardwareCodeEnum enumItem)
			{
				if (enumItem == HardwareCodeEnum.Hardware_problem_code)
				{
					return 0u;
				}
				return uint.MaxValue;
			}

			public static string HardwareCodeToString(HardwareCodeEnum enumItem)
			{
				if (enumItem == HardwareCodeEnum.Hardware_problem_code)
				{
					return "Hardware problem code";
				}
				return "Invalid HardwareCode Value";
			}

			public static string HardwareCodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0xFF = Hardware problem code");
				return stringBuilder.ToString();
			}
		}

		private uint _HardwareCode;

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

		public HardwareCodeEnum GetHardwareCodeAsHardwareCodeEnum()
		{
			return HardwareCodeInfo.ValueToHardwareCode(_HardwareCode);
		}

		public void SetHardwareCodeAsHardwareCodeEnum(HardwareCodeEnum enumItem)
		{
			HardwareCode = HardwareCodeInfo.HardwareCodeToValue(enumItem);
		}
	}

	private const uint _EventCode = 16u;

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

	public uint EventCode => 16u;

	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;
		}
		UserFields.HardwareCode = 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.HardwareCode, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
