using System.Windows;
using System.Windows.Controls;

namespace NexusShell.Controls;

public partial class NexusContentLayout : UserControl
{
    public static readonly DependencyProperty MainContentProperty =
        DependencyProperty.Register(nameof(MainContent), typeof(object), typeof(NexusContentLayout),
            new PropertyMetadata(null));

    public static readonly DependencyProperty SideNavContentProperty =
        DependencyProperty.Register(nameof(SideNavContent), typeof(object), typeof(NexusContentLayout),
            new PropertyMetadata(null));

    public static readonly DependencyProperty RightPanelContentProperty =
        DependencyProperty.Register(nameof(RightPanelContent), typeof(object), typeof(NexusContentLayout),
            new PropertyMetadata(null));

    public static readonly DependencyProperty ShowSideNavProperty =
        DependencyProperty.Register(nameof(ShowSideNav), typeof(bool), typeof(NexusContentLayout),
            new PropertyMetadata(false, OnShowSideNavChanged));

    public static readonly DependencyProperty ShowRightPanelProperty =
        DependencyProperty.Register(nameof(ShowRightPanel), typeof(bool), typeof(NexusContentLayout),
            new PropertyMetadata(false, OnShowRightPanelChanged));

    public NexusContentLayout()
    {
        InitializeComponent();
    }

    public object? MainContent
    {
        get => GetValue(MainContentProperty);
        set => SetValue(MainContentProperty, value);
    }

    public object? SideNavContent
    {
        get => GetValue(SideNavContentProperty);
        set => SetValue(SideNavContentProperty, value);
    }

    public object? RightPanelContent
    {
        get => GetValue(RightPanelContentProperty);
        set => SetValue(RightPanelContentProperty, value);
    }

    public bool ShowSideNav
    {
        get => (bool)GetValue(ShowSideNavProperty);
        set => SetValue(ShowSideNavProperty, value);
    }

    public bool ShowRightPanel
    {
        get => (bool)GetValue(ShowRightPanelProperty);
        set => SetValue(ShowRightPanelProperty, value);
    }

    private static void OnShowSideNavChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is NexusContentLayout layout)
        {
            layout.UpdatePanels();
        }
    }

    private static void OnShowRightPanelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is NexusContentLayout layout)
        {
            layout.UpdatePanels();
        }
    }

    private void UpdatePanels()
    {
        SideNavColumn.Width = ShowSideNav ? new GridLength(220) : new GridLength(0);
        SideNavHost.Visibility = ShowSideNav ? Visibility.Visible : Visibility.Collapsed;

        RightPanelColumn.Width = ShowRightPanel ? new GridLength(280) : new GridLength(0);
        RightPanelHost.Visibility = ShowRightPanel ? Visibility.Visible : Visibility.Collapsed;
    }
}
