using ImagingSamples.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 ImagingSamples
{
    /// <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 =
                "ACQBJAIkB31JAG0AYQBnAGkAbgBnAFMAYQBtAHAAbABlAHMAH/GZYjPTj4EnS3r6" +
                "wNMbwHwp4kwX3So1v9b+tFjwp+fhRVMlqcI7dpUM1BFFXDSuA2BzMeBqPmAGjYN8" +
                "ffSD+tTo8IbF/0CFaPeQt6tkspA+jn+QnlDrzClXyUK0RhuhISbINPTXQVScn4Vi" +
                "o159oY/EUMDBqIxkb660+eFRvXhRbCT28DGidhmsWD0HxmWFELSCo6YcVKbgOJec" +
                "T9PkXfG2XOL4g6kKow1B9hoiLrp3mfKaoMkpRUduIZusxeNY5u66eFh2kbgeOObf" +
                "cf1vdxndViU/BnmKowPex3F8yImfLJOEDkcyV46g4mIUm3VEXbW+9403109HuR3Q" +
                "zxITQ5Cz60VERzEzkJEGP7ZS/hvqUi95ILeP4Nck9q96LQoyOhnRvmMZKp6KrrpF" +
                "yOM2FTXa3hPGwK2Y9+2z7WUUSsWENRw90AcVoAZdXb02WdAx3deE5wuJsk0K8+p3" +
                "D8HrwI7XFaNHnynAm5y9I31QjhiBNiUMn5Sqs1nX+d0MUFLq3OChWPjKpmbResPc" +
                "D1eIFn/7CS8+FsPHoxZveO4H7dwaBwhEHUVKtjyIqz4yeFCZVbO7O2tvWbkXZEv5" +
                "f9Hvgepqg0hnjYIORzHQ2lukIWgJYIw9choKHp0pp54Yu/DrDI5ALYxm8wpVvvDy" +
                "I++J8hZ8+n3yHZ20rPjVZq78JbIwggVVMIIEPaADAgECAhBBA3jSJjZZehbbJsa9" +
                "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" +
                "V1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjE0MDE1NVoXDTE3MDUxMjE0" +
                "MDE1NVowJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjCCASIw" +
                "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALLG6jWnVIUUBI9J8Qs/S1o+YSsl" +
                "gTh74EUZ1w4ow2+eE034ojiIvxhO7hcIyQWZcaUIPPtqlANQMqgw760DplVKte1Z" +
                "VRisJVbxRvWfQ4Za7I5J2NbJkSpplOiezXIVzzdnFf+bXNaxZYPsWY5mhnlg75ix" +
                "J8TzDLyWphwuCbhOu6FyWuTz+NqXBLFpFaDPEZl2SVPZk29j7VwFYqRN8wP+dHan" +
                "0Oc07lqW5UdrMqebWtyQ4Lj40T7n+NueVUrknzpWo6dMVLpfqngUViZqtYlE16R4" +
                "3hBOacRzKqgKAKAgDtIah9HYTGDS6vReNsPM0kETA3YEAQVMTIckoVkyM08CAwEA" +
                "ATANBgkqhkiG9w0BAQUFAAOCAQEAEBI0WVlac0kgkYe/v2pNOlwI1n2vrMnuO+F/" +
                "+mcGeH/p0XBoBlbke3xvWB9d1lJZtxf87RJpH0JdYTAnAx3/Jnjwrg5FJW+ecgG3" +
                "/K3giYyuEt5krGfyc+/hJxTm0xu56u4c77alnzPJxm1FkEJFwq6XYbBcw0bsWo96" +
                "c/T83TPqlMmkKREKgVBfzdwdZkzw0betLjjkYGS4nLSPVZoGCCCzPBNUFeKyE40w" +
                "cimiT0yfo+udqeTqYNvHPy0pmjmVtPebLI6xeHSmbAQjOvwk/xEXUHlUjIZ2ksrw" +
                "ok7dDCNvSQv/726WsENZbNbKS5VlWZiDbv5DB5484f0WNX3d5A==";
            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();
        }
    }
}
