using DateTimeSelector_UIATest.Common;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Grid App template is documented at http://go.microsoft.com/fwlink/?LinkId=234226

namespace DateTimeSelector_UIATest
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton Application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            C1.UWP.LicenseManager.Key =
                "AEIBQgJCB5tDADEARABhAHQAZQBUAGkAbQBlAFMAZQBsAGUAYwB0AG8AcgBfAFQA" +
                "ZQBzAHQAUwBhAG0AcABsAGUAWW1UQNeniSRdzOtrq9EwPjDSggiCjQOaDqWQgCS5" +
                "aITJjx7MHkfNpjPYHPf5nN0+4q6y9MJKUZllLdv9N681p/kUY3eYg5u8rIuJqr21" +
                "Q+TwBe09Xr3TSJj2HGIRGugtAUj3iFnL5KAJujlr3d3bRapf5CnlDw5KpgkZQrvD" +
                "ljAfPIlQ9nXuSzDnv90Qyul5XZq6q7aStIM/M2aLDtKrB0aXGY2KYEA+tOXnVpd5" +
                "ht1QVszxpsScCcSYHc++pdrWK/LBeFXH914pHgmUZXnVdNcSGYOxH/bp1kRXD0PR" +
                "O/PTs33K7S6VQlvDtYkbYqPEo0FUjaOoF24ZxoTVfYhKmbOHIP4NHCd/qCk33lE4" +
                "ghhaap0kuU1nt6aSFFDzqdV0Ov/vkOTUi1I72rOXP/mwIe2o+Rr0mWXxHKxWCw07" +
                "8Hx8MQ5/X7CULNGonl3oNuiQsuZXVYpsBG2r2EfnXhNVyqGdZ2WqLEyHnojB/FfI" +
                "vbIoiuql8j4Ds+yAfT48Drof4mVbgwVPHUFaAESKbeOJgkSfJFQm9e7atL41+4VI" +
                "Cu4sldjxD4xtCxSkmdkpispH0Svpiiyt+0W0M+3zaTxowJkZThAUThyHh4hNUDd4" +
                "P+RucNXwBEczJ7P7UrmfAyaRxNlPLMk5Sh1dFI7Y5pY8+G2PdoMliNf8KvtB3lai" +
                "fp0wggVVMIIEPaADAgECAhBBA3jSJjZZehbbJsa9EJSLMA0GCSqGSIb3DQEBBQUA" +
                "MIG0MQswCQYDVQQGEwJVUzEXMBUGA1UEChMOVmVyaVNpZ24sIEluYy4xHzAdBgNV" +
                "BAsTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsxOzA5BgNVBAsTMlRlcm1zIG9mIHVz" +
                "ZSBhdCBodHRwczovL3d3dy52ZXJpc2lnbi5jb20vcnBhIChjKTEwMS4wLAYDVQQD" +
                "EyVWZXJpU2lnbiBDbGFzcyAzIENvZGUgU2lnbmluZyAyMDEwIENBMB4XDTE0MTIx" +
                "MTAwMDAwMFoXDTE1MTIyMjIzNTk1OVowgYYxCzAJBgNVBAYTAkpQMQ8wDQYDVQQI" +
                "EwZNaXlhZ2kxGDAWBgNVBAcTD1NlbmRhaSBJenVtaS1rdTEXMBUGA1UEChQOR3Jh" +
                "cGVDaXR5IGluYy4xGjAYBgNVBAsUEVRvb2xzIERldmVsb3BtZW50MRcwFQYDVQQD" +
                "FA5HcmFwZUNpdHkgaW5jLjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB" +
                "AMEC9splW7KVpAqRB6VVB7XJm8+a60x78HZWNmvAjs/ermp7pbh8M9HYuEVTRj8H" +
                "4baXxFYq4i75e5kNr7s+nGbJ5UfM6CldqbeRsQ4zvRb6npfrtcOBwfYOx0woeYQf" +
                "I6VzLVzhPbGjUDK6qdHZLf/EcvSIKvRbu3ILOE3kuux07gQkb++rrSBUyJKXU8nW" +
                "8c+K+9sfWqHYPb8FF2IazWxhmfIdIoDNKyjr3r/wIn3jQejpqfamiHNsU/O26KS0" +
                "EcvCS5CFOKDT6vvdnAkek3kyLWef+ca/cbFo74vawYGtmw2wzw/hcwlANIQaCAxz" +
                "+5JHQZ3SEg8LLSZ+C8E4T+8CAwEAAaOCAY0wggGJMAkGA1UdEwQCMAAwDgYDVR0P" +
                "AQH/BAQDAgeAMCsGA1UdHwQkMCIwIKAeoByGGmh0dHA6Ly9zZi5zeW1jYi5jb20v" +
                "c2YuY3JsMGYGA1UdIARfMF0wWwYLYIZIAYb4RQEHFwMwTDAjBggrBgEFBQcCARYX" +
                "aHR0cHM6Ly9kLnN5bWNiLmNvbS9jcHMwJQYIKwYBBQUHAgIwGQwXaHR0cHM6Ly9k" +
                "LnN5bWNiLmNvbS9ycGEwEwYDVR0lBAwwCgYIKwYBBQUHAwMwVwYIKwYBBQUHAQEE" +
                "SzBJMB8GCCsGAQUFBzABhhNodHRwOi8vc2Yuc3ltY2QuY29tMCYGCCsGAQUFBzAC" +
                "hhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNydDAfBgNVHSMEGDAWgBTPmanqeyb0" +
                "S8mOj9fwBSbv49KnnTAdBgNVHQ4EFgQUAFrwraXUeDX1hBKoLAUO+FYbjo4wEQYJ" +
                "YIZIAYb4QgEBBAQDAgQQMBYGCisGAQQBgjcCARsECDAGAQEAAQH/MA0GCSqGSIb3" +
                "DQEBBQUAA4IBAQCIwphaN45b5ViKsRfCA6hbVeqMhNCJmL64mqxdYvw3gsF4oOCD" +
                "oiM6kWhy1NoUd2Lpcr9PjfGJ55ZX4sZlqoxeflTpHgnX8MPJuKpDBsk7WkhiEu65" +
                "fBwb+g9wQHMMfgY0gjHIAQ2ZEf2PJ6TTSFcvj24wh73dsyqcmj290fpwDtHaF2jE" +
                "5pq/tQBOJ8vDkN46wt4qMhG6nIpOgc9KngyLLdEO7V34KM1Nb+sN9JkqTWXNoNql" +
                "Of7QjYHwlw7Ku7W3kIGK+f1kP1xqTBpXwsBE5sLlnujP+JAqV8aKlBJDKRI3Ihlr" +
                "G9CJUmqFNhmZ2f5QE5jAvPQeXevLnMYiqJrgMIICxDCCAaygAwIBAgIEDu7u7jAN" +
                "BgkqhkiG9w0BAQUFADAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxv" +
                "cGVyMB4XDTE2MDUxMjE0MTAyM1oXDTE3MDUxMjE0MTAyM1owJDEiMCAGA1UEAwwZ" +
                "R0MtVVdQIEludGVybmFsIERldmVsb3BlcjCCASIwDQYJKoZIhvcNAQEBBQADggEP" +
                "ADCCAQoCggEBAK1cRaVk+vLZPe0zUnyQA94vR46wSsKjiEyfGzcJxL1PLTtrIbKO" +
                "Mg0yXsedmB7zWCiL60eHnraSDBPkiEiHYMGQ+UTK5UepSH9MHnBTFOtyTE+oIAbN" +
                "XyJ7ak4DGnuVnPJkaQQXRn0Kr+/ZyFuHbG6hTS8Or4NA4ZLrMVNbgA0VHJuE/1kR" +
                "tzrgFv6twWGWU9IH6xapVz6DWcf2yFNQLTUjaqbq23IlDfCzXC2f+ZksKRqm9ybc" +
                "38vrinI71F/zdEwPg3jP2HWJlPEROOkRAJvw84USZXbDyxM+G+Nw1EY1pJ5cZK2f" +
                "Ay2pAUD9yAsnUaKaT/r2M3dcqj7vGJYFIi8CAwEAATANBgkqhkiG9w0BAQUFAAOC" +
                "AQEAgMf9oYtkPmKDDeCjNrOcXtkK4Odrx/SH8EwqFC6fbm0UdT0Ikk/lcu0DooDW" +
                "0EkeZX8ov504idSKQJXSdvvl0adGMkYoL7mhzX20Z1a/aMe9NPIdZg4KC+1g+XNA" +
                "Gjp0ITDtdK16unFwtnkGq5GNcbv6vY9IGv1UcbXjeSy1DwmmD28C4cDAJgOiQ2St" +
                "s8MxdDH4YaD+cSVz7sbGZVXviiNujCI8OrpBWhMjW+agLr2GgqgS4ztObN8NNJUX" +
                "DHkaILCoOP5OgIr2XPb0t8zhRsKzwsUdzekfBBTeV0AKS3ZLbXHKHDirhqHhjsW6" +
                "10wMci+zhLmKNER/QznSB0UDEA==";
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override async void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            // Show graphics profiling information while debugging.
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // Display the current frame rate counters
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active

            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                //Associate the frame with a SuspensionManager key                                
                SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // Restore the saved session state only when appropriate
                    try
                    {
                        await SuspensionManager.RestoreAsync();
                    }
                    catch (SuspensionManagerException)
                    {
                        //Something went wrong restoring state.
                        //Assume there is no state and continue
                    }
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(GroupedItemsPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception(Strings.InitializationException + " " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private async void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            await SuspensionManager.SaveAsync();
            deferral.Complete();
        }
    }
}
