using RichTextBoxSamples.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 RichTextBoxSamples
{
    /// <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 =
                "ACwBLAIsB4VSAGkAYwBoAFQAZQB4AHQAQgBvAHgAUwBhAG0AcABsAGUAcwBhBeLb" +
                "ruXvexBC6Wpf+pKN541cCIFJrhh4zcEEDE3ubsdN/252H/9yQISE4XQ+8r7IYC18" +
                "/rhxHIifBzfmLRpzQuYJkAF5gPWzsN2Lqbinbs93y/CRxqG1WS8Jz3uIaf2RpBan" +
                "HTl8SZnYHPjmfzFIl3crs23ewHOJTAog+WWXsSvfcML3D/nUW1A7s2YWpjzI5kR8" +
                "BJ7+mhhD3ruZBm5c69BERnw4ja9jtuKR85/nFU9DFvy8pNGUraeIIZQxyJ7BVIRv" +
                "hsCmH6mDKtfWANgYkKEekXUdjeNz0poNQH7fB+bDpEjMosheMFzuHBLCTi9omH1p" +
                "zVOCO7+WJcnLdCsGVEXcom1SoKFeedqOGU4EhOG+ysN6OEh/koUiUQR5rM1yFh40" +
                "nRsFGMxcTbHBVDqFqplAzQsiEpLVyNJlnaxuNhpy3TAP3OtlXSETdvVsWrekKlaj" +
                "zDymgRawtJEhJLGUdV9BZnU1n4h03ENg7C1k47EFV2AIfpXvP61e5WBvJv+DUQfA" +
                "WJpEnB8VO0+YwCtZ2pOgVu41ymRIaqJJ76SEJ1qUKV8QstcQHNNWtu/ArPCfeKfg" +
                "M8lJvN/1Jjr92YNgYKbKa0U+mGeBLTijdxTsfAXcVUd3eOQJq+roswAB+G+4eNMi" +
                "FPZlwj5X03R0bggQIKCp1KImnkjZT3Gjp2zzQjCCBVUwggQ9oAMCAQICEEEDeNIm" +
                "Nll6Ftsmxr0QlIswDQYJKoZIhvcNAQEFBQAwgbQxCzAJBgNVBAYTAlVTMRcwFQYD" +
                "VQQKEw5WZXJpU2lnbiwgSW5jLjEfMB0GA1UECxMWVmVyaVNpZ24gVHJ1c3QgTmV0" +
                "d29yazE7MDkGA1UECxMyVGVybXMgb2YgdXNlIGF0IGh0dHBzOi8vd3d3LnZlcmlz" +
                "aWduLmNvbS9ycGEgKGMpMTAxLjAsBgNVBAMTJVZlcmlTaWduIENsYXNzIDMgQ29k" +
                "ZSBTaWduaW5nIDIwMTAgQ0EwHhcNMTQxMjExMDAwMDAwWhcNMTUxMjIyMjM1OTU5" +
                "WjCBhjELMAkGA1UEBhMCSlAxDzANBgNVBAgTBk1peWFnaTEYMBYGA1UEBxMPU2Vu" +
                "ZGFpIEl6dW1pLWt1MRcwFQYDVQQKFA5HcmFwZUNpdHkgaW5jLjEaMBgGA1UECxQR" +
                "VG9vbHMgRGV2ZWxvcG1lbnQxFzAVBgNVBAMUDkdyYXBlQ2l0eSBpbmMuMIIBIjAN" +
                "BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwQL2ymVbspWkCpEHpVUHtcmbz5rr" +
                "THvwdlY2a8COz96uanuluHwz0di4RVNGPwfhtpfEViriLvl7mQ2vuz6cZsnlR8zo" +
                "KV2pt5GxDjO9Fvqel+u1w4HB9g7HTCh5hB8jpXMtXOE9saNQMrqp0dkt/8Ry9Igq" +
                "9Fu7cgs4TeS67HTuBCRv76utIFTIkpdTydbxz4r72x9aodg9vwUXYhrNbGGZ8h0i" +
                "gM0rKOvev/AifeNB6Omp9qaIc2xT87bopLQRy8JLkIU4oNPq+92cCR6TeTItZ5/5" +
                "xr9xsWjvi9rBga2bDbDPD+FzCUA0hBoIDHP7kkdBndISDwstJn4LwThP7wIDAQAB" +
                "o4IBjTCCAYkwCQYDVR0TBAIwADAOBgNVHQ8BAf8EBAMCB4AwKwYDVR0fBCQwIjAg" +
                "oB6gHIYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcmwwZgYDVR0gBF8wXTBbBgtg" +
                "hkgBhvhFAQcXAzBMMCMGCCsGAQUFBwIBFhdodHRwczovL2Quc3ltY2IuY29tL2Nw" +
                "czAlBggrBgEFBQcCAjAZDBdodHRwczovL2Quc3ltY2IuY29tL3JwYTATBgNVHSUE" +
                "DDAKBggrBgEFBQcDAzBXBggrBgEFBQcBAQRLMEkwHwYIKwYBBQUHMAGGE2h0dHA6" +
                "Ly9zZi5zeW1jZC5jb20wJgYIKwYBBQUHMAKGGmh0dHA6Ly9zZi5zeW1jYi5jb20v" +
                "c2YuY3J0MB8GA1UdIwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMB0GA1UdDgQW" +
                "BBQAWvCtpdR4NfWEEqgsBQ74VhuOjjARBglghkgBhvhCAQEEBAMCBBAwFgYKKwYB" +
                "BAGCNwIBGwQIMAYBAQABAf8wDQYJKoZIhvcNAQEFBQADggEBAIjCmFo3jlvlWIqx" +
                "F8IDqFtV6oyE0ImYvriarF1i/DeCwXig4IOiIzqRaHLU2hR3Yulyv0+N8Ynnllfi" +
                "xmWqjF5+VOkeCdfww8m4qkMGyTtaSGIS7rl8HBv6D3BAcwx+BjSCMcgBDZkR/Y8n" +
                "pNNIVy+PbjCHvd2zKpyaPb3R+nAO0doXaMTmmr+1AE4ny8OQ3jrC3ioyEbqcik6B" +
                "z0qeDIst0Q7tXfgozU1v6w30mSpNZc2g2qU5/tCNgfCXDsq7tbeQgYr5/WQ/XGpM" +
                "GlfCwETmwuWe6M/4kCpXxoqUEkMpEjciGWsb0IlSaoU2GZnZ/lATmMC89B5d68uc" +
                "xiKomuAwggLEMIIBrKADAgECAgQO7u7uMA0GCSqGSIb3DQEBBQUAMCQxIjAgBgNV" +
                "BAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwHhcNMTYwNTEyMTQwMTU1WhcN" +
                "MTcwNTEyMTQwMTU1WjAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxv" +
                "cGVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1K5cIWrV1M0ZGnYo" +
                "1tXq42C2hI+Qq0kaf1JumMyu2L0D6WMrN6wqrzj6cNJ7r2zLCXgYfEzxRAqGvsbn" +
                "1jbKuT5teEvAwY9aoYk/YLTWTv31AfvdAh+FS/q/om4unu5TvICia6WXsqjAkUAx" +
                "d7Dn7mjMOmRsomD9hGa416LjBs33pTJjW1KWwaxpg/2fsWn7FIESKFupxajnXgIR" +
                "+/pqBupwJwdw+sQ2o66+CQe5eLIlSl57Z3GKaK7Wd8rO9fOo6IAyqI0KuBlstehE" +
                "0fAkeC+L4K0LmbQJAgSpsnEHkkhEAp/wWc7xJPvAuLJBj7zuGF6AVIWDdJpz5xjn" +
                "iAIhHwIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQCote4IczNko5DXQr6HECXXhrgA" +
                "HKo4c6uv8z6AxsP50RaQWXpx/zaZ4KTgEqzSF2iecBuKfd6tPIeojUI9zV9U1c9u" +
                "nEhLQdocmG7hXE+cSRJTstoRvFlPgd5K1OV/JU57gKJLPyz4l80LEyr8GKdwVprQ" +
                "8UA9Ttyd9CTN12ZfTBIbBwe9m1nnxqUeKWGoOFGudj4pXVl5tpx7QNP5WxocZDO5" +
                "ar58DZBn49qjeIy3nWRq5DPaFxjNryMxsA35haFPuaMyiFlclEW9L+pJnY4q4ad+" +
                "z2/MxIrLXrla6vjA1ypCf2UIy5CfsdoJNSSTVcVOqBco7UBcR+x7ICrvPKgk";
            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(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();
        }
    }
}
