using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using AxCopilot.ViewModels;

namespace AxCopilot.Views;

public partial class StatisticsWindow : Window
{
    [DllImport("user32.dll")] private static extern void ReleaseCapture();
    [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    private const int WM_NCLBUTTONDOWN = 0xA1;
    private const int HTBOTTOMRIGHT    = 17;

    private readonly StatisticsViewModel _vm;

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

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

    private void ResizeGrip_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ButtonState != MouseButtonState.Pressed) return;
        ReleaseCapture();
        SendMessage(new WindowInteropHelper(this).Handle, WM_NCLBUTTONDOWN, (IntPtr)HTBOTTOMRIGHT, 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) return;
        PanelMain.Visibility = StatsTabMain.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
        PanelCommander.Visibility = StatsTabCommander.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
        PanelAgent.Visibility = StatsTabAgent.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
    }
}
