using System;
using System.Text;

namespace QC.QMSLPhone;

public struct Message
{
	public MessageType type;

	public string source;

	public string data;

	public DateTime timestamp;

	public uint sequenceNumber;

	public bool truncated;

	public Message(MessageType type, string source, bool data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, char data)
		: this(type, source, new string(data, 1))
	{
	}

	public Message(MessageType type, string source, short data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, ushort data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, int data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, uint data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, long data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, ulong data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, float data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, double data)
		: this(type, source, data.ToString())
	{
	}

	public Message(MessageType type, string source, Exception ex)
		: this(type, source, "Source = " + ex.Source + ", Message = " + ex.Message)
	{
	}

	public Message(MessageType type, string source, string data)
	{
		int maxMessageSize = Logger.MaxMessageSize;
		timestamp = DateTime.Now;
		sequenceNumber = 0u;
		this.type = type;
		this.source = source;
		if (data.Length > maxMessageSize)
		{
			this.data = data.Substring(0, maxMessageSize);
			truncated = true;
		}
		else
		{
			this.data = data;
			truncated = false;
		}
	}

	public Message(MessageType type, string source, byte data)
		: this(type, source, new byte[1] { data }, 1)
	{
	}

	public Message(MessageType type, string source, byte[] data)
		: this(type, source, data, data.Length)
	{
	}

	public Message(MessageType type, string source, byte[] data, int dataLength)
	{
		int num = 0;
		int maxMessageSize = Logger.MaxMessageSize;
		timestamp = DateTime.Now;
		sequenceNumber = 0u;
		this.type = type;
		this.source = source;
		this.data = "";
		for (int i = 0; i < data.Length; i++)
		{
			byte b = data[i];
			num++;
			if (num <= dataLength)
			{
				if (this.data.Length == 0)
				{
					this.data = "0x" + b.ToString("X2");
				}
				else
				{
					this.data = this.data + " 0x" + b.ToString("X2");
				}
				if (this.data.Length > maxMessageSize)
				{
					this.data.Remove(maxMessageSize);
					truncated = true;
					return;
				}
			}
		}
		truncated = false;
	}

	public override string ToString()
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.AppendLine("TI Library Log Message");
		stringBuilder.AppendLine("Timestamp: " + timestamp.ToString());
		stringBuilder.AppendLine("Sequence Number: " + sequenceNumber);
		stringBuilder.AppendLine("Truncated: " + truncated);
		stringBuilder.AppendLine("Type: " + type);
		stringBuilder.AppendLine("Source: " + source.ToString());
		stringBuilder.AppendLine("Data: " + data.ToString());
		return stringBuilder.ToString();
	}
}
