using FlexChartExplorer.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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 FlexChartExplorer
{
    /// <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 =
                "ACoBKgIqB4NGAGwAZQB4AEMAaABhAHIAdABFAHgAcABsAG8AcgBlAHIASqOJXxzG" +
                "UOU0uNyP8ANGg48J+k2u9RP1QLKLUVjNPz02XCevZeWvYxmR36zB1nAKRs1a+aRQ" +
                "0djEmVK50yy3WvqeaTzj4yzpOmIqNZWBpD3K4deXauGxlTBEviX9S6B36zSy2ld+" +
                "aav63SoYhLlJTIjjc8e2/B6AGsFaWFaLoj87FKpU8MKtg1xqYyfyKJPnoRuvPKK/" +
                "MzEJBt5jaUD9x+0qAnDAkdRIUhK4ood5dmNtgkFywY7gEOm1WdJT2UMdgiFYa5O5" +
                "e+I1PCsvpsKZvCDDmNzNzAafh0rj+4/txf6iQAVQWADY77y8Vj3DPWXxVv05bJB6" +
                "h6dTTyS7pomIsj2ffkZa/3NOkGi16OzuzDYHJ353PM/CGmXNNflB6wzrx7EYLpND" +
                "5xQOX9XY9h0caiChr+ONWhUGIgQIaGpqNnMSRRqYFJn+NR20D6XA/5+o7YT7HQ7P" +
                "qAGewbG5VrYOKFpJjetQT5noaEq0zN9G+lOSDgCR7TQJ7lokQjvCYZPSvJbJM3pg" +
                "lfIvUk2dbTqAo7tsgf+58gI+bpaiE+bEPg2kD4kzSQMQ67YD8t7I2ErdWbS4ddtM" +
                "F+I1dnvmPeNnCMJRo4xHCRMoVHqmGNTdmnaeeJslcSgjQ8PwZ5GjEpaEAw4eEqEF" +
                "k+vE9kV0nbiuD5D3wq1ZTMe12dt/qwCakGQwggVVMIIEPaADAgECAhBBA3jSJjZZ" +
                "ehbbJsa9EJSLMA0GCSqGSIb3DQEBBQUAMIG0MQswCQYDVQQGEwJVUzEXMBUGA1UE" +
                "ChMOVmVyaVNpZ24sIEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdv" +
                "cmsxOzA5BgNVBAsTMlRlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy52ZXJpc2ln" +
                "bi5jb20vcnBhIChjKTEwMS4wLAYDVQQDEyVWZXJpU2lnbiBDbGFzcyAzIENvZGUg" +
                "U2lnbmluZyAyMDEwIENBMB4XDTE0MTIxMTAwMDAwMFoXDTE1MTIyMjIzNTk1OVow" +
                "gYYxCzAJBgNVBAYTAkpQMQ8wDQYDVQQIEwZNaXlhZ2kxGDAWBgNVBAcTD1NlbmRh" +
                "aSBJenVtaS1rdTEXMBUGA1UEChQOR3JhcGVDaXR5IGluYy4xGjAYBgNVBAsUEVRv" +
                "b2xzIERldmVsb3BtZW50MRcwFQYDVQQDFA5HcmFwZUNpdHkgaW5jLjCCASIwDQYJ" +
                "KoZIhvcNAQEBBQADggEPADCCAQoCggEBAMEC9splW7KVpAqRB6VVB7XJm8+a60x7" +
                "8HZWNmvAjs/ermp7pbh8M9HYuEVTRj8H4baXxFYq4i75e5kNr7s+nGbJ5UfM6Cld" +
                "qbeRsQ4zvRb6npfrtcOBwfYOx0woeYQfI6VzLVzhPbGjUDK6qdHZLf/EcvSIKvRb" +
                "u3ILOE3kuux07gQkb++rrSBUyJKXU8nW8c+K+9sfWqHYPb8FF2IazWxhmfIdIoDN" +
                "Kyjr3r/wIn3jQejpqfamiHNsU/O26KS0EcvCS5CFOKDT6vvdnAkek3kyLWef+ca/" +
                "cbFo74vawYGtmw2wzw/hcwlANIQaCAxz+5JHQZ3SEg8LLSZ+C8E4T+8CAwEAAaOC" +
                "AY0wggGJMAkGA1UdEwQCMAAwDgYDVR0PAQH/BAQDAgeAMCsGA1UdHwQkMCIwIKAe" +
                "oByGGmh0dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3JsMGYGA1UdIARfMF0wWwYLYIZI" +
                "AYb4RQEHFwMwTDAjBggrBgEFBQcCARYXaHR0cHM6Ly9kLnN5bWNiLmNvbS9jcHMw" +
                "JQYIKwYBBQUHAgIwGQwXaHR0cHM6Ly9kLnN5bWNiLmNvbS9ycGEwEwYDVR0lBAww" +
                "CgYIKwYBBQUHAwMwVwYIKwYBBQUHAQEESzBJMB8GCCsGAQUFBzABhhNodHRwOi8v" +
                "c2Yuc3ltY2QuY29tMCYGCCsGAQUFBzAChhpodHRwOi8vc2Yuc3ltY2IuY29tL3Nm" +
                "LmNydDAfBgNVHSMEGDAWgBTPmanqeyb0S8mOj9fwBSbv49KnnTAdBgNVHQ4EFgQU" +
                "AFrwraXUeDX1hBKoLAUO+FYbjo4wEQYJYIZIAYb4QgEBBAQDAgQQMBYGCisGAQQB" +
                "gjcCARsECDAGAQEAAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQCIwphaN45b5ViKsRfC" +
                "A6hbVeqMhNCJmL64mqxdYvw3gsF4oOCDoiM6kWhy1NoUd2Lpcr9PjfGJ55ZX4sZl" +
                "qoxeflTpHgnX8MPJuKpDBsk7WkhiEu65fBwb+g9wQHMMfgY0gjHIAQ2ZEf2PJ6TT" +
                "SFcvj24wh73dsyqcmj290fpwDtHaF2jE5pq/tQBOJ8vDkN46wt4qMhG6nIpOgc9K" +
                "ngyLLdEO7V34KM1Nb+sN9JkqTWXNoNqlOf7QjYHwlw7Ku7W3kIGK+f1kP1xqTBpX" +
                "wsBE5sLlnujP+JAqV8aKlBJDKRI3IhlrG9CJUmqFNhmZ2f5QE5jAvPQeXevLnMYi" +
                "qJrgMIICxDCCAaygAwIBAgIEDu7u7jANBgkqhkiG9w0BAQUFADAkMSIwIAYDVQQD" +
                "DBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjEyMjQxOVoXDTE3" +
                "MDUxMjEyMjQxOVowJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3Bl" +
                "cjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ62VwET0olIM3/bCPaU" +
                "ncyTFoiP6qFNuDqFz0egpAWvSrf9iK2xQyQsbMwNN+fpUsjZ7bzLDkKj2QA/WKGd" +
                "H+xoUEfG2OA+BG5wsWc+tCmBvm+ljo1YVhjwNyWfw5fksN1zRrRQXlRao6P5M4R+" +
                "SLA9r9G+0ry+1fySMGJhW4EnyT9Cm53ZQ1u03+H2wuL+i7A454h6QRPvZY/5DEzZ" +
                "R6SEbLs+rbL1QosCrALwkZr/7Rsv6MB5IR/oFG4yG//eVxg3nFumFbVfT5ohblOv" +
                "lvz3Erd7eFAvicTLOGrggF1m0cLJeYt6ckhnAhZc0Vi6W35f4iRY8AaHGaJc7Wo1" +
                "YcUCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAGzbujV3qUiFNPpHYssl/jx1At6EH" +
                "hTOiegtUVzHsM2sJKoHTaem9YmeNR8hOp8414FeuydNrx9pz8rsafxWrOx5tgcye" +
                "YQw+cwEn1NaSE4AMJ3heptkEB8lrJ+GosCPM5hAeGWjRI6vqywTSDBNOU1mbl4Mg" +
                "8FowPx2CE4fCybGwsIQGtu8Jmar1v/IYQg1fnTcr4qnlERU1/Yz7iPcQIMwvIlI+" +
                "z4aFgONY7mFRY+22+AUz4YbtzswylSqKbeGVuEj6E/J5h5FCmHV0WVRayKebb8/V" +
                "hYxm35Nx87kMJUX68lwLbG/+qiu3+NFOWa0xuFgnYrKx94SCCOj5++B+VA==";
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">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();
        }
    }
}
