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

namespace QCRPG.TSL.BT.Packets;

public class CommandCompleteForEdlBurnPatchRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SubOpcodeEnum
		{
			EDL_PATCH_BURN_REQ,
			InvalidSubOpcodeValue
		}

		public static class SubOpcodeInfo
		{
			public static SubOpcodeEnum ValueToSubOpcode(uint value)
			{
				if (value == 26)
				{
					return SubOpcodeEnum.EDL_PATCH_BURN_REQ;
				}
				return SubOpcodeEnum.InvalidSubOpcodeValue;
			}

			public static uint SubOpcodeToValue(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.EDL_PATCH_BURN_REQ)
				{
					return 26u;
				}
				return uint.MaxValue;
			}

			public static string SubOpcodeToString(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.EDL_PATCH_BURN_REQ)
				{
					return "EDL_PATCH_BURN_REQ";
				}
				return "Invalid SubOpcode Value";
			}

			public static string SubOpcodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1A = EDL_PATCH_BURN_REQ");
				return stringBuilder.ToString();
			}
		}

		public enum EdlAppTypeEnum
		{
			EDL_appFLASH,
			EDL_appBOOT,
			EDL_appRAM,
			EDL_appROM,
			InvalidEdlAppTypeValue
		}

		public static class EdlAppTypeInfo
		{
			public static EdlAppTypeEnum ValueToEdlAppType(uint value)
			{
				return value switch
				{
					0u => EdlAppTypeEnum.EDL_appFLASH, 
					1u => EdlAppTypeEnum.EDL_appBOOT, 
					2u => EdlAppTypeEnum.EDL_appRAM, 
					3u => EdlAppTypeEnum.EDL_appROM, 
					_ => EdlAppTypeEnum.InvalidEdlAppTypeValue, 
				};
			}

			public static uint EdlAppTypeToValue(EdlAppTypeEnum enumItem)
			{
				return enumItem switch
				{
					EdlAppTypeEnum.EDL_appFLASH => 0u, 
					EdlAppTypeEnum.EDL_appBOOT => 1u, 
					EdlAppTypeEnum.EDL_appRAM => 2u, 
					EdlAppTypeEnum.EDL_appROM => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EdlAppTypeToString(EdlAppTypeEnum enumItem)
			{
				return enumItem switch
				{
					EdlAppTypeEnum.EDL_appFLASH => "EDL_appFLASH", 
					EdlAppTypeEnum.EDL_appBOOT => "EDL_appBOOT", 
					EdlAppTypeEnum.EDL_appRAM => "EDL_appRAM", 
					EdlAppTypeEnum.EDL_appROM => "EDL_appROM", 
					_ => "Invalid EdlAppType Value", 
				};
			}

			public static string EdlAppTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = EDL_appFLASH");
				stringBuilder.AppendLine("0x1 = EDL_appBOOT");
				stringBuilder.AppendLine("0x2 = EDL_appRAM");
				stringBuilder.AppendLine("0x3 = EDL_appROM");
				return stringBuilder.ToString();
			}
		}

		private const uint _CommandOpcode = 64512u;

		private const uint _SubOpcode = 26u;

		private uint _NumHciCommandPackets;

		private uint _Status;

		private uint _ProductId;

		private uint _EdlAppType;

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

		public uint CommandOpcode => 64512u;

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

		public uint SubOpcode => 26u;

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

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

		public CommandOpcodeEnum GetCommandOpcodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(64512u);
		}

		public EdlErrorCodeEnum GetStatusAsEdlErrorCodeEnum()
		{
			return EdlErrorCodeInfo.ValueToEdlErrorCode(_Status);
		}

		public void SetStatusAsEdlErrorCodeEnum(EdlErrorCodeEnum enumItem)
		{
			Status = EdlErrorCodeInfo.EdlErrorCodeToValue(enumItem);
		}

		public SubOpcodeEnum GetSubOpcodeAsSubOpcodeEnum()
		{
			return SubOpcodeInfo.ValueToSubOpcode(26u);
		}

		public ProductIdEnum GetProductIdAsProductIdEnum()
		{
			return ProductIdInfo.ValueToProductId(_ProductId);
		}

		public void SetProductIdAsProductIdEnum(ProductIdEnum enumItem)
		{
			ProductId = ProductIdInfo.ProductIdToValue(enumItem);
		}

		public EdlAppTypeEnum GetEdlAppTypeAsEdlAppTypeEnum()
		{
			return EdlAppTypeInfo.ValueToEdlAppType(_EdlAppType);
		}

		public void SetEdlAppTypeAsEdlAppTypeEnum(EdlAppTypeEnum enumItem)
		{
			EdlAppType = EdlAppTypeInfo.EdlAppTypeToValue(enumItem);
		}
	}

	private const uint _EventCode = 14u;

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

	public uint EventCode => 14u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumHciCommandPackets = 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;
		}
		UserFields.NumHciCommandPackets = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.CommandOpcode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.ProductId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.EdlAppType = 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.NumHciCommandPackets, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CommandOpcode, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.ProductId, 8);
		bitBuilder.AddBitsAsUint(UserFields.EdlAppType, 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.EdlAppType > 3)
		{
			list.Add("Maximum allowed value for EDL App Type is 0x3");
		}
		return list;
	}
}
