using System;
using Windows.Devices.Sensors;
using Windows.Graphics.Display;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;

namespace OrientationAndOrientation
{
    public sealed partial class MainPage : Page
    {
        SimpleOrientationSensor simpleOrientationSensor = SimpleOrientationSensor.GetDefault();

        public MainPage()
        {
            this.InitializeComponent();

            // Inicjalizacja SimpleOrientationSensor
            if (simpleOrientationSensor != null)
            {
                SetOrientationSensorText(simpleOrientationSensor.GetCurrentOrientation());
                simpleOrientationSensor.OrientationChanged += OnSimpleOrientationChanged;
            }

            // Inicjalizacja DisplayProperties 
            SetDisplayOrientationText(DisplayProperties.CurrentOrientation);
            DisplayProperties.OrientationChanged += OnDisplayPropertiesOrientationChanged;
        }

        // Metoda obsługująca SimpleOrientationSensor
        async void OnSimpleOrientationChanged(SimpleOrientationSensor sender,
                                                SimpleOrientationSensorOrientationChangedEventArgs args)
        {
            await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    SetOrientationSensorText(args.Orientation);
                });
        }

        void SetOrientationSensorText(SimpleOrientation simpleOrientation)
        {
            orientationSensorTextBlock.Text = simpleOrientation.ToString();
        }

        // Metoda obsługująca DisplayProperties 
        void OnDisplayPropertiesOrientationChanged(object sender)
        {
            SetDisplayOrientationText(DisplayProperties.CurrentOrientation);
        }

        void SetDisplayOrientationText(DisplayOrientations displayOrientation)
        {
            displayOrientationTextBlock.Text = displayOrientation.ToString();
        }
    }
}
