using System.Runtime.InteropServices;
using Decal.Interop.Net;

namespace Decal.Adapter.NetParser;

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IMessage2))]
[ProgId("DecalAdapter.MessageWrapper")]
[Guid("76142307-98E3-494d-8320-3C801010221D")]
public class MessageWrapper : IMessage2, IMessage
{
	private Message msg;

	internal Message Wrapped => msg;

	public MessageRoot Begin => new MessageRootWrapper(this);

	public int Count => msg.Count;

	public byte[] RawData => msg.RawData;

	public int Type => msg.Type;

	// IMessage/IMessage2 properties - parameterless accessors for the COM interface
	object IMessage.Value => get_Value(0);
	IMessageMember IMessage.Struct => get_Struct(0);
	byte[] IMessage.RawValue => get_RawValue(0);
	string IMessage.FieldName => get_FieldName(0);
	object IMessage2.Value => get_Value(0);
	IMessageMember IMessage2.Struct => get_Struct(0);
	byte[] IMessage2.RawValue => get_RawValue(0);
	string IMessage2.FieldName => get_FieldName(0);

	public MessageWrapper()
	{
	}

	internal MessageWrapper(Message msg)
	{
		this.msg = msg;
		if (!this.msg.mStruct.mParsed)
		{
			this.msg.mStruct.Parse();
		}
	}

	public string get_FieldName(int Index)
	{
		return msg.Name(Index);
	}

	public byte[] get_RawValue(object vElement)
	{
		return msg.RawValue(msg.mStruct.ObjectToIndex(vElement));
	}

	public IMessageMember get_Struct(object vElement)
	{
		int num = msg.mStruct.ObjectToIndex(vElement);
		if (num < 0)
		{
			throw new COMHResultException((HResults)1);
		}
		return new MessageMemberWrapper(msg.Struct(num), this);
	}

	public object get_Value(object vElement)
	{
		int num = msg.mStruct.ObjectToIndex(vElement);
		if (num >= 0)
		{
			return msg.Value<object>(num);
		}
		return null;
	}
}
