using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Wpf.Ui.Controls;

namespace Crysome.Server.View;

public class ScreenshotViewerWindow : FluentWindow, IComponentConnector
{
	internal TextBlock TitleText;

	internal Button CloseBtn;

	internal Image ScreenshotImage;

	internal TextBlock StatusText;

	private bool _contentLoaded;

	public ScreenshotViewerWindow(string title, byte[] imageData)
	{
		InitializeComponent();
		((Window)this).Title = title ?? "Screenshot";
		TitleText.Text = title ?? "Screenshot";
		if (imageData != null && imageData.Length != 0)
		{
			try
			{
				BitmapImage bitmapImage = new BitmapImage();
				bitmapImage.BeginInit();
				bitmapImage.StreamSource = new MemoryStream(imageData);
				bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
				bitmapImage.EndInit();
				((Freezable)bitmapImage).Freeze();
				ScreenshotImage.Source = bitmapImage;
				StatusText.Text = $"{bitmapImage.PixelWidth} × {bitmapImage.PixelHeight}";
				return;
			}
			catch (Exception ex)
			{
				StatusText.Text = "Failed to load image: " + ex.Message;
				return;
			}
		}
		StatusText.Text = "No image data received.";
	}

	private void CloseBtn_Click(object sender, RoutedEventArgs e)
	{
		((Window)this).Close();
	}

	private void Header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		try
		{
			((Window)this).DragMove();
		}
		catch
		{
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "9.0.14.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/Crysome.Server;component/view/screenshotviewerwindow.xaml", UriKind.Relative);
			Application.LoadComponent(this, resourceLocator);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "9.0.14.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Expected O, but got Unknown
		switch (connectionId)
		{
		case 1:
			((Border)target).MouseLeftButtonDown += Header_MouseLeftButtonDown;
			break;
		case 2:
			TitleText = (TextBlock)target;
			break;
		case 3:
			CloseBtn = (Button)target;
			((ButtonBase)(object)CloseBtn).Click += CloseBtn_Click;
			break;
		case 4:
			ScreenshotImage = (Image)target;
			break;
		case 5:
			StatusText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
