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

namespace DataPassingAndReturning
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Enabled;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs args)
        {
            if (args.SourcePageType.Equals(typeof(DialogPage)))
                (args.Content as DialogPage).Completed += OnDialogPageCompleted;

            base.OnNavigatedFrom(args);
        }

        void OnGotoButtonClick(object sender, RoutedEventArgs args)
        {
            // Tworzenie obiektu PassData
            PassData passData = new PassData();

            // Ustawienie właściwości InitializeColor na podstawie kontrolek RadioButton
            foreach (UIElement child in radioStack.Children)
                if ((child as RadioButton).IsChecked.Value)
                    passData.InitializeColor = (Color)(child as RadioButton).Tag;

            // Przekazanie obiektu do Navigate
            this.Frame.Navigate(typeof(DialogPage), passData);
        }

        void OnDialogPageCompleted(object sender, ReturnData args)
        {
            // Ustawienie tła na podstawie zwróconego koloru
            contentGrid.Background = new SolidColorBrush(args.ReturnColor);

            // Ustawienie elementu RadioButton na podstawie zwrócnego koloru
            foreach (UIElement child in radioStack.Children)
                if ((Color)(child as RadioButton).Tag == args.ReturnColor)
                    (child as RadioButton).IsChecked = true;

            (sender as DialogPage).Completed -= OnDialogPageCompleted;
        }
    }
}
