using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace C1FlexReportExplorer
{
    /// <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 =
                "ADABMAIwB4lDADEARgBsAGUAeABSAGUAcABvAHIAdABFAHgAcABsAG8AcgBlAHIA" +
                "H3V17bQgdR9ZSktgGwJIyn4pYuZS9U1duYINnqzC5P0z19EUvjI/RvSRbuYA0odv" +
                "wOYgPeXUL2J/rVBynnlU6NARiC5NWCzTPL9PD33Nymp7JclingZKXz3+HDArnAB4" +
                "9e7Bxo4DzpK6mGqgFwNy5B85BFVLx9jlfNnuLq+QHIIVPyO9QEvmtpOLtefjvQoI" +
                "Y3spIqsMAy6PLNEsC4swPGXEmz3C6I7zphYHyuy0leyjFKhpyJVGAqq31vcFWw2r" +
                "cKrenknVKt6Igz8ywnwneUtlkflkCgZaMz3Mhw2nDXQ9Y9GtuaXlYa5nvuhXiPWs" +
                "vbznllfWU6LrGg8oF2zBFxDBJUp1LKqqtqga1eWuZ/Nr0adn3pg/Cuikbf8phJ7h" +
                "/zdR/ZqEvVwWQJD0sXvI/izGfJoFv+qV6eVct66Dh4QfRVsX2PQmc4j2VtD8UM7T" +
                "CevPHYMsdLonUfIRIsMModD+lqog66FJUMVXbeYrSjKCo/6mdskCjRyV9XXXmjaS" +
                "CoT2LUFnWmbmtRmQdvTrki7X0XlrQpB45XNuYDBo2i1weJwe6soeDdK7bgoEHCzU" +
                "Ap74ai7/6Z4PhZcOvlJQtteEwlqfr3VHoStQFL1gcDo6v/SRGlLJ80tZsJ+8PwjG" +
                "/nw0RUZtYvr7v9xA3IZ8mPAY33HGcRpQwn0duxvqHGEwggVVMIIEPaADAgECAhBB" +
                "A3jSJjZZehbbJsa9EJSLMA0GCSqGSIb3DQEBBQUAMIG0MQswCQYDVQQGEwJVUzEX" +
                "MBUGA1UEChMOVmVyaVNpZ24sIEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0" +
                "IE5ldHdvcmsxOzA5BgNVBAsTMlRlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy52" +
                "ZXJpc2lnbi5jb20vcnBhIChjKTEwMS4wLAYDVQQDEyVWZXJpU2lnbiBDbGFzcyAz" +
                "IENvZGUgU2lnbmluZyAyMDEwIENBMB4XDTE0MTIxMTAwMDAwMFoXDTE1MTIyMjIz" +
                "NTk1OVowgYYxCzAJBgNVBAYTAkpQMQ8wDQYDVQQIEwZNaXlhZ2kxGDAWBgNVBAcT" +
                "D1NlbmRhaSBJenVtaS1rdTEXMBUGA1UEChQOR3JhcGVDaXR5IGluYy4xGjAYBgNV" +
                "BAsUEVRvb2xzIERldmVsb3BtZW50MRcwFQYDVQQDFA5HcmFwZUNpdHkgaW5jLjCC" +
                "ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMEC9splW7KVpAqRB6VVB7XJ" +
                "m8+a60x78HZWNmvAjs/ermp7pbh8M9HYuEVTRj8H4baXxFYq4i75e5kNr7s+nGbJ" +
                "5UfM6CldqbeRsQ4zvRb6npfrtcOBwfYOx0woeYQfI6VzLVzhPbGjUDK6qdHZLf/E" +
                "cvSIKvRbu3ILOE3kuux07gQkb++rrSBUyJKXU8nW8c+K+9sfWqHYPb8FF2IazWxh" +
                "mfIdIoDNKyjr3r/wIn3jQejpqfamiHNsU/O26KS0EcvCS5CFOKDT6vvdnAkek3ky" +
                "LWef+ca/cbFo74vawYGtmw2wzw/hcwlANIQaCAxz+5JHQZ3SEg8LLSZ+C8E4T+8C" +
                "AwEAAaOCAY0wggGJMAkGA1UdEwQCMAAwDgYDVR0PAQH/BAQDAgeAMCsGA1UdHwQk" +
                "MCIwIKAeoByGGmh0dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3JsMGYGA1UdIARfMF0w" +
                "WwYLYIZIAYb4RQEHFwMwTDAjBggrBgEFBQcCARYXaHR0cHM6Ly9kLnN5bWNiLmNv" +
                "bS9jcHMwJQYIKwYBBQUHAgIwGQwXaHR0cHM6Ly9kLnN5bWNiLmNvbS9ycGEwEwYD" +
                "VR0lBAwwCgYIKwYBBQUHAwMwVwYIKwYBBQUHAQEESzBJMB8GCCsGAQUFBzABhhNo" +
                "dHRwOi8vc2Yuc3ltY2QuY29tMCYGCCsGAQUFBzAChhpodHRwOi8vc2Yuc3ltY2Iu" +
                "Y29tL3NmLmNydDAfBgNVHSMEGDAWgBTPmanqeyb0S8mOj9fwBSbv49KnnTAdBgNV" +
                "HQ4EFgQUAFrwraXUeDX1hBKoLAUO+FYbjo4wEQYJYIZIAYb4QgEBBAQDAgQQMBYG" +
                "CisGAQQBgjcCARsECDAGAQEAAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQCIwphaN45b" +
                "5ViKsRfCA6hbVeqMhNCJmL64mqxdYvw3gsF4oOCDoiM6kWhy1NoUd2Lpcr9PjfGJ" +
                "55ZX4sZlqoxeflTpHgnX8MPJuKpDBsk7WkhiEu65fBwb+g9wQHMMfgY0gjHIAQ2Z" +
                "Ef2PJ6TTSFcvj24wh73dsyqcmj290fpwDtHaF2jE5pq/tQBOJ8vDkN46wt4qMhG6" +
                "nIpOgc9KngyLLdEO7V34KM1Nb+sN9JkqTWXNoNqlOf7QjYHwlw7Ku7W3kIGK+f1k" +
                "P1xqTBpXwsBE5sLlnujP+JAqV8aKlBJDKRI3IhlrG9CJUmqFNhmZ2f5QE5jAvPQe" +
                "XevLnMYiqJrgMIICxDCCAaygAwIBAgIEDu7u7jANBgkqhkiG9w0BAQUFADAkMSIw" +
                "IAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjE0NTIx" +
                "MloXDTE3MDUxMjE0NTIxMlowJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERl" +
                "dmVsb3BlcjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK9Rj3LujUzi" +
                "NX9YqAyARj/GNpuiaSdv6yU2PtavfkK7duTS8gGSMbyb2DF03tYa+yQHGdXcxuV1" +
                "xr7DZ1fl/G5HMdeqC7GgEcNokaiYYV/2DfRnneiD9gy5qLtkoUE8fBxjG1shG5Us" +
                "DeBxJOXdEOeZBjo8WBUN01LwOth0Tjd5PYF5SXTRQPxNWQN7Rr+ZXcWIk1+D7nyn" +
                "+Ey9zKjOEkWqqrpyX88jOw/Mv12ptWLd8vzsgPkszKVFS7QHicaqX0c1b/UCSnYj" +
                "TiFlZbP8Qcsa7qt89Clz+3wrx7NA1RRA0d4o1kBXZhFxKn+Ydzx8Pi/1Uyg3K57p" +
                "YErZr1r1Dy0CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEATGKyatmEnqPtKhAX6v30" +
                "qcBHxypU1wY8alR8Vg7SnZTYiH68KtZPIC1raa7eV/49pYnIoLi5zZu5eAbM5mas" +
                "WJ4uIYlt0VmMYs6IBnMdI0EiqC7e4TVbdZ5Px8My7NokTISpzuXtxKMSYC4MkLSd" +
                "fbiGfWCj9HEEmhTLX5KUUffQrvPyhIJ7Hro1U/yd3NMVPLD7ddeQpHA48cvDZj/S" +
                "UiC5pmtrgT3kE4DeKSZ9hwzQCiNfx/5K/nTW2qRHixFgJbOJ3Gup70gYVSxsI2Hc" +
                "YRDRnfmZvVHhEO4chQrOqC2wMGBSU+NsspU29sO08tJxyTZ3dYWw7tydU4EZivUj" +
                "Nw==";
            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)
        {
            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;

                SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
            }

            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
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        void App_BackRequested(object sender, BackRequestedEventArgs e)
        {
            var frame = Window.Current.Content as Frame;
            if (frame != null && frame.Content is MainPage)
            {
                if (((MainPage)frame.Content).HandleBackRequest())
                {
                    e.Handled = true;
                }
            }
        }

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

            var frame = Window.Current.Content as Frame;
            if (frame != null && frame.Content is MainPage)
            {
                ((MainPage)frame.Content).App_Suspending();
            }

            deferral.Complete();
        }
    }
}


