using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Sonex.Client.Layouts;

public sealed partial class TitlePage : UserControl
{
    public TitlePage()
    {
        InitializeComponent();

        HeaderBar = new ObservableCollection<ICommandBarElement>();
        SecondaryCommands = new ObservableCollection<ICommandBarElement>();

        SyncCommands();
    }

    public string Title
    {
        get => (string)GetValue(TitleProperty);
        set => SetValue(TitleProperty, value);
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register(
            nameof(Title),
            typeof(string),
            typeof(TitlePage),
            new PropertyMetadata(""));

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

    public static readonly DependencyProperty BodyContentProperty =
        DependencyProperty.Register(
            nameof(BodyContent),
            typeof(object),
            typeof(TitlePage),
            new PropertyMetadata(null));

    public ObservableCollection<ICommandBarElement> HeaderBar
    {
        get => (ObservableCollection<ICommandBarElement>)GetValue(HeaderBarProperty);
        set => SetValue(HeaderBarProperty, value);
    }

    public static readonly DependencyProperty HeaderBarProperty =
        DependencyProperty.Register(
            nameof(HeaderBar),
            typeof(ObservableCollection<ICommandBarElement>),
            typeof(TitlePage),
            new PropertyMetadata(null, OnCommandsDpChanged));

    public ObservableCollection<ICommandBarElement> SecondaryCommands
    {
        get => (ObservableCollection<ICommandBarElement>)GetValue(SecondaryCommandsProperty);
        set => SetValue(SecondaryCommandsProperty, value);
    }

    public static readonly DependencyProperty SecondaryCommandsProperty =
        DependencyProperty.Register(
            nameof(SecondaryCommands),
            typeof(ObservableCollection<ICommandBarElement>),
            typeof(TitlePage),
            new PropertyMetadata(null, OnCommandsDpChanged));

    private static void OnCommandsDpChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var c = (TitlePage)d;

        if (e.OldValue is ObservableCollection<ICommandBarElement> oldCol)
            oldCol.CollectionChanged -= c.PrimaryOrSecondaryChanged;

        if (e.NewValue is ObservableCollection<ICommandBarElement> newCol)
            newCol.CollectionChanged += c.PrimaryOrSecondaryChanged;

        c.SyncCommands();
    }

    private void PrimaryOrSecondaryChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        SyncCommands();
    }

    private void SyncCommands()
    {
        if (HeaderCommandBar is null)
            return;

        HeaderCommandBar.PrimaryCommands.Clear();
        if (HeaderBar != null)
        {
            foreach (var cmd in HeaderBar)
                HeaderCommandBar.PrimaryCommands.Add(cmd);
        }

        HeaderCommandBar.SecondaryCommands.Clear();
        if (SecondaryCommands != null)
        {
            foreach (var cmd in SecondaryCommands)
                HeaderCommandBar.SecondaryCommands.Add(cmd);
        }
    }
}