#define TRACE
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using Decal.Interop.Core;
using Microsoft.CSharp;
using Microsoft.Win32;

namespace Decal.Adapter.Support;

internal static class Util
{
	private static int mTrace;

	internal static int TraceLevel
	{
		get
		{
			return mTrace;
		}
		set
		{
			mTrace = value;
		}
	}

	internal static Color ColorFromBGR(int color)
	{
		return Color.FromArgb(color & 0xFF, (color & 0xFF00) >> 8, (color & 0xFF0000) >> 16);
	}

	internal static int ColorToBGR(Color color)
	{
		return (color.B << 16) | (color.G << 8) | color.R;
	}

	internal static Rectangle toRectangle(tagRECT tr)
	{
		return new Rectangle(tr.left, tr.top, tr.right - tr.left, tr.bottom - tr.top);
	}

	internal static tagRECT toTagRECT(Rectangle r)
	{
		tagRECT result = default(tagRECT);
		result.top = r.Top;
		result.left = r.Left;
		result.bottom = r.Bottom;
		result.right = r.Right;
		return result;
	}

	internal static T UnboxTo<T>(object obj)
	{
		try
		{
			return (T)obj;
		}
		catch (InvalidCastException)
		{
			TypeConverter converter = new CSharpCodeProvider().GetConverter(obj.GetType());
			if (converter.CanConvertTo(typeof(T)))
			{
				return (T)converter.ConvertTo(obj, typeof(T));
			}
			WriteLine("Error Converting object type {0} to {1}", obj.GetType(), typeof(T));
			throw;
		}
	}

	internal static void InitializeTracing(string key, string value, int defaultValue)
	{
		try
		{
			TraceLevel = (int)Registry.GetValue(key, value, defaultValue);
		}
		catch
		{
			TraceLevel = 0;
		}
	}

	public static void WriteLine(string fmt, params object[] args)
	{
		WriteLine(1, fmt, args);
	}

	public static void WriteLine(int traceLevel, string fmt, params object[] args)
	{
		WriteLine(traceLevel, string.Format(fmt, args));
	}

	public static void WriteLine(int traceLevel, string msg)
	{
		Trace.WriteLineIf(mTrace >= traceLevel, msg);
	}

	public static void Write(string fmt, params object[] args)
	{
		Write(1, string.Format(fmt, args));
	}

	public static void Write(int traceLevel, string msg)
	{
		Trace.WriteIf(mTrace >= traceLevel, msg);
	}

	public static void SafeFireEvent(object sender, EventHandler eventHandler, EventArgs args)
	{
		if (eventHandler == null)
		{
			return;
		}
		Delegate[] invocationList = eventHandler.GetInvocationList();
		for (int i = 0; i < invocationList.Length; i++)
		{
			EventHandler eventHandler2 = (EventHandler)invocationList[i];
			try
			{
				eventHandler2(sender, args);
			}
			catch (Exception ex)
			{
				WriteLine("SafeFire exception: {0}", ex.ToString());
			}
		}
	}

	public static void SafeFireEvent<T>(object sender, EventHandler<T> eventHandler, T args) where T : EventArgs
	{
		if (eventHandler == null)
		{
			return;
		}
		Delegate[] invocationList = eventHandler.GetInvocationList();
		for (int i = 0; i < invocationList.Length; i++)
		{
			EventHandler<T> eventHandler2 = (EventHandler<T>)invocationList[i];
			try
			{
				eventHandler2(sender, args);
			}
			catch (Exception ex)
			{
				WriteLine("SafeFire exception: {0}", ex.ToString());
			}
		}
	}

	public static void SafeFireEvent<T>(object sender, EventHandler<T> eventHandler, T args, ref bool eaten) where T : EatableEventArgs
	{
		if (eventHandler == null)
		{
			return;
		}
		Delegate[] invocationList = eventHandler.GetInvocationList();
		for (int i = 0; i < invocationList.Length; i++)
		{
			EventHandler<T> eventHandler2 = (EventHandler<T>)invocationList[i];
			try
			{
				eventHandler2(sender, args);
				if (args.Eat)
				{
					eaten = true;
				}
			}
			catch (Exception ex)
			{
				WriteLine("SafeFire exception: {0}", ex.ToString());
			}
		}
	}
}
