using ExtendedSamples.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 ExtendedSamples
{
    /// <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 =
                "ACYBJgImB39FAHgAdABlAG4AZABlAGQAUwBhAG0AcABsAGUAcwBisSZZRuufTMMz" +
                "ewQkhP3nA225bpKB/ocCLqFSU8zLjivkZa4PpkMbJ6Cn4OGkHLaHHHu8vEbdFUD9" +
                "UMmjyIumkIuXS2+POJcJw5YkmK1ge6DlCfCv1/j6fhWBI4AnCWAxbpD4UgEr3iQJ" +
                "yIxlcwM3oKhGE700Zd4fNPlfVQfb3LShUQmTlCy2s440TiUCF1iFU9VGYHg1RIPN" +
                "6ZVGF9kK7quUIEMHmS2KhYp9eAmxs+iQzXBNmgMgG3uSdOL30F1ElSTRPH+IuN07" +
                "QGHk7FOLV/dzWhB4bSLaRFlFJpp9lDLpwjgaitTK12BSAxAsP4pG8AhvazIyTxVf" +
                "ru1tNZLPhQKTOErSBELC9za2E7ll9lldO0jTWiDMyti3Bot4EmjTQ5xL41S1Q0HY" +
                "6jiUW1OiY/92V/c6GhZ/5v0rrGLDJmt6tYJfsxT9d4zTttRu/zSFjtloMSfcE7et" +
                "yoTQ9xsF/ToEb/0VD/LwQTlqJU+L4rUeC4KUBvfqHn9knGi8JiD8R4eTl8jF3dVT" +
                "LQBbX7/0rEhZgRTkCocAqLVBScHI9vXa3mONOfdypXKSFgrRfIzmmnGR0CNTyKcD" +
                "eiHV81JcEpK4nYVKnDsL1S0+HI8ff9KhALGBvGtsOgOF3DFpC7nvIELUZYT10620" +
                "PwG3vGlUf9suo7da3Ws0qmdRPrvenzCCBVUwggQ9oAMCAQICEEEDeNImNll6Ftsm" +
                "xr0QlIswDQYJKoZIhvcNAQEFBQAwgbQxCzAJBgNVBAYTAlVTMRcwFQYDVQQKEw5W" +
                "ZXJpU2lnbiwgSW5jLjEfMB0GA1UECxMWVmVyaVNpZ24gVHJ1c3QgTmV0d29yazE7" +
                "MDkGA1UECxMyVGVybXMgb2YgdXNlIGF0IGh0dHBzOi8vd3d3LnZlcmlzaWduLmNv" +
                "bS9ycGEgKGMpMTAxLjAsBgNVBAMTJVZlcmlTaWduIENsYXNzIDMgQ29kZSBTaWdu" +
                "aW5nIDIwMTAgQ0EwHhcNMTQxMjExMDAwMDAwWhcNMTUxMjIyMjM1OTU5WjCBhjEL" +
                "MAkGA1UEBhMCSlAxDzANBgNVBAgTBk1peWFnaTEYMBYGA1UEBxMPU2VuZGFpIEl6" +
                "dW1pLWt1MRcwFQYDVQQKFA5HcmFwZUNpdHkgaW5jLjEaMBgGA1UECxQRVG9vbHMg" +
                "RGV2ZWxvcG1lbnQxFzAVBgNVBAMUDkdyYXBlQ2l0eSBpbmMuMIIBIjANBgkqhkiG" +
                "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwQL2ymVbspWkCpEHpVUHtcmbz5rrTHvwdlY2" +
                "a8COz96uanuluHwz0di4RVNGPwfhtpfEViriLvl7mQ2vuz6cZsnlR8zoKV2pt5Gx" +
                "DjO9Fvqel+u1w4HB9g7HTCh5hB8jpXMtXOE9saNQMrqp0dkt/8Ry9Igq9Fu7cgs4" +
                "TeS67HTuBCRv76utIFTIkpdTydbxz4r72x9aodg9vwUXYhrNbGGZ8h0igM0rKOve" +
                "v/AifeNB6Omp9qaIc2xT87bopLQRy8JLkIU4oNPq+92cCR6TeTItZ5/5xr9xsWjv" +
                "i9rBga2bDbDPD+FzCUA0hBoIDHP7kkdBndISDwstJn4LwThP7wIDAQABo4IBjTCC" +
                "AYkwCQYDVR0TBAIwADAOBgNVHQ8BAf8EBAMCB4AwKwYDVR0fBCQwIjAgoB6gHIYa" +
                "aHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcmwwZgYDVR0gBF8wXTBbBgtghkgBhvhF" +
                "AQcXAzBMMCMGCCsGAQUFBwIBFhdodHRwczovL2Quc3ltY2IuY29tL2NwczAlBggr" +
                "BgEFBQcCAjAZDBdodHRwczovL2Quc3ltY2IuY29tL3JwYTATBgNVHSUEDDAKBggr" +
                "BgEFBQcDAzBXBggrBgEFBQcBAQRLMEkwHwYIKwYBBQUHMAGGE2h0dHA6Ly9zZi5z" +
                "eW1jZC5jb20wJgYIKwYBBQUHMAKGGmh0dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3J0" +
                "MB8GA1UdIwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMB0GA1UdDgQWBBQAWvCt" +
                "pdR4NfWEEqgsBQ74VhuOjjARBglghkgBhvhCAQEEBAMCBBAwFgYKKwYBBAGCNwIB" +
                "GwQIMAYBAQABAf8wDQYJKoZIhvcNAQEFBQADggEBAIjCmFo3jlvlWIqxF8IDqFtV" +
                "6oyE0ImYvriarF1i/DeCwXig4IOiIzqRaHLU2hR3Yulyv0+N8YnnllfixmWqjF5+" +
                "VOkeCdfww8m4qkMGyTtaSGIS7rl8HBv6D3BAcwx+BjSCMcgBDZkR/Y8npNNIVy+P" +
                "bjCHvd2zKpyaPb3R+nAO0doXaMTmmr+1AE4ny8OQ3jrC3ioyEbqcik6Bz0qeDIst" +
                "0Q7tXfgozU1v6w30mSpNZc2g2qU5/tCNgfCXDsq7tbeQgYr5/WQ/XGpMGlfCwETm" +
                "wuWe6M/4kCpXxoqUEkMpEjciGWsb0IlSaoU2GZnZ/lATmMC89B5d68ucxiKomuAw" +
                "ggLEMIIBrKADAgECAgQO7u7uMA0GCSqGSIb3DQEBBQUAMCQxIjAgBgNVBAMMGUdD" +
                "LVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwHhcNMTYwNTEyMTQxNDI3WhcNMTcwNTEy" +
                "MTQxNDI3WjAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMIIB" +
                "IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApgbEe7fMhX4voZpsYd/e3nmx" +
                "cjD9QvF6Yi/lZ+wQZ37pq57ScJOrNxea4vu3JM8FIzE/XuFAC7hmyHqrLwLiKrRH" +
                "8pbZBPlwRnq4sIh4QTsI/re9G2CZFzdLmDX1G92+gISHzOYZXX2l+4QgxFrbbISX" +
                "xs1wtYPgZarWywCs4/mgHRiRnU9uHQrVbrTWJCyjCGk3thV94hWF3pj8Pp0M7/M7" +
                "k6JzbWFiWF9jrakc/6kIRb229fBGmlkzO9ksavkAHrUH7RdjvxShyvGko6M0MNta" +
                "NQppNXyoTUAVZxcg+Jk5zZesuR2bJD4lsON5zIswIFL5SMMuCx/KmGXzlZh3UQID" +
                "AQABMA0GCSqGSIb3DQEBBQUAA4IBAQCUUsIorPIJd9mqZboPAak5IixUQIOm7LOy" +
                "vBtyia0w2+ilFyHXqICvE6G+0XxzxUdN7r9Ag6Mz9KsPABe6otMDiXlMwGlOOfK7" +
                "8Yox7+f0rocMet5cX/NfOcB1y4h25K24igRC6BL4k6KuT+l0VKv3tTSmClS/A6tD" +
                "6n9USbBQ3PkIZ90ZhBQBh7z54/qI31u/yyPzAkcL7B+KNkC/ShdtUlSh3jOUDAsX" +
                "LXD6HPpx086CUKIRRbcPhrLLellTWTaDxjkCqgqqxZM/m99OORpr7CDnzifyKul7" +
                "QUHg0lsimNyUwwHuwpXsk2rB280zqs5mdXBd4jnjbun7XM4J+yTz";
        }

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