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

namespace QCRPG.TSL.BT.Packets;

public class EdlStatusResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EdlAppTypeEnum
		{
			FLASH,
			BOOT,
			RAM,
			ROM,
			RESERVED,
			InvalidEdlAppTypeValue
		}

		public static class EdlAppTypeInfo
		{
			public static EdlAppTypeEnum ValueToEdlAppType(uint value)
			{
				switch (value)
				{
				case 0u:
					return EdlAppTypeEnum.FLASH;
				case 1u:
					return EdlAppTypeEnum.BOOT;
				case 2u:
					return EdlAppTypeEnum.RAM;
				case 3u:
					return EdlAppTypeEnum.ROM;
				default:
					if (value >= 4 && value <= 255)
					{
						return EdlAppTypeEnum.RESERVED;
					}
					return EdlAppTypeEnum.InvalidEdlAppTypeValue;
				}
			}

			public static uint EdlAppTypeToValue(EdlAppTypeEnum enumItem)
			{
				return enumItem switch
				{
					EdlAppTypeEnum.FLASH => 0u, 
					EdlAppTypeEnum.BOOT => 1u, 
					EdlAppTypeEnum.RAM => 2u, 
					EdlAppTypeEnum.ROM => 3u, 
					EdlAppTypeEnum.RESERVED => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EdlAppTypeToString(EdlAppTypeEnum enumItem)
			{
				return enumItem switch
				{
					EdlAppTypeEnum.FLASH => "FLASH", 
					EdlAppTypeEnum.BOOT => "BOOT", 
					EdlAppTypeEnum.RAM => "RAM", 
					EdlAppTypeEnum.ROM => "ROM", 
					EdlAppTypeEnum.RESERVED => "RESERVED", 
					_ => "Invalid EdlAppType Value", 
				};
			}

			public static string EdlAppTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = FLASH");
				stringBuilder.AppendLine("0x1 = BOOT");
				stringBuilder.AppendLine("0x2 = RAM");
				stringBuilder.AppendLine("0x3 = ROM");
				stringBuilder.AppendLine("0x4-0xFF = RESERVED");
				return stringBuilder.ToString();
			}
		}

		private const uint _VendorEventClass = 0u;

		private const uint _SubOpcode = 0u;

		private uint _Status;

		private uint _ProductId;

		private uint _EdlAppType;

		public uint VendorEventClass => 0u;

		public uint SubOpcode => 0u;

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

		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 EdlErrorCodeEnum GetStatusAsEdlErrorCodeEnum()
		{
			return EdlErrorCodeInfo.ValueToEdlErrorCode(_Status);
		}

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

		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 = 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 EdlStatusResponseInfo(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.Status = bitBuilder.RemoveBitsAsUint(8);
		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.VendorEventClass, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Status, 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();
	}
}
