using MapsSamples.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;

namespace MapsSamples
{
    /// <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()
        {
            C1.UWP.LicenseManager.Key =
                "AB4BHgIeB3dNAGEAcABzAFMAYQBtAHAAbABlAHMAeLU4USymCyKJ8mAslCs8fESR" +
                "hNb6LY7YcoJJHynOi6f/hFgymRZwlZzgbvNrZO3sFYfNGoGL2pmx1eZ2uG50+a3D" +
                "NWCsqPMeJCM/888wjI92SslpMwW5xO+nemJa7C9dGtjgP6hZDPCvDHLGP2UTmpO8" +
                "HzDI4SIbR4qqSnV7u9tIztB4SsIWRwS+V0qW98TKiGW03M7J++WBZzzVXdlSVGZq" +
                "I0i2FH40omFiTvC+7GskxnGDZYOtcNANI0j2tRjqthDB3CtaqwF8AWyLbGuaYueb" +
                "McLSnAiZ6EpPEpVlzhmsUYOB4kW2Sen8u6FcsFZlY+howFSuXRy+konhRDjMIxz8" +
                "ttSpL3/R1BKm40sxY9sAYOqkKZvFZAVDD4wgEHXwi8nIxbU05Z9pQuZVpH1C05mv" +
                "UTSDyiBZ33wj8xsz6kDUNJBtaaK5TqsQ7pMkxV/siKAqgJHD7D5RfQbBiNI/qtIV" +
                "4m0DiaD/sosEF2FjLPlG/ZgBLzuFz+nXPIAJQuA2VNuVP2izmAt5ALpKilco5lMd" +
                "dOL4p407OruOG+P2jo1whfP7XSnr7BkBwR+Yfd+D4QdlvPaEfguvU0wEDE23dq3L" +
                "ltt5rHxJopbNYFHJGHuB796oZkY7dHrAW9p81btM1B8sIssxIHvQtqSKXTSa3Da8" +
                "4S1PGYZ0LURi/bBiOvswggVVMIIEPaADAgECAhBBA3jSJjZZehbbJsa9EJSLMA0G" +
                "CSqGSIb3DQEBBQUAMIG0MQswCQYDVQQGEwJVUzEXMBUGA1UEChMOVmVyaVNpZ24s" +
                "IEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsxOzA5BgNVBAsT" +
                "MlRlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy52ZXJpc2lnbi5jb20vcnBhIChj" +
                "KTEwMS4wLAYDVQQDEyVWZXJpU2lnbiBDbGFzcyAzIENvZGUgU2lnbmluZyAyMDEw" +
                "IENBMB4XDTE0MTIxMTAwMDAwMFoXDTE1MTIyMjIzNTk1OVowgYYxCzAJBgNVBAYT" +
                "AkpQMQ8wDQYDVQQIEwZNaXlhZ2kxGDAWBgNVBAcTD1NlbmRhaSBJenVtaS1rdTEX" +
                "MBUGA1UEChQOR3JhcGVDaXR5IGluYy4xGjAYBgNVBAsUEVRvb2xzIERldmVsb3Bt" +
                "ZW50MRcwFQYDVQQDFA5HcmFwZUNpdHkgaW5jLjCCASIwDQYJKoZIhvcNAQEBBQAD" +
                "ggEPADCCAQoCggEBAMEC9splW7KVpAqRB6VVB7XJm8+a60x78HZWNmvAjs/ermp7" +
                "pbh8M9HYuEVTRj8H4baXxFYq4i75e5kNr7s+nGbJ5UfM6CldqbeRsQ4zvRb6npfr" +
                "tcOBwfYOx0woeYQfI6VzLVzhPbGjUDK6qdHZLf/EcvSIKvRbu3ILOE3kuux07gQk" +
                "b++rrSBUyJKXU8nW8c+K+9sfWqHYPb8FF2IazWxhmfIdIoDNKyjr3r/wIn3jQejp" +
                "qfamiHNsU/O26KS0EcvCS5CFOKDT6vvdnAkek3kyLWef+ca/cbFo74vawYGtmw2w" +
                "zw/hcwlANIQaCAxz+5JHQZ3SEg8LLSZ+C8E4T+8CAwEAAaOCAY0wggGJMAkGA1Ud" +
                "EwQCMAAwDgYDVR0PAQH/BAQDAgeAMCsGA1UdHwQkMCIwIKAeoByGGmh0dHA6Ly9z" +
                "Zi5zeW1jYi5jb20vc2YuY3JsMGYGA1UdIARfMF0wWwYLYIZIAYb4RQEHFwMwTDAj" +
                "BggrBgEFBQcCARYXaHR0cHM6Ly9kLnN5bWNiLmNvbS9jcHMwJQYIKwYBBQUHAgIw" +
                "GQwXaHR0cHM6Ly9kLnN5bWNiLmNvbS9ycGEwEwYDVR0lBAwwCgYIKwYBBQUHAwMw" +
                "VwYIKwYBBQUHAQEESzBJMB8GCCsGAQUFBzABhhNodHRwOi8vc2Yuc3ltY2QuY29t" +
                "MCYGCCsGAQUFBzAChhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNydDAfBgNVHSME" +
                "GDAWgBTPmanqeyb0S8mOj9fwBSbv49KnnTAdBgNVHQ4EFgQUAFrwraXUeDX1hBKo" +
                "LAUO+FYbjo4wEQYJYIZIAYb4QgEBBAQDAgQQMBYGCisGAQQBgjcCARsECDAGAQEA" +
                "AQH/MA0GCSqGSIb3DQEBBQUAA4IBAQCIwphaN45b5ViKsRfCA6hbVeqMhNCJmL64" +
                "mqxdYvw3gsF4oOCDoiM6kWhy1NoUd2Lpcr9PjfGJ55ZX4sZlqoxeflTpHgnX8MPJ" +
                "uKpDBsk7WkhiEu65fBwb+g9wQHMMfgY0gjHIAQ2ZEf2PJ6TTSFcvj24wh73dsyqc" +
                "mj290fpwDtHaF2jE5pq/tQBOJ8vDkN46wt4qMhG6nIpOgc9KngyLLdEO7V34KM1N" +
                "b+sN9JkqTWXNoNqlOf7QjYHwlw7Ku7W3kIGK+f1kP1xqTBpXwsBE5sLlnujP+JAq" +
                "V8aKlBJDKRI3IhlrG9CJUmqFNhmZ2f5QE5jAvPQeXevLnMYiqJrgMIICxDCCAayg" +
                "AwIBAgIEDu7u7jANBgkqhkiG9w0BAQUFADAkMSIwIAYDVQQDDBlHQy1VV1AgSW50" +
                "ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjE0MDE1NVoXDTE3MDUxMjE0MDE1NVow" +
                "JDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjCCASIwDQYJKoZI" +
                "hvcNAQEBBQADggEPADCCAQoCggEBAKqj/XBkoAvxQcAD0HWQOo6kCplxdgMv7ZTv" +
                "VwKjt8esHFPyiYDTfcJu6ZdOn9mt4XimrEZzohacNVdlkbZXL4VhNu78GxddkGP2" +
                "T+UuI9MIioVELkOSZxLu1Z6sgY9u55zRGDRD27j7dh/gXnlfIbFK2uKcC+bL28XB" +
                "6PrwYi/01IgnlJ02HKDeoo4eDYyZn1yUo3PiOl0O+7P1Lk0T1MF7/aqPmV5RNYH8" +
                "ThE4msjqMFkBfcYk2xLlSXxP43Pay8b4OLnqaPXoZeBrujFM4bZI5LMwnRubzSaD" +
                "9Ex9RjjTRh45+7TVHCb2quBcylbAfGMC7G/y7g/VhlBac8uWMW8CAwEAATANBgkq" +
                "hkiG9w0BAQUFAAOCAQEAapjSnkVc+yo8NrVvUUJejaVR0JipwnBSMoxXP+7GVPI3" +
                "RFwZ4z5uPKdLAwfX7qqKZYxJpugBljmq5arx5+sU59+yryzbjnSbS/n8ZAErX5B0" +
                "dl2DZ4fDLa9LhKPXoBARE69xr++fQLpif7PBjIhHrJsP/Sa2AZc6FwNQXB50h232" +
                "3jD5Ca0/FJ8D1J/QmKHABBvBTtMxvvm4hH7dwUHsOoTHCJARgrslkY13UQzwhXeg" +
                "nk4K14KUk//ZJEax3fv6msrqpln5tU20E8r+EJ03FafhbK03pPpJ4NGzizNJIgr2" +
                "s6jDi+eurffSSS1axvZIeVtFJAY3BuGMNnSlCmFiTw==";
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used when the application is launched to open a specific file, to display
        /// search results, and so forth.
        /// </summary>
        /// <param name="args">Details about the launch request and process.</param>
        protected override async void OnLaunched(LaunchActivatedEventArgs args)
        {
            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");

                if (args.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
                if (!rootFrame.Navigate(typeof(MainPage), "AllItems"))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        /// <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();
        }
    }
}
