using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using AxCopilot.ViewModels;

namespace AxCopilot.Views;

public class StatisticsWindow : Window, IComponentConnector
{
	private const int WM_NCLBUTTONDOWN = 161;

	private const int HTBOTTOMRIGHT = 17;

	private readonly StatisticsViewModel _vm;

	internal RadioButton StatsTabMain;

	internal RadioButton StatsTabCommander;

	internal RadioButton StatsTabAgent;

	internal ScrollViewer PanelMain;

	internal ScrollViewer PanelCommander;

	internal ScrollViewer PanelAgent;

	private bool _contentLoaded;

	[DllImport("user32.dll")]
	private static extern void ReleaseCapture();

	[DllImport("user32.dll")]
	private static extern nint SendMessage(nint hWnd, int msg, nint wParam, nint lParam);

	public StatisticsWindow()
	{
		InitializeComponent();
		_vm = new StatisticsViewModel();
		base.DataContext = _vm;
	}

	private void Window_MouseDown(object sender, MouseButtonEventArgs e)
	{
		if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed)
		{
			try
			{
				DragMove();
			}
			catch
			{
			}
		}
	}

	private void ResizeGrip_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		if (e.ButtonState == MouseButtonState.Pressed)
		{
			ReleaseCapture();
			SendMessage(new WindowInteropHelper(this).Handle, 161, 17, IntPtr.Zero);
		}
	}

	private void Close_Click(object sender, RoutedEventArgs e)
	{
		Close();
	}

	private void Refresh_Click(object sender, RoutedEventArgs e)
	{
		_vm.Refresh();
	}

	private void StatsTab_Click(object sender, RoutedEventArgs e)
	{
		if (PanelMain != null && PanelCommander != null && PanelAgent != null)
		{
			PanelMain.Visibility = ((StatsTabMain.IsChecked != true) ? Visibility.Collapsed : Visibility.Visible);
			PanelCommander.Visibility = ((StatsTabCommander.IsChecked != true) ? Visibility.Collapsed : Visibility.Visible);
			PanelAgent.Visibility = ((StatsTabAgent.IsChecked != true) ? Visibility.Collapsed : Visibility.Visible);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/AxCopilot;component/views/statisticswindow.xaml", UriKind.Relative);
			Application.LoadComponent(this, resourceLocator);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		switch (connectionId)
		{
		case 1:
			((StatisticsWindow)target).MouseDown += Window_MouseDown;
			break;
		case 2:
			((Button)target).Click += Refresh_Click;
			break;
		case 3:
			((Button)target).Click += Close_Click;
			break;
		case 4:
			StatsTabMain = (RadioButton)target;
			StatsTabMain.Click += StatsTab_Click;
			break;
		case 5:
			StatsTabCommander = (RadioButton)target;
			StatsTabCommander.Click += StatsTab_Click;
			break;
		case 6:
			StatsTabAgent = (RadioButton)target;
			StatsTabAgent.Click += StatsTab_Click;
			break;
		case 7:
			PanelMain = (ScrollViewer)target;
			break;
		case 8:
			PanelCommander = (ScrollViewer)target;
			break;
		case 9:
			PanelAgent = (ScrollViewer)target;
			break;
		case 10:
			((Border)target).MouseLeftButtonDown += ResizeGrip_MouseLeftButtonDown;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
