using PdfSamples.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 PdfSamples
{
    /// <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 =
                "ABwBHAIcB3VQAGQAZgBTAGEAbQBwAGwAZQBzAJxwpBNIwI49vEHsa7tsiMeo731P" +
                "d1krGeJoL3bHOzb3CJQrbXmfGaQKRzzyATRSg9XFWoVCbTQUBj3CwTa6rkpY4+Cq" +
                "NjTgkAWcQBewYO/Q/a4QziK9wgtZn0inn+1HP7YTr5NiDxDS9Eb0fLmy0n5z4mYP" +
                "g9oqGSmnLLt6ByzYEiosOX1NrdZLxqAgaLFCqvelInrVvjWPWlFZ4WkgrfA9XjEj" +
                "4ebM+YWChN99g1dQW7bUBIP86TOElDDmUGORe7a/CZvkBC7z/3fLOSyu7Ah+BBPg" +
                "LU32CDJWKTemiNgKdH31n1XayP0yN3WZcF9MM4YjPr3pvCfVK0AJ78YpDrAcLw2Z" +
                "tuv71UQTjzag6QcaKPWPo1s106uU0M5iXsW1Rt6YR9airbtn6lHV52z/zjtPbA36" +
                "0Y9n4GG8nCfNrJ1Ksd79UF12gI+QmMCWSyghqROHEioBYuZZC1oGilGx8Gb4vW0A" +
                "zWDdNjALZD7kaJL5x2GOJw9b2hEhsHjczzRtGkqyEHO8+pRNixo890XalMbJnSC+" +
                "tvJR+M6guVFXLlLllIcpBTuPnoYg4zZjBDkz0/VxP8+2LMgu7lzP5NIKTALcbcYx" +
                "BFJxeJGB1b912Akbt54gNBzrJNrnWAH5x40ynzdCqNWDnib9Ch0t35ZKCTB6+j7C" +
                "2kSSBsj0EtNaFaq6MIIFVTCCBD2gAwIBAgIQQQN40iY2WXoW2ybGvRCUizANBgkq" +
                "hkiG9w0BAQUFADCBtDELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJ" +
                "bmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTswOQYDVQQLEzJU" +
                "ZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JwYSAoYykx" +
                "MDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNpZ25pbmcgMjAxMCBD" +
                "QTAeFw0xNDEyMTEwMDAwMDBaFw0xNTEyMjIyMzU5NTlaMIGGMQswCQYDVQQGEwJK" +
                "UDEPMA0GA1UECBMGTWl5YWdpMRgwFgYDVQQHEw9TZW5kYWkgSXp1bWkta3UxFzAV" +
                "BgNVBAoUDkdyYXBlQ2l0eSBpbmMuMRowGAYDVQQLFBFUb29scyBEZXZlbG9wbWVu" +
                "dDEXMBUGA1UEAxQOR3JhcGVDaXR5IGluYy4wggEiMA0GCSqGSIb3DQEBAQUAA4IB" +
                "DwAwggEKAoIBAQDBAvbKZVuylaQKkQelVQe1yZvPmutMe/B2VjZrwI7P3q5qe6W4" +
                "fDPR2LhFU0Y/B+G2l8RWKuIu+XuZDa+7PpxmyeVHzOgpXam3kbEOM70W+p6X67XD" +
                "gcH2DsdMKHmEHyOlcy1c4T2xo1AyuqnR2S3/xHL0iCr0W7tyCzhN5LrsdO4EJG/v" +
                "q60gVMiSl1PJ1vHPivvbH1qh2D2/BRdiGs1sYZnyHSKAzSso696/8CJ940Ho6an2" +
                "pohzbFPztuiktBHLwkuQhTig0+r73ZwJHpN5Mi1nn/nGv3GxaO+L2sGBrZsNsM8P" +
                "4XMJQDSEGggMc/uSR0Gd0hIPCy0mfgvBOE/vAgMBAAGjggGNMIIBiTAJBgNVHRME" +
                "AjAAMA4GA1UdDwEB/wQEAwIHgDArBgNVHR8EJDAiMCCgHqAchhpodHRwOi8vc2Yu" +
                "c3ltY2IuY29tL3NmLmNybDBmBgNVHSAEXzBdMFsGC2CGSAGG+EUBBxcDMEwwIwYI" +
                "KwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUGCCsGAQUFBwICMBkM" +
                "F2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMBMGA1UdJQQMMAoGCCsGAQUFBwMDMFcG" +
                "CCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3NmLnN5bWNkLmNvbTAm" +
                "BggrBgEFBQcwAoYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcnQwHwYDVR0jBBgw" +
                "FoAUz5mp6nsm9EvJjo/X8AUm7+PSp50wHQYDVR0OBBYEFABa8K2l1Hg19YQSqCwF" +
                "DvhWG46OMBEGCWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3AgEbBAgwBgEBAAEB" +
                "/zANBgkqhkiG9w0BAQUFAAOCAQEAiMKYWjeOW+VYirEXwgOoW1XqjITQiZi+uJqs" +
                "XWL8N4LBeKDgg6IjOpFoctTaFHdi6XK/T43xieeWV+LGZaqMXn5U6R4J1/DDybiq" +
                "QwbJO1pIYhLuuXwcG/oPcEBzDH4GNIIxyAENmRH9jyek00hXL49uMIe93bMqnJo9" +
                "vdH6cA7R2hdoxOaav7UATifLw5DeOsLeKjIRupyKToHPSp4Miy3RDu1d+CjNTW/r" +
                "DfSZKk1lzaDapTn+0I2B8JcOyru1t5CBivn9ZD9cakwaV8LARObC5Z7oz/iQKlfG" +
                "ipQSQykSNyIZaxvQiVJqhTYZmdn+UBOYwLz0Hl3ry5zGIqia4DCCAsQwggGsoAMC" +
                "AQICBA7u7u4wDQYJKoZIhvcNAQEFBQAwJDEiMCAGA1UEAwwZR0MtVVdQIEludGVy" +
                "bmFsIERldmVsb3BlcjAeFw0xNjA1MTIxNDUyMTJaFw0xNzA1MTIxNDUyMTJaMCQx" +
                "IjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwggEiMA0GCSqGSIb3" +
                "DQEBAQUAA4IBDwAwggEKAoIBAQCp3NxfsbVsm4IwIrvSJA+7uAFmmJ4H8VwAd2ZS" +
                "9VmHm47VR30lybuL4seG+xU5tWz800GKRkW/P005jOxCr9pSnyzBkcL/CAZgXzHU" +
                "rTyyEvRHxD0TTWsC4TjH9q373OYpENovtV1od87rKbujdhtmNyO1aZC4geiD2/Gp" +
                "XK3jDfhnX/2SFh+iZalRbSGxSKcHD05Qa7cJ9HpdZDQCJ+iHinpP7STYtJG4rvdr" +
                "nOjJR/WtCmnm0gCbMms3tai0PZ8oe9q7dlhL01744tSXeFLiqCnJLZWZ1T4thQA7" +
                "LLdyxAdLp1gaBKtJyNjDO+xnGQAvsbhD+N/5MCKbAFgCe0+zAgMBAAEwDQYJKoZI" +
                "hvcNAQEFBQADggEBACN+SIDWWH3+nKe+OoAFwkDqfm5WhYkFGzu0qvYvMNNsHXRP" +
                "a0qLXeCliucDVUqFbLnom7BcN8+s4/oRs9+05THCO6Xy8cXS1x/SAaxjdqAeKnNu" +
                "XXXpkYdC1ugY7dYfPYJ4zWgbKvDr69SGq294AWTYd/aFcm132nBiFBk3JnlGP/fy" +
                "4b8W424lQwBfd/Zt8zqXmbPUvMdD19g3/lLV210QmHYzC1h3Adoa4rtio3iPWo3S" +
                "yQxInTx44yo5l/hdtsJxtiDW65FGK4NvlGviMzgQejVJBnvgBXJX5b9KU1wIrwm8" +
                "5KlQ5NW1djSjsz5x7MBkGohBiFa1DIaWWhS8ZHQ=";
            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();
        }
    }
}
