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

namespace VisitedPageSave
{
    public sealed partial class MainPage : SaveStatePage
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs args)
        {
            // Włączenie przycisków
            forwardButton.IsEnabled = this.Frame.CanGoForward;
            backButton.IsEnabled = this.Frame.CanGoBack;

            // Utworzenie klucza słownika
            int pageKey = this.Frame.BackStackDepth;

            if (args.NavigationMode != NavigationMode.New)
            {
                // Pobranie słownika stanu dla tej strony
                pageState = pages[pageKey];

                // Pobranie stanu strony ze słownika
                txtbox.Text = pageState["TextBoxText"] as string;
            }

            base.OnNavigatedTo(args);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs args)
        {
            pageState.Clear();

            // Zapisanie stanu strony w słowniku
            pageState.Add("TextBoxText", txtbox.Text);

            base.OnNavigatedFrom(args);
        }

        void OnGotoButtonClick(object sender, RoutedEventArgs args)
        {
            this.Frame.Navigate(typeof(SecondPage));
        }

        void OnForwardButtonClick(object sender, RoutedEventArgs args)
        {
            this.Frame.GoForward();
        }

        void OnBackButtonClick(object sender, RoutedEventArgs args)
        {
            this.Frame.GoBack();
        }
    }
}
