using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace FoxTube.Controls
{
    public sealed partial class ContentFrame : UserControl
    {
        public Frame Frame => content;
        public LoadingPage LoadingPage => loading;
        public event NavigatedEventHandler Navigated;

        public ContentFrame()
        {
            InitializeComponent();
            content.Navigated += (s, e) => Navigated?.Invoke(s, e);
        }

        private void Content_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            loading.Refresh();
        }

        private void Loading_RefreshPage(object sender, RoutedEventArgs e)
        {
            content.Navigate(content.CurrentSourcePageType, (content.Content as INavigationPage).Parameter);
            content.BackStack.RemoveAt(content.BackStack.Count - 1);
        }

        public void Refresh()
        {
            Loading_RefreshPage(this, null);
        }
    }
}
