using PdfViewerSamples.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 PdfViewerSamples
{
    /// <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 =
                "ACgBKAIoB4FQAGQAZgBWAGkAZQB3AGUAcgBTAGEAbQBwAGwAZQBzAEJdqAW7+Rcx" +
                "v1zWr+FU3ZzhDynij612/EeF4x8Ibdgo5RN/ILvzunJ3OnTOwXxv6CRU/YJZDbwd" +
                "EQmg2EBPdbOfDKWG8YdqpC0h4KQntYfeainClgQPlJTO8rVMajCYf9SyRz4aJnTt" +
                "9bFOCTPjNr+QTODlZO03mKRD5ji/RrDGaGuLE5sLMmQhbOEy8yy0AGmAxD4/mm3Q" +
                "8/4oVAyeia6ltenxtRlDqCPOz4nb+Jy96VuTzkJR9DuCcW/MzRFNx0rBTk6QUXJD" +
                "YkqvGa91xdj/0TqF/qgedq5mQ3d3kv2xShWt0FqI/JR1OiIwOmAAdbXFbJpM4rFU" +
                "PC+ig+i8Ryu220faQcbxxy/vtjIZfmyh8ARmgX2CRgk37FhZfqd0TF6u3Ruu85Aj" +
                "vALyv/mwufBrLeqqovnSbX0ZJ5fSDmzFmK5gQHgYmFygl1clRzXBYA76a74VtQSt" +
                "NZRzX14mpSEKq2WyGYYrndbU5KdPgjhzc6iygp3EFvGHafRRyrDjcXYQ4rG8W2S7" +
                "HSffdiHFFAY4AFmMXwOYi3m6K4V3QNMcQ2/Y2kjQVNuztDDlrabMIuXKclTYSG9g" +
                "SUoitxCczTcfOJ+5tx6fJXGTa1qbs5TqrBUEKglfTgo0m4pgft2uTmaVFajcGpYa" +
                "HQW0kLs/pmyruVy2TZEAiQKmLJ34xaDfMIIFVTCCBD2gAwIBAgIQQQN40iY2WXoW" +
                "2ybGvRCUizANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCVVMxFzAVBgNVBAoT" +
                "DlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3Jr" +
                "MTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24u" +
                "Y29tL3JwYSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNp" +
                "Z25pbmcgMjAxMCBDQTAeFw0xNDEyMTEwMDAwMDBaFw0xNTEyMjIyMzU5NTlaMIGG" +
                "MQswCQYDVQQGEwJKUDEPMA0GA1UECBMGTWl5YWdpMRgwFgYDVQQHEw9TZW5kYWkg" +
                "SXp1bWkta3UxFzAVBgNVBAoUDkdyYXBlQ2l0eSBpbmMuMRowGAYDVQQLFBFUb29s" +
                "cyBEZXZlbG9wbWVudDEXMBUGA1UEAxQOR3JhcGVDaXR5IGluYy4wggEiMA0GCSqG" +
                "SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBAvbKZVuylaQKkQelVQe1yZvPmutMe/B2" +
                "VjZrwI7P3q5qe6W4fDPR2LhFU0Y/B+G2l8RWKuIu+XuZDa+7PpxmyeVHzOgpXam3" +
                "kbEOM70W+p6X67XDgcH2DsdMKHmEHyOlcy1c4T2xo1AyuqnR2S3/xHL0iCr0W7ty" +
                "CzhN5LrsdO4EJG/vq60gVMiSl1PJ1vHPivvbH1qh2D2/BRdiGs1sYZnyHSKAzSso" +
                "696/8CJ940Ho6an2pohzbFPztuiktBHLwkuQhTig0+r73ZwJHpN5Mi1nn/nGv3Gx" +
                "aO+L2sGBrZsNsM8P4XMJQDSEGggMc/uSR0Gd0hIPCy0mfgvBOE/vAgMBAAGjggGN" +
                "MIIBiTAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDArBgNVHR8EJDAiMCCgHqAc" +
                "hhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNybDBmBgNVHSAEXzBdMFsGC2CGSAGG" +
                "+EUBBxcDMEwwIwYIKwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUG" +
                "CCsGAQUFBwICMBkMF2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMBMGA1UdJQQMMAoG" +
                "CCsGAQUFBwMDMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3Nm" +
                "LnN5bWNkLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5j" +
                "cnQwHwYDVR0jBBgwFoAUz5mp6nsm9EvJjo/X8AUm7+PSp50wHQYDVR0OBBYEFABa" +
                "8K2l1Hg19YQSqCwFDvhWG46OMBEGCWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3" +
                "AgEbBAgwBgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEAiMKYWjeOW+VYirEXwgOo" +
                "W1XqjITQiZi+uJqsXWL8N4LBeKDgg6IjOpFoctTaFHdi6XK/T43xieeWV+LGZaqM" +
                "Xn5U6R4J1/DDybiqQwbJO1pIYhLuuXwcG/oPcEBzDH4GNIIxyAENmRH9jyek00hX" +
                "L49uMIe93bMqnJo9vdH6cA7R2hdoxOaav7UATifLw5DeOsLeKjIRupyKToHPSp4M" +
                "iy3RDu1d+CjNTW/rDfSZKk1lzaDapTn+0I2B8JcOyru1t5CBivn9ZD9cakwaV8LA" +
                "RObC5Z7oz/iQKlfGipQSQykSNyIZaxvQiVJqhTYZmdn+UBOYwLz0Hl3ry5zGIqia" +
                "4DCCAsQwggGsoAMCAQICBA7u7u4wDQYJKoZIhvcNAQEFBQAwJDEiMCAGA1UEAwwZ" +
                "R0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNjA1MTIxNDAxNTVaFw0xNzA1" +
                "MTIxNDAxNTVaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIw" +
                "ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCVr482PaD9dDQdF90PX7Wg" +
                "NWCM0zT8BLnX0gWpQmR9PmIJwwRn4VgojTK/B/di+y2XhlH6OC98N0mpjSx8ep9y" +
                "Rs+VEGZYallPm/DuuhofFPAcGDBcjuHgTyNKBDAyCqwm3gTbjYBirRVNf7Lf59on" +
                "orZTNcn0zoeu2LNron/gVoQ5gTGiRauC5Twz7L6NiTg1HuO2KClB3CJBbCYjvjxs" +
                "bbi4QN80SpkiuKdHk39n3m3gHCRZBHrUcRW6a75x2DDynoLdtBhi7UtwqUzFh+2I" +
                "ume1BZZRH3WLYHcULeD4YKqnF9VNiFgT87ODRw8mXeUaxeBKYflMsJFmmoGEyZaR" +
                "AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAHlIDcg3vFAV6JoRYhQwjAYrVmaoClNP" +
                "apbqOiCmM6M74oPzvWUEcQEjLZvkpKCc0vbX9fvdHIljxVtG5EnIVVOO77Vxpz6/" +
                "Vl4bG0GW4m9USPPg2VQ4ZW1JgHZsssT+fsg1EXt3nrJviDL9irBtojy6mqZENN5b" +
                "bMa019iCaGl3mOljsf674w6iyqtF5rPeR4H8ZY1sTltN08XCZC8KSj4Ph72h9dmU" +
                "4+jJU6QSF78HSiq3yzRU5P6lD4qJAU6KbzLwpNmsFuuYNr0IQy2qerwKuPIerZ8E" +
                "Oy1auAu6rhqShszBIPet8wLGwrFaOetqqFZWBLauHSEbs9elNHLjnrM=";
            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();
        }
    }
}
