using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AxCopilot.Views;

public class RegionSelectWindow : Window, IComponentConnector
{
	private readonly System.Drawing.Rectangle _screenBounds;

	private Point _startPoint;

	private Point _endPoint;

	private bool _isDragging;

	internal Canvas RootCanvas;

	internal System.Windows.Shapes.Rectangle OverlayTop;

	internal System.Windows.Shapes.Rectangle OverlayBottom;

	internal System.Windows.Shapes.Rectangle OverlayLeft;

	internal System.Windows.Shapes.Rectangle OverlayRight;

	internal System.Windows.Shapes.Rectangle SelectionBorder;

	internal Border GuideText;

	internal Border SizeLabel;

	internal TextBlock SizeLabelText;

	private bool _contentLoaded;

	public System.Drawing.Rectangle? SelectedRect { get; private set; }

	public RegionSelectWindow(Bitmap fullScreenshot, System.Drawing.Rectangle screenBounds)
	{
		InitializeComponent();
		_screenBounds = screenBounds;
		base.Left = screenBounds.X;
		base.Top = screenBounds.Y;
		base.Width = screenBounds.Width;
		base.Height = screenBounds.Height;
		RootCanvas.Background = CreateFrozenBrush(fullScreenshot);
		OverlayTop.Width = screenBounds.Width;
		OverlayTop.Height = screenBounds.Height;
		OverlayBottom.Width = screenBounds.Width;
		OverlayLeft.Width = 0.0;
		OverlayRight.Width = 0.0;
		base.Loaded += delegate
		{
			Canvas.SetLeft(GuideText, (base.Width - GuideText.ActualWidth) / 2.0);
			Canvas.SetTop(GuideText, (base.Height - GuideText.ActualHeight) / 2.0);
		};
	}

	private static ImageBrush CreateFrozenBrush(Bitmap bmp)
	{
		using MemoryStream memoryStream = new MemoryStream();
		bmp.Save(memoryStream, ImageFormat.Bmp);
		memoryStream.Position = 0L;
		BitmapImage bitmapImage = new BitmapImage();
		bitmapImage.BeginInit();
		bitmapImage.StreamSource = memoryStream;
		bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
		bitmapImage.EndInit();
		((Freezable)bitmapImage).Freeze();
		ImageBrush imageBrush = new ImageBrush(bitmapImage)
		{
			Stretch = Stretch.None,
			AlignmentX = AlignmentX.Left,
			AlignmentY = AlignmentY.Top
		};
		((Freezable)imageBrush).Freeze();
		return imageBrush;
	}

