using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AxCopilot.Views;

public class EyeDropperWindow : Window, IComponentConnector
{
	private struct POINT
	{
		public int X;

		public int Y;
	}

	private readonly System.Drawing.Rectangle _screenBounds;

	internal Canvas RootCanvas;

	internal Border Magnifier;

	internal Ellipse MagPreview;

	internal Border HexLabel;

	internal TextBlock HexLabelText;

	private bool _contentLoaded;

	public System.Drawing.Color? PickedColor { get; private set; }

	public double PickX { get; private set; }

	public double PickY { get; private set; }

	[DllImport("user32.dll")]
	private static extern bool GetCursorPos(out POINT lpPoint);

	[DllImport("gdi32.dll")]
	private static extern uint GetPixel(nint hdc, int x, int y);

	[DllImport("user32.dll")]
	private static extern nint GetDC(nint hWnd);

	[DllImport("user32.dll")]
	private static extern int ReleaseDC(nint hWnd, nint hDC);

	public EyeDropperWindow()
	{
		InitializeComponent();
		_screenBounds = GetAllScreenBounds();
		base.Left = _screenBounds.X;
		base.Top = _screenBounds.Y;
		base.Width = _screenBounds.Width;
		base.Height = _screenBounds.Height;
		base.Loaded += delegate
		{
			Magnifier.Visibility = Visibility.Visible;
			HexLabel.Visibility = Visibility.Visible;
		};
	}

	private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		Point position = e.GetPosition(RootCanvas);
		System.Drawing.Color screenPixelColor = GetScreenPixelColor();
		Canvas.SetLeft(Magnifier, ((Point)(ref position)).X + 16.0);
		Canvas.SetTop(Magnifier, ((Point)(ref position)).Y - 100.0);
		Canvas.SetLeft(HexLabel, ((Point)(ref position)).X + 16.0);
		Canvas.SetTop(HexLabel, ((Point)(ref position)).Y - 10.0);
		System.Windows.Media.Color color = System.Windows.Media.Color.FromRgb(screenPixelColor.R, screenPixelColor.G, screenPixelColor.B);
		MagPreview.Fill = new SolidColorBrush(color);
		HexLabelText.Text = $"#{screenPixelColor.R:X2}{screenPixelColor.G:X2}{screenPixelColor.B:X2}";
	}

	private void Window_MouseDown(object sender, MouseButtonEventArgs e)
	{
		if (e.ChangedButton == MouseButton.Left)
		{
			PickedColor = GetScreenPixelColor();
			GetCursorPos(out var lpPoint);
			PickX = lpPoint.X;
			PickY = lpPoint.Y;
			Close();
		}
		else if (e.ChangedButton == MouseButton.Right)
		{
			PickedColor = null;
			Close();
		}
	}

	private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Invalid comparison between Unknown and I4
		if ((int)e.Key == 13)
		{
			PickedColor = null;
			Close();
		}
	}

	private static System.Drawing.Color GetScreenPixelColor()
	{
		GetCursorPos(out var lpPoint);
		nint dC = GetDC(IntPtr.Zero);
		try
		{
			uint pixel = GetPixel(dC, lpPoint.X, lpPoint.Y);
			int red = (int)(pixel & 0xFF);
			int green = (int)((pixel >> 8) & 0xFF);
			int blue = (int)((pixel >> 16) & 0xFF);
			return System.Drawing.Color.FromArgb(red, green, blue);
		}
		finally
		{
			ReleaseDC(IntPtr.Zero, dC);
		}
	}

	private static System.Drawing.Rectangle GetAllScreenBounds()
	{
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		Screen[] allScreens = Screen.AllScreens;
		foreach (Screen screen in allScreens)
		{
			System.Drawing.Rectangle bounds = screen.Bounds;
			num = Math.Min(num, bounds.Left);
			num2 = Math.Min(num2, bounds.Top);
			num3 = Math.Max(num3, bounds.Right);
			num4 = Math.Max(num4, bounds.Bottom);
		}
		return new System.Drawing.Rectangle(num, num2, num3 - num, num4 - num2);
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/AxCopilot;component/views/eyedropperwindow.xaml", UriKind.Relative);
			System.Windows.Application.LoadComponent(this, resourceLocator);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		switch (connectionId)
		{
		case 1:
			((EyeDropperWindow)target).MouseDown += Window_MouseDown;
			((EyeDropperWindow)target).MouseMove += Window_MouseMove;
			((EyeDropperWindow)target).KeyDown += Window_KeyDown;
			break;
		case 2:
			RootCanvas = (Canvas)target;
			break;
		case 3:
			Magnifier = (Border)target;
			break;
		case 4:
			MagPreview = (Ellipse)target;
			break;
		case 5:
			HexLabel = (Border)target;
			break;
		case 6:
			HexLabelText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
