using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using NexusShell.Helpers;

namespace NexusShell.Views.Controls;

public partial class NexusWindowChromeButtons
{
    public NexusWindowChromeButtons()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        UpdateMaximizeIcon();
        if (HostWindow is { } window)
        {
            window.StateChanged += (_, _) => UpdateMaximizeIcon();
        }
    }

    private Window? HostWindow => Window.GetWindow(this);

    private void Minimize_OnClick(object sender, RoutedEventArgs e)
    {
        if (HostWindow is { } window)
        {
            NexusAppWindowHelper.Minimize(window);
        }
    }

    private void Maximize_OnClick(object sender, RoutedEventArgs e)
    {
        if (HostWindow is { } window)
        {
            NexusAppWindowHelper.ToggleMaximize(window);
            UpdateMaximizeIcon();
        }
    }

    private void Close_OnClick(object sender, RoutedEventArgs e)
    {
        HostWindow?.Close();
    }

    private void UpdateMaximizeIcon()
    {
        if (HostWindow is not { } window)
        {
            return;
        }

        var maximized = NexusAppWindowHelper.GetIsMaximized(window);
        MaximizeButton.ToolTip = maximized ? "Gendan" : "Maksimer";
        MaximizeIcon.Data = Geometry.Parse(maximized
            ? "M8,6 L16,6 L16,14 L18,14 L18,16 L6,16 L6,8 L8,8 Z M10,8 L10,14 L14,14 L14,10 L10,10 Z"
            : "M6,6 L18,6 L18,18 L6,18 Z M9,9 L15,9 L15,15 L9,15 Z");
    }
}
