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

namespace QCRPG.TSL.BT.Packets;

public class ReadResetReasonEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ResetReasonEnum
		{
			Bit15_8_Reset_reason_valid_value_Bit7_0,
			UNKNOWN,
			SW_REQUESTED,
			STACK_OVERVIEW,
			EXCEPTION,
			ASSERT,
			TRAP,
			OS_FATAL,
			HCI_RESET,
			POWERON,
			WATCHDOG,
			InvalidResetReasonValue
		}

		public static class ResetReasonInfo
		{
			public static ResetReasonEnum ValueToResetReason(uint value)
			{
				return value switch
				{
					48512u => ResetReasonEnum.Bit15_8_Reset_reason_valid_value_Bit7_0, 
					48513u => ResetReasonEnum.UNKNOWN, 
					48514u => ResetReasonEnum.SW_REQUESTED, 
					48515u => ResetReasonEnum.STACK_OVERVIEW, 
					48516u => ResetReasonEnum.EXCEPTION, 
					48517u => ResetReasonEnum.ASSERT, 
					48518u => ResetReasonEnum.TRAP, 
					48519u => ResetReasonEnum.OS_FATAL, 
					48520u => ResetReasonEnum.HCI_RESET, 
					48521u => ResetReasonEnum.POWERON, 
					48528u => ResetReasonEnum.WATCHDOG, 
					_ => ResetReasonEnum.InvalidResetReasonValue, 
				};
			}

			public static uint ResetReasonToValue(ResetReasonEnum enumItem)
			{
				return enumItem switch
				{
					ResetReasonEnum.Bit15_8_Reset_reason_valid_value_Bit7_0 => 48512u, 
					ResetReasonEnum.UNKNOWN => 48513u, 
					ResetReasonEnum.SW_REQUESTED => 48514u, 
					ResetReasonEnum.STACK_OVERVIEW => 48515u, 
					ResetReasonEnum.EXCEPTION => 48516u, 
					ResetReasonEnum.ASSERT => 48517u, 
					ResetReasonEnum.TRAP => 48518u, 
					ResetReasonEnum.OS_FATAL => 48519u, 
					ResetReasonEnum.HCI_RESET => 48520u, 
					ResetReasonEnum.POWERON => 48521u, 
					ResetReasonEnum.WATCHDOG => 48528u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ResetReasonToString(ResetReasonEnum enumItem)
			{
				return enumItem switch
				{
					ResetReasonEnum.Bit15_8_Reset_reason_valid_value_Bit7_0 => "Bit15~8: 0xBD Reset reason valid value. Bit7~0: ", 
					ResetReasonEnum.UNKNOWN => "UNKNOWN", 
					ResetReasonEnum.SW_REQUESTED => "SW_REQUESTED", 
					ResetReasonEnum.STACK_OVERVIEW => "STACK_OVERVIEW", 
					ResetReasonEnum.EXCEPTION => "EXCEPTION", 
					ResetReasonEnum.ASSERT => "ASSERT", 
					ResetReasonEnum.TRAP => "TRAP", 
					ResetReasonEnum.OS_FATAL => "OS_FATAL", 
					ResetReasonEnum.HCI_RESET => "HCI_RESET", 
					ResetReasonEnum.POWERON => "POWERON", 
					ResetReasonEnum.WATCHDOG => "WATCHDOG", 
					_ => "Invalid ResetReason Value", 
				};
			}

			public static string ResetReasonToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0xBD80 = Bit15~8: 0xBD Reset reason valid value. Bit7~0: ");
				stringBuilder.AppendLine("0xBD81 = UNKNOWN");
				stringBuilder.AppendLine("0xBD82 = SW_REQUESTED");
				stringBuilder.AppendLine("0xBD83 = STACK_OVERVIEW");
				stringBuilder.AppendLine("0xBD84 = EXCEPTION");
				stringBuilder.AppendLine("0xBD85 = ASSERT");
				stringBuilder.AppendLine("0xBD86 = TRAP");
				stringBuilder.AppendLine("0xBD87 = OS_FATAL");
				stringBuilder.AppendLine("0xBD88 = HCI_RESET");
				stringBuilder.AppendLine("0xBD89 = POWERON");
				stringBuilder.AppendLine("0xBD90 = WATCHDOG");
				return stringBuilder.ToString();
			}
		}

		private const uint _VendorEventClass = 12u;

		private const uint _Subopcode = 6u;

		private uint _ResetReason;

		private uint _DebugInfo1;

		private uint _DebugInfo2;

		public uint VendorEventClass => 12u;

		public uint Subopcode => 6u;

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

		public uint DebugInfo1
		{
			get
			{
				return _DebugInfo1;
			}
			set
			{
				_DebugInfo1 = value;
			}
		}

		public uint DebugInfo2
		{
			get
			{
				return _DebugInfo2;
			}
			set
			{
				_DebugInfo2 = value;
			}
		}

		public ResetReasonEnum GetResetReasonAsResetReasonEnum()
		{
			return ResetReasonInfo.ValueToResetReason(_ResetReason);
		}

		public void SetResetReasonAsResetReasonEnum(ResetReasonEnum enumItem)
		{
			ResetReason = ResetReasonInfo.ResetReasonToValue(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 ReadResetReasonEventInfo(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.VendorEventClass)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Subopcode)
		{
			return false;
		}
		UserFields.ResetReason = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.DebugInfo1 = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.DebugInfo2 = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		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.VendorEventClass, 8);
		bitBuilder.AddBitsAsUint(UserFields.Subopcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ResetReason, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.DebugInfo1, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.DebugInfo2, 32);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
