using FlexRadarIntro.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 FlexRadarIntro
{
    /// <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 =
                "ACQBJAIkB31GAGwAZQB4AFIAYQBkAGEAcgBJAG4AdAByAG8AV+Hccv3IVp1q5RQP" +
                "onmLuzect+QMy8MDby4pQsAV9za01jUgmNMT1VIcpxDUB6UzeKnMYPsdCjBbeVZu" +
                "TI1qbn41EDVAHMTx15HQGfSQHyCLyxwczbX0BeTu0Jsnq1ditqb2raq/p4beNPwN" +
                "vd89ewCioUIkf3OAx1b33EcSBr+Rnnqqnh7VVT960vcCjlBuOJBEoo+DhHL3r2RT" +
                "YSk17zw/Mqha/mcqtR47MwMx253NgfBMi1V6AnLVS9MFiCQlODtvP3E6A+9GooK1" +
                "kP+lnogl4sy2Z1Ui4E4JoUKabbGDNP0BzjlMyvE7zCdoZxl5dXQGsoyok+W3WCJp" +
                "UC8H0ATqiYxcZg6qXMAwsoSRx5AD7HIbYIjU8MKKk1Jzs77WrRB1139riAFG2ITt" +
                "g/VPW+up2abV5SlQR4RRAACL4SBcTDwrrcADAONS8ZAzqq7RHhZB8YEBDn6mEHb6" +
                "ypzH2w15OlmBy0RlFy4mTo/j5i4t5IW7vdFzD3eqTPMdUMZna9vtHPB837m72ZId" +
                "reygKbcvQnCtBx+E+lK68LNfshgDDvB8EBKGaCGFTIb5XuxdfLZpA0egCy5C4ipB" +
                "QOdpWqietPXnKVPunS2vaDaSdNZQS0KS+bcGrspAymDGbAGJ/dwgWWtXto1eSQte" +
                "6D7axAqdYHeUAbUMuAujxoxW92UwggVVMIIEPaADAgECAhBBA3jSJjZZehbbJsa9" +
                "EJSLMA0GCSqGSIb3DQEBBQUAMIG0MQswCQYDVQQGEwJVUzEXMBUGA1UEChMOVmVy" +
                "aVNpZ24sIEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsxOzA5" +
                "BgNVBAsTMlRlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy52ZXJpc2lnbi5jb20v" +
                "cnBhIChjKTEwMS4wLAYDVQQDEyVWZXJpU2lnbiBDbGFzcyAzIENvZGUgU2lnbmlu" +
                "ZyAyMDEwIENBMB4XDTE0MTIxMTAwMDAwMFoXDTE1MTIyMjIzNTk1OVowgYYxCzAJ" +
                "BgNVBAYTAkpQMQ8wDQYDVQQIEwZNaXlhZ2kxGDAWBgNVBAcTD1NlbmRhaSBJenVt" +
                "aS1rdTEXMBUGA1UEChQOR3JhcGVDaXR5IGluYy4xGjAYBgNVBAsUEVRvb2xzIERl" +
                "dmVsb3BtZW50MRcwFQYDVQQDFA5HcmFwZUNpdHkgaW5jLjCCASIwDQYJKoZIhvcN" +
                "AQEBBQADggEPADCCAQoCggEBAMEC9splW7KVpAqRB6VVB7XJm8+a60x78HZWNmvA" +
                "js/ermp7pbh8M9HYuEVTRj8H4baXxFYq4i75e5kNr7s+nGbJ5UfM6CldqbeRsQ4z" +
                "vRb6npfrtcOBwfYOx0woeYQfI6VzLVzhPbGjUDK6qdHZLf/EcvSIKvRbu3ILOE3k" +
                "uux07gQkb++rrSBUyJKXU8nW8c+K+9sfWqHYPb8FF2IazWxhmfIdIoDNKyjr3r/w" +
                "In3jQejpqfamiHNsU/O26KS0EcvCS5CFOKDT6vvdnAkek3kyLWef+ca/cbFo74va" +
                "wYGtmw2wzw/hcwlANIQaCAxz+5JHQZ3SEg8LLSZ+C8E4T+8CAwEAAaOCAY0wggGJ" +
                "MAkGA1UdEwQCMAAwDgYDVR0PAQH/BAQDAgeAMCsGA1UdHwQkMCIwIKAeoByGGmh0" +
                "dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3JsMGYGA1UdIARfMF0wWwYLYIZIAYb4RQEH" +
                "FwMwTDAjBggrBgEFBQcCARYXaHR0cHM6Ly9kLnN5bWNiLmNvbS9jcHMwJQYIKwYB" +
                "BQUHAgIwGQwXaHR0cHM6Ly9kLnN5bWNiLmNvbS9ycGEwEwYDVR0lBAwwCgYIKwYB" +
                "BQUHAwMwVwYIKwYBBQUHAQEESzBJMB8GCCsGAQUFBzABhhNodHRwOi8vc2Yuc3lt" +
                "Y2QuY29tMCYGCCsGAQUFBzAChhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNydDAf" +
                "BgNVHSMEGDAWgBTPmanqeyb0S8mOj9fwBSbv49KnnTAdBgNVHQ4EFgQUAFrwraXU" +
                "eDX1hBKoLAUO+FYbjo4wEQYJYIZIAYb4QgEBBAQDAgQQMBYGCisGAQQBgjcCARsE" +
                "CDAGAQEAAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQCIwphaN45b5ViKsRfCA6hbVeqM" +
                "hNCJmL64mqxdYvw3gsF4oOCDoiM6kWhy1NoUd2Lpcr9PjfGJ55ZX4sZlqoxeflTp" +
                "HgnX8MPJuKpDBsk7WkhiEu65fBwb+g9wQHMMfgY0gjHIAQ2ZEf2PJ6TTSFcvj24w" +
                "h73dsyqcmj290fpwDtHaF2jE5pq/tQBOJ8vDkN46wt4qMhG6nIpOgc9KngyLLdEO" +
                "7V34KM1Nb+sN9JkqTWXNoNqlOf7QjYHwlw7Ku7W3kIGK+f1kP1xqTBpXwsBE5sLl" +
                "nujP+JAqV8aKlBJDKRI3IhlrG9CJUmqFNhmZ2f5QE5jAvPQeXevLnMYiqJrgMIIC" +
                "xDCCAaygAwIBAgIEDu7u7jANBgkqhkiG9w0BAQUFADAkMSIwIAYDVQQDDBlHQy1V" +
                "V1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjEzMDkyNFoXDTE3MDUxMjEz" +
                "MDkyNFowJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjCCASIw" +
                "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMDirBlfDqNuboS0M9B1ael4uCag" +
                "GnDav8RQ3AYGJoKv3SxNqyRBrp9eppjYF3JINZ1OlyUsxIZ8VzvWMfmA9kajWbdi" +
                "aeGEGStFug1zQBOy6b41/yROHWE3/DQ2WK0flxpuCgGkriiXTiqEJYR4PNLBpvO2" +
                "AQ57H/SZ2bEKHFQazyVtw/HroxCSGzXu2EzDO9oiDRj0lhOI1At0oXGw/feu6ed+" +
                "aHnnJCm8FZE8vzwgV1NN6P7rCa5336o/8Gnsq7lyg3tmhi57SRPd+C122e7fFMXV" +
                "kM64gI32gF592UtXT5t6ohYIX8qkaVjLM0dzCdssh96HKWZrC2pQbPINhoMCAwEA" +
                "ATANBgkqhkiG9w0BAQUFAAOCAQEAqVAOT4YHy4DMWtkTA7D43h+oFWmyeIzKFvYO" +
                "DDNnpXjn+Gf/++B4ULMq9Dm3tg37BKoWN+cYIHNJauOa2EAvFDGBFoZz9qrNLyJh" +
                "rRhUdkaO779aXKCfXNIB9UGQFqIg68UHcWViT83RzWE6+OvaDf2KKq4T8k9Teo+R" +
                "cwR/XUGZE4JZWBGUMVerLgERhhXelXZ5uBRNwvq9acqmXJ9FCrWUHXLYr2btGmV6" +
                "cEB6QcKuU6uVB8xjdgT5b3NzOPbhG3v140AOjACyhGFs6M1XdTC43Zg+2TeZQxJl" +
                "QFiRyEiBnAkKJqIh8LfLha8heGsRJu9kdl7BYCA9eWSELzTxig==";
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

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