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

namespace Serein.Plus.Helpers;

/// <remarks>
/// Written by: Osama Javed https://stackoverflow.com/questions/6367876
/// </remarks>
public static class DisableNavigation
{
    public static bool GetDisable(DependencyObject o)
    {
        return (bool)o.GetValue(DisableProperty);
    }

    public static void SetDisable(DependencyObject o, bool value)
    {
        o.SetValue(DisableProperty, value);
    }

    public static readonly DependencyProperty DisableProperty = DependencyProperty.RegisterAttached(
        "Disable",
        typeof(bool),
        typeof(DisableNavigation),
        new PropertyMetadata(false, DisableChanged)
    );

    public static void DisableChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var frame = (Frame)sender;
        frame.Navigated += DontNavigate;
        frame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
    }

    public static void DontNavigate(object sender, NavigationEventArgs e)
    {
        ((Frame)sender).NavigationService.RemoveBackEntry();
    }
}
