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 RemoteChatWindow : FluentWindow, IComponentConnector
{
	private readonly ClientInfo _client;

	private readonly ManageClientsViewModel _vm;

	internal TextBlock TitleText;

	internal Button CloseBtn;

	internal ListBox ChatList;

	internal TextBox MessageBox;

	internal Button SendBtn;

	internal TextBlock StatusText;

	private bool _contentLoaded;

	public RemoteChatWindow(ClientInfo client, ManageClientsViewModel vm)
	{
		InitializeComponent();
		_client = client;
		_vm = vm;
		((Window)this).Title = "Chat — " + (client?.Address ?? "?");
		TitleText.Text = ((Window)this).Title;
		_vm.RegisterChatHandler(_client, OnChatMessage);
		((Window)this).Closed += delegate
		{
			_vm.UnregisterChatHandler(_client);
		};
	}

	private void OnChatMessage(string from, string msg)
	{
		((DispatcherObject)this).Dispatcher.BeginInvoke((Delegate)(Action)delegate
		{
			ChatList.Items.Add("[" + from + "]: " + msg);
			if (ChatList.Items.Count > 0)
			{
				ChatList.ScrollIntoView(ChatList.Items[ChatList.Items.Count - 1]);
			}
		}, Array.Empty<object>());
	}

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

	private void MessageBox_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
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		if ((int)e.Key == 6 && !((Enum)e.KeyboardDevice.Modifiers).HasFlag((Enum)(object)(ModifierKeys)4))
		{
			e.Handled = true;
			SendMessage();
		}
	}

	private void SendMessage()
	{
		string text = MessageBox?.Text?.Trim();
		if (!string.IsNullOrEmpty(text))
		{
			MessageBox.Clear();
			_vm.SendChatMessage(_client, text);
			OnChatMessage("You", text);
		}
	}

	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/remotechatwindow.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_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: 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:
			ChatList = (ListBox)target;
			break;
		case 5:
			MessageBox = (TextBox)target;
			MessageBox.KeyDown += MessageBox_KeyDown;
			break;
		case 6:
			SendBtn = (Button)target;
			((ButtonBase)(object)SendBtn).Click += SendBtn_Click;
			break;
		case 7:
			StatusText = (TextBlock)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
