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

namespace QCRPG.TSL.BT.Packets;

public class DbfwScoPacketInfo : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PacketTypeM2sEnum
		{
			HV1,
			HV3,
			EV3,
			E2_EV3,
			Others,
			InvalidPacketTypeM2sValue
		}

		public static class PacketTypeM2sInfo
		{
			public static PacketTypeM2sEnum ValueToPacketTypeM2s(uint value)
			{
				return value switch
				{
					0u => PacketTypeM2sEnum.HV1, 
					1u => PacketTypeM2sEnum.HV3, 
					2u => PacketTypeM2sEnum.EV3, 
					3u => PacketTypeM2sEnum.E2_EV3, 
					255u => PacketTypeM2sEnum.Others, 
					_ => PacketTypeM2sEnum.InvalidPacketTypeM2sValue, 
				};
			}

			public static uint PacketTypeM2sToValue(PacketTypeM2sEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeM2sEnum.HV1 => 0u, 
					PacketTypeM2sEnum.HV3 => 1u, 
					PacketTypeM2sEnum.EV3 => 2u, 
					PacketTypeM2sEnum.E2_EV3 => 3u, 
					PacketTypeM2sEnum.Others => 255u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketTypeM2sToString(PacketTypeM2sEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeM2sEnum.HV1 => "HV1", 
					PacketTypeM2sEnum.HV3 => "HV3", 
					PacketTypeM2sEnum.EV3 => "EV3", 
					PacketTypeM2sEnum.E2_EV3 => "2EV3", 
					PacketTypeM2sEnum.Others => "Others", 
					_ => "Invalid PacketTypeM2s Value", 
				};
			}

			public static string PacketTypeM2sToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = HV1");
				stringBuilder.AppendLine("0x1 = HV3");
				stringBuilder.AppendLine("0x2 = EV3");
				stringBuilder.AppendLine("0x3 = 2EV3");
				stringBuilder.AppendLine("0xFF = Others");
				return stringBuilder.ToString();
			}
		}

		public enum PacketTypeS2mEnum
		{
			HV1,
			HV3,
			EV3,
			E2_EV3,
			Others,
			InvalidPacketTypeS2mValue
		}

		public static class PacketTypeS2mInfo
		{
			public static PacketTypeS2mEnum ValueToPacketTypeS2m(uint value)
			{
				return value switch
				{
					0u => PacketTypeS2mEnum.HV1, 
					1u => PacketTypeS2mEnum.HV3, 
					2u => PacketTypeS2mEnum.EV3, 
					3u => PacketTypeS2mEnum.E2_EV3, 
					255u => PacketTypeS2mEnum.Others, 
					_ => PacketTypeS2mEnum.InvalidPacketTypeS2mValue, 
				};
			}

			public static uint PacketTypeS2mToValue(PacketTypeS2mEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeS2mEnum.HV1 => 0u, 
					PacketTypeS2mEnum.HV3 => 1u, 
					PacketTypeS2mEnum.EV3 => 2u, 
					PacketTypeS2mEnum.E2_EV3 => 3u, 
					PacketTypeS2mEnum.Others => 255u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketTypeS2mToString(PacketTypeS2mEnum enumItem)
			{
				return enumItem switch
				{
					PacketTypeS2mEnum.HV1 => "HV1", 
					PacketTypeS2mEnum.HV3 => "HV3", 
					PacketTypeS2mEnum.EV3 => "EV3", 
					PacketTypeS2mEnum.E2_EV3 => "2EV3", 
					PacketTypeS2mEnum.Others => "Others", 
					_ => "Invalid PacketTypeS2m Value", 
				};
			}

			public static string PacketTypeS2mToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = HV1");
				stringBuilder.AppendLine("0x1 = HV3");
				stringBuilder.AppendLine("0x2 = EV3");
				stringBuilder.AppendLine("0x3 = 2EV3");
				stringBuilder.AppendLine("0xFF = Others");
				return stringBuilder.ToString();
			}
		}

		private const uint _DbfwEventCode = 144u;

		private const uint _SubEventCode = 50u;

		private uint _PacketTypeM2s;

		private uint _PacketTypeS2m;

		public uint DbfwEventCode => 144u;

		public uint SubEventCode => 50u;

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

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

		public PacketTypeM2sEnum GetPacketTypeM2sAsPacketTypeM2sEnum()
		{
			return PacketTypeM2sInfo.ValueToPacketTypeM2s(_PacketTypeM2s);
		}

		public void SetPacketTypeM2sAsPacketTypeM2sEnum(PacketTypeM2sEnum enumItem)
		{
			PacketTypeM2s = PacketTypeM2sInfo.PacketTypeM2sToValue(enumItem);
		}

		public PacketTypeS2mEnum GetPacketTypeS2mAsPacketTypeS2mEnum()
		{
			return PacketTypeS2mInfo.ValueToPacketTypeS2m(_PacketTypeS2m);
		}

		public void SetPacketTypeS2mAsPacketTypeS2mEnum(PacketTypeS2mEnum enumItem)
		{
			PacketTypeS2m = PacketTypeS2mInfo.PacketTypeS2mToValue(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 DbfwScoPacketInfoInfo(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.DbfwEventCode)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubEventCode)
		{
			return false;
		}
		UserFields.PacketTypeM2s = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketTypeS2m = 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.DbfwEventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubEventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketTypeM2s, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketTypeS2m, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
