using System;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

namespace SimpleContextDialog
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        void OnTextBlockRightTapped(object sender, RightTappedRoutedEventArgs args)
        {
            StackPanel stackPanel = new StackPanel();

            // Utworzenie dwóch kontrolek Button i dodanie ich do elementu StackPanel
            Button btn1 = new Button
            {
                Content = "Większa czcionka",
                Tag = 1.2,
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(12)
            };
            btn1.Click += OnButtonClick;
            stackPanel.Children.Add(btn1);

            Button btn2 = new Button
            {
                Content = "Mniejsza czcionka",
                Tag = 1 / 1.2,
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(12)
            };
            btn2.Click += OnButtonClick;
            stackPanel.Children.Add(btn2);

            // Utworzenie trzech kontrolek Button i dodanie ich do elementu StackPanel
            string[] names = { "Czerwony", "Zielony", "Niebieski" };
            Color[] colors = { Colors.Red, Colors.Green, Colors.Blue };

            for (int i = 0; i < names.Length; i++)
            {
                RadioButton radioButton = new RadioButton
                {
                    Content = names[i],
                    Foreground = new SolidColorBrush(colors[i]),
                    IsChecked = (textBlock.Foreground as SolidColorBrush).Color == colors[i],
                    Margin = new Thickness(12)
                };
                radioButton.Checked += OnRadioButtonChecked;
                stackPanel.Children.Add(radioButton);
            }

            // Utworzenie obiektu Border dla obiektu StackPanel
            Border border = new Border
            {
                Child = stackPanel,
                Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
                BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
                BorderThickness = new Thickness(1),
                Padding = new Thickness(24),
            };

            // Utworzenie obiektu Popup
            Popup popup = new Popup
            {
                Child = border,
                IsLightDismissEnabled = true
            };

            // Dostosowanie lokalizacji na podstawie rozmiaru treści
            border.Loaded += (loadedSender, loadedArgs) =>
                {
                    // Pobranie początkowej pozycji góra-lewo
                    Point point = args.GetPosition(this);
                    point.X -= border.ActualWidth / 2;
                    point.Y -= border.ActualHeight;

                    //Pozostawienie marginesu o szerokości co najmniej jednej czwartej cala
                    popup.HorizontalOffset = 
                        Math.Min(this.ActualWidth - border.ActualWidth - 24, 
                            Math.Max(24, point.X));

                    popup.VerticalOffset = 
                        Math.Min(this.ActualHeight - border.ActualHeight - 24,
                            Math.Max(24, point.Y));

                    // Ustawienie fokusu na pierwszy element
                    btn1.Focus(FocusState.Programmatic);
                };

            // Otwarcie okna Popup
            popup.IsOpen = true;
        }

        void OnButtonClick(object sender, RoutedEventArgs args)
        {
            textBlock.FontSize *= (double)(sender as Button).Tag;
        }

        void OnRadioButtonChecked(object sender, RoutedEventArgs args)
        {
            textBlock.Foreground = (sender as RadioButton).Foreground;
        }
    }
}
