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

namespace QCRPG.TSL.BT.Packets;

public class EdlBurnTopcfgRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum WriteOptEnableEnum
		{
			Write_To_Flash,
			Write_To_OTP,
			InvalidWriteOptEnableValue
		}

		public static class WriteOptEnableInfo
		{
			public static WriteOptEnableEnum ValueToWriteOptEnable(uint value)
			{
				if (value == 0)
				{
					return WriteOptEnableEnum.Write_To_Flash;
				}
				if (value >= 1 && value <= uint.MaxValue)
				{
					return WriteOptEnableEnum.Write_To_OTP;
				}
				return WriteOptEnableEnum.InvalidWriteOptEnableValue;
			}

			public static uint WriteOptEnableToValue(WriteOptEnableEnum enumItem)
			{
				return enumItem switch
				{
					WriteOptEnableEnum.Write_To_Flash => 0u, 
					WriteOptEnableEnum.Write_To_OTP => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string WriteOptEnableToString(WriteOptEnableEnum enumItem)
			{
				return enumItem switch
				{
					WriteOptEnableEnum.Write_To_Flash => "Write_To_Flash", 
					WriteOptEnableEnum.Write_To_OTP => "Write_To_OTP", 
					_ => "Invalid WriteOptEnable Value", 
				};
			}

			public static string WriteOptEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Write_To_Flash");
				stringBuilder.AppendLine("0x1-0xFFFFFFFF = Write_To_OTP");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 28u;

		private uint _Address;

		private uint _Length;

		private uint _WriteOptEnable;

		private uint _OtpOffset;

		public uint SubOpcode => 28u;

		public uint Address
		{
			get
			{
				return _Address;
			}
			set
			{
				_Address = value;
			}
		}

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				_Length = value;
			}
		}

		public uint WriteOptEnable
		{
			get
			{
				return _WriteOptEnable;
			}
			set
			{
				_WriteOptEnable = value;
			}
		}

		public uint OtpOffset
		{
			get
			{
				return _OtpOffset;
			}
			set
			{
				_OtpOffset = value;
			}
		}

		public WriteOptEnableEnum GetWriteOptEnableAsWriteOptEnableEnum()
		{
			return WriteOptEnableInfo.ValueToWriteOptEnable(_WriteOptEnable);
		}

		public void SetWriteOptEnableAsWriteOptEnableEnum(WriteOptEnableEnum enumItem)
		{
			WriteOptEnable = WriteOptEnableInfo.WriteOptEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 64512u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(EdlStatusResponse),
		typeof(CommandCompleteForNop)
	};

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new EdlBurnTopcfgRequestInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64512u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			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.SubOpcode)
		{
			return false;
		}
		UserFields.Address = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Length = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.WriteOptEnable = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.OtpOffset = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Address, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Length, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.WriteOptEnable, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.OtpOffset, 32);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
