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 FlexReportSamples
{
    /// <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=
                "ACoBKgIqB4NGAGwAZQB4AFIAZQBwAG8AcgB0AFMAYQBtAHAAbABlAHMAbiNyZfgA" +
                "g2KmALOzf5NuyPgwIpocyqJQzVvIev1V5GTTGHI80KBAArq6QKKKkW945pfX8lKO" +
                "QuqTT0zBQqnOtHRKFty3IQTPzMkoh9mQsgNQ+sHFM3/cjZficQ1mCl1gb0QYzIVW" +
                "Y+WBxS+M1esPa4YRWSdV0AAwbM05Pze+lWTHrXo+K3By8bhNg60hF07yGxcGSjzU" +
                "Lcd+fvPoFSXjnDe5ubV8C/UgYjJbEioc/h9QbNXQ83kGjPG1OT6ILq0wCLVAxuM6" +
                "ccZNayjCILxNw3s7aRhpv5Impv3SjDsybk/bZWhg9qo2K9tq7hoDAta62xCVTWXg" +
                "q3r1B9EQpkn9dZi4rAT/znX/1TRX5yeGKhNvCJusc4looyg4btT+Lf1XBEHzV63U" +
                "2/IBPR8jMMLzI3ofLjZRpYndcso0qCFBFpcW0RtA3p0iehqlnGS9tx2YEiL9HHGe" +
                "Cf43T/UjxwzpRfXQR89XUjolF2TPVzn3kYZNgb6rpcwQtwOGnX9Vy69G4TeXLyeL" +
                "421xcL+BkeMTV6vF1CXeM38tKlMs7U/uSpkujZ96ruyqhoDA8R9l+TyU2ECpfS2F" +
                "hXqCA9IfvKdpE/0T7MBw4T/rbOb+zvdntC1zxgMU9MNF8TFAKaEp83L4A1aY7Ppw" +
                "oZ/aWwB5G99DuACGdNcZbIVrLmowkLM4478wggVVMIIEPaADAgECAhBBA3jSJjZZ" +
                "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" +
                "DBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjE0NTIxMloXDTE3" +
                "MDUxMjE0NTIxMlowJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3Bl" +
                "cjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAIEJv5/36LltIoqQc4+X" +
                "7rhylaiX09xw7Q97JM83OazNMpMxglMpuMiAIvKY8suaAGwrClbwj7Q7t8KeCD/9" +
                "nJTG4fL6SZ/VzY5lM362Zr9VftoWbpOgvvzaENhbyVxwsEu2Dg40j9NTtpSbh0Hd" +
                "ZSaE3CZlOcy6pLu/zHh2jvQoFRm36gH5UKh4T+ZbUbNc49ifYz/a2hbe2BO9W9YV" +
                "r8Lt/4veAmZrgIMGldeHQaHBqJW8tXnaHC6wP7NcYJ2/j4Frvr9ZzkwfoXWJt0nM" +
                "WNrSRHm07xlizU4aKcxCkjlfnslgGb2ZE2JikEQf844aLCf+BJYkhUeMEbca44KS" +
                "vuMCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAaIvCDFiyYCuE6Q+8U1j/Sg1ImY7V" +
                "NrBgjRe1QE8/XNDFYAj+sj1kOpmWoJ7Uf7nKZTZnRGp1fpCqTFBuy0IBFwsNyHMa" +
                "Z0Mwq9Qzm4hCbFo1IoGEGYhZtfsvUK9iuALiIscnrI9CATPliC9EZTQ/DyN4zaaW" +
                "sWlrBtsYhIWXBstPNWSl784ywjmbLZbB2cImY0p0AkQjhKfxhK3vKu/rSpe5snYP" +
                "qC9nVyyJ1lexrUMwbjwO2GDs2ZJ4c/GumFPTqOTW6P2lWEvYTWoRrSnQnsITYlhJ" +
                "owBMc0EXoL/Uy7pXnSTKRQ/bEa0qzCn++oI/heV4bLs02ixJbutioMbwlw==";
            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 void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

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

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // 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 Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <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 void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    }
}
