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 Crysome.Server.Model;
using Crysome.Server.ViewModel;
using Wpf.Ui.Controls;

namespace Crysome.Server.View;

public class RemoteCameraWindow : FluentWindow, IComponentConnector
{
	private readonly ClientInfo _client;

	private readonly ManageClientsViewModel _vm;

	private string[] _devices = new string[0];

	internal TextBlock TitleText;

	internal Button RefreshBtn;

	internal Button CloseBtn;

	internal ComboBox CameraCombo;

	internal Image CameraImage;

	internal TextBlock StatusText;

	private bool _contentLoaded;

	public RemoteCameraWindow(ClientInfo client, ManageClientsViewModel vm)
	{
		InitializeComponent();
		_client = client;
		_vm = vm;
		((Window)this).Title = "Remote Camera — " + (client?.Address ?? "?");
		TitleText.Text = ((Window)this).Title;
		((FrameworkElement)this).Loaded += delegate
		{
			_vm.Log("RemoteCamera Loaded, calling LoadCameras", "CAM");
			LoadCameras();
		};
	}

	private void LoadCameras()
	{
		_vm.GetCameraDevices(_client, delegate(string[] devs)
		{
			_devices = devs ?? new string[0];
			if (_devices.Length == 0)
			{
				_devices = new string[1] { "(No cameras found)" };
			}
			CameraCombo.Items.Clear();
			for (int i = 0; i < _devices.Length; i++)
			{
				CameraCombo.Items.Add(_devices[i] ?? ("Camera " + i));
			}
			if (CameraCombo.Items.Count > 0)
			{
				CameraCombo.SelectedIndex = 0;
			}
		});
	}

	private void RefreshBtn_Click(object sender, RoutedEventArgs e)
	{
		((UIElement)(object)RefreshBtn).IsEnabled = false;
		StatusText.Text = "Requesting frame...";
		int deviceIndex = ((_devices.Length != 0 && _devices[0] != "(No cameras found)" && CameraCombo.SelectedIndex >= 0) ? CameraCombo.SelectedIndex : 0);
		_vm.RequestCameraFrame(_client, deviceIndex, delegate(byte[] jpegData)
		{
			((UIElement)(object)RefreshBtn).IsEnabled = true;
			if (jpegData == null || jpegData.Length == 0)
			{
				StatusText.Text = "No frame received (no camera?).";
				CameraImage.Source = null;
				return;
			}
			try
			{
				BitmapImage bitmapImage = new BitmapImage();
				bitmapImage.BeginInit();
				bitmapImage.StreamSource = new MemoryStream(jpegData);
				bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
				bitmapImage.EndInit();
				((Freezable)bitmapImage).Freeze();
				CameraImage.Source = bitmapImage;
				StatusText.Text = "Frame received.";
			}
			catch (Exception ex)
			{
				StatusText.Text = "Failed to show frame: " + ex.Message;
			}
		});
	}

	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/remotecamerawindow.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_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		switch (connectionId)
		{
		case 1:
			((Border)target).MouseLeftButtonDown += Header_MouseLeftButtonDown;
			break;
		case 2:
			TitleText = (TextBlock)target;
			break;
		case 3:
			RefreshBtn = (Button)target;
			((ButtonBase)(object)RefreshBtn).Click += RefreshBtn_Click;
			break;
		case 4:
			CloseBtn = (Button)target;
			((ButtonBase)(object)CloseBtn).Click += CloseBtn_Click;
			break;
		case 5:
			CameraCombo = (ComboBox)target;
			break;
		case 6:
			CameraImage = (Image)target;
			break;
		case 7:
			StatusText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