	private void Window_MouseDown(object sender, MouseButtonEventArgs e)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		if (e.ChangedButton == MouseButton.Left)
		{
			_startPoint = e.GetPosition(RootCanvas);
			_isDragging = true;
			GuideText.Visibility = Visibility.Collapsed;
			SelectionBorder.Visibility = Visibility.Visible;
			SizeLabel.Visibility = Visibility.Visible;
			CaptureMouse();
		}
	}

	private void Window_MouseMove(object sender, MouseEventArgs e)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		if (_isDragging)
		{
			Point position = e.GetPosition(RootCanvas);
			UpdateSelection(_startPoint, position);
		}
	}

	private void Window_MouseUp(object sender, MouseButtonEventArgs e)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		if (_isDragging && e.ChangedButton == MouseButton.Left)
		{
			_isDragging = false;
			ReleaseMouseCapture();
			Rect val = MakeRect(b: _endPoint = e.GetPosition(RootCanvas), a: _startPoint);
			if (!(((Rect)(ref val)).Width < 4.0) && !(((Rect)(ref val)).Height < 4.0))
			{
				SizeLabelText.Text += "  ·  ↑↓←→ 미세조정  ·  Enter 확정";
			}
		}
	}

	private void Window_KeyDown(object sender, KeyEventArgs e)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Invalid comparison between Unknown and I4
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Invalid comparison between Unknown and I4
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Expected I4, but got Unknown
		//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
		if ((int)e.Key == 13)
		{
			SelectedRect = null;
			Close();
		}
		else
		{
			if (_isDragging || SelectionBorder.Visibility != Visibility.Visible)
			{
				return;
			}
			double num = 0.0;
			double num2 = 0.0;
			double num3 = ((!((Enum)Keyboard.Modifiers).HasFlag((Enum)(object)(ModifierKeys)4)) ? 1 : 10);
			Key key = e.Key;
			Key val = key;
			if ((int)val != 6)
			{
				switch (val - 23)
				{
				default:
					return;
				case 0:
					num = 0.0 - num3;
					break;
				case 2:
					num = num3;
					break;
				case 1:
					num2 = 0.0 - num3;
					break;
				case 3:
					num2 = num3;
					break;
				}
				_endPoint = new Point(((Point)(ref _endPoint)).X + num, ((Point)(ref _endPoint)).Y + num2);
				UpdateSelection(_startPoint, _endPoint);
				e.Handled = true;
			}
			else
			{
				Rect val2 = MakeRect(_startPoint, _endPoint);
				if (((Rect)(ref val2)).Width >= 4.0 && ((Rect)(ref val2)).Height >= 4.0)
				{
					DpiScale dpi = VisualTreeHelper.GetDpi(this);
					SelectedRect = new System.Drawing.Rectangle((int)(((Rect)(ref val2)).X * dpi.DpiScaleX) + _screenBounds.X, (int)(((Rect)(ref val2)).Y * dpi.DpiScaleY) + _screenBounds.Y, (int)(((Rect)(ref val2)).Width * dpi.DpiScaleX), (int)(((Rect)(ref val2)).Height * dpi.DpiScaleY));
				}
				Close();
			}
		}
	}

	private void UpdateSelection(Point a, Point b)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		Rect val = MakeRect(a, b);
		Canvas.SetLeft(SelectionBorder, ((Rect)(ref val)).X);
		Canvas.SetTop(SelectionBorder, ((Rect)(ref val)).Y);
		SelectionBorder.Width = ((Rect)(ref val)).Width;
		SelectionBorder.Height = ((Rect)(ref val)).Height;
		OverlayTop.Width = base.Width;
		OverlayTop.Height = ((Rect)(ref val)).Y;
		OverlayBottom.Width = base.Width;
		OverlayBottom.Height = base.Height - ((Rect)(ref val)).Y - ((Rect)(ref val)).Height;
		Canvas.SetTop(OverlayBottom, ((Rect)(ref val)).Y + ((Rect)(ref val)).Height);
		OverlayLeft.Width = ((Rect)(ref val)).X;
		OverlayLeft.Height = ((Rect)(ref val)).Height;
		Canvas.SetTop(OverlayLeft, ((Rect)(ref val)).Y);
		OverlayRight.Width = base.Width - ((Rect)(ref val)).X - ((Rect)(ref val)).Width;
		OverlayRight.Height = ((Rect)(ref val)).Height;
		Canvas.SetLeft(OverlayRight, ((Rect)(ref val)).X + ((Rect)(ref val)).Width);
		Canvas.SetTop(OverlayRight, ((Rect)(ref val)).Y);
		DpiScale dpi = VisualTreeHelper.GetDpi(this);
		int value = (int)(((Rect)(ref val)).Width * dpi.DpiScaleX);
		int value2 = (int)(((Rect)(ref val)).Height * dpi.DpiScaleY);
		SizeLabelText.Text = $"{value} × {value2}";
		Canvas.SetLeft(SizeLabel, ((Rect)(ref val)).X + ((Rect)(ref val)).Width + 8.0);
		Canvas.SetTop(SizeLabel, ((Rect)(ref val)).Y + ((Rect)(ref val)).Height + 4.0);
	}

	private static Rect MakeRect(Point a, Point b)
	{
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		return new Rect(Math.Min(((Point)(ref a)).X, ((Point)(ref b)).X), Math.Min(((Point)(ref a)).Y, ((Point)(ref b)).Y), Math.Abs(((Point)(ref b)).X - ((Point)(ref a)).X), Math.Abs(((Point)(ref b)).Y - ((Point)(ref a)).Y));
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/AxCopilot;component/views/regionselectwindow.xaml", UriKind.Relative);
			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:
			((RegionSelectWindow)target).MouseDown += Window_MouseDown;
			((RegionSelectWindow)target).MouseMove += Window_MouseMove;
			((RegionSelectWindow)target).MouseUp += Window_MouseUp;
			((RegionSelectWindow)target).KeyDown += Window_KeyDown;
			break;
		case 2:
			RootCanvas = (Canvas)target;
			break;
		case 3:
			OverlayTop = (System.Windows.Shapes.Rectangle)target;
			break;
		case 4:
			OverlayBottom = (System.Windows.Shapes.Rectangle)target;
			break;
		case 5:
			OverlayLeft = (System.Windows.Shapes.Rectangle)target;
			break;
		case 6:
			OverlayRight = (System.Windows.Shapes.Rectangle)target;
			break;
		case 7:
			SelectionBorder = (System.Windows.Shapes.Rectangle)target;
			break;
		case 8:
			GuideText = (Border)target;
			break;
		case 9:
			SizeLabel = (Border)target;
			break;
		case 10:
			SizeLabelText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
