using GridTreeViewSamples.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 GridTreeViewSamples
{
    /// <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()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            C1.UWP.LicenseManager.Key =
                "AC4BLgIuB4dHAHIAaQBkAFQAcgBlAGUAVgBpAGUAdwBTAGEAbQBwAGwAZQBzADp7" +
                "T7ALcryfkxmmlticVb6guDjQkOtRzxHHivdD6kxUth6WE8WCmYvqX3olgzzXQi4P" +
                "zAHnNOwgE5TFP39RXr+pQKSXirItW1o3ae3s4VWMKK+v4IJCI/gLWmJE4uTUL67j" +
                "Bq1U8FlD22Q3/hRmWNKLkhVKg1jQBrbOVFZZKQ4CU98gNiTZEIsv2butg+grFf7e" +
                "8L6jEBRU9MEOIahtzY635sca4+e+Mw4y+jtEKkC++Pc7l8sF0hw4TzTN4steNtxa" +
                "P/lIAgejdUfx4c5oo2Xr7FdOf5H1OxSq9xES+jsjG1oqMaqvZSfMyDq4OgFoQO4I" +
                "sga2NDfwNYeYpxe6zc+n/iqVuc5NsgjnQFEiKcxzFQ5lkWLQOz1hP2tcXe/cEe6R" +
                "/xvs7PKxGzt+KfIfNOlzL07jvjRiA3yBtBVooS1i8OvO/nZHoXzu7bhZ6R/yzdRE" +
                "7DGilzHkF4fpB1ts0/oy+A8kBrsYEM2HXq2qODNynD2MsCn/slq0w+Mm2VzCRzi7" +
                "+hWU5asqvE66EXRX1BR5LuApgTnF2eOMaGBfMjOgM46cJbRqINRtouf1V6vANsZw" +
                "FpRXO7+AQU/eLFjhnS/6O6bHTt5r86PZVCfFh/8LVXiCAWsm7lFVahTCba9degtc" +
                "nVbUUc1xiduW+WGbs+8qLz/VAa33s2V8oXVRhbbiMIIFVTCCBD2gAwIBAgIQQQN4" +
                "0iY2WXoW2ybGvRCUizANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCVVMxFzAV" +
                "BgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBO" +
                "ZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVy" +
                "aXNpZ24uY29tL3JwYSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBD" +
                "b2RlIFNpZ25pbmcgMjAxMCBDQTAeFw0xNDEyMTEwMDAwMDBaFw0xNTEyMjIyMzU5" +
                "NTlaMIGGMQswCQYDVQQGEwJKUDEPMA0GA1UECBMGTWl5YWdpMRgwFgYDVQQHEw9T" +
                "ZW5kYWkgSXp1bWkta3UxFzAVBgNVBAoUDkdyYXBlQ2l0eSBpbmMuMRowGAYDVQQL" +
                "FBFUb29scyBEZXZlbG9wbWVudDEXMBUGA1UEAxQOR3JhcGVDaXR5IGluYy4wggEi" +
                "MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBAvbKZVuylaQKkQelVQe1yZvP" +
                "mutMe/B2VjZrwI7P3q5qe6W4fDPR2LhFU0Y/B+G2l8RWKuIu+XuZDa+7PpxmyeVH" +
                "zOgpXam3kbEOM70W+p6X67XDgcH2DsdMKHmEHyOlcy1c4T2xo1AyuqnR2S3/xHL0" +
                "iCr0W7tyCzhN5LrsdO4EJG/vq60gVMiSl1PJ1vHPivvbH1qh2D2/BRdiGs1sYZny" +
                "HSKAzSso696/8CJ940Ho6an2pohzbFPztuiktBHLwkuQhTig0+r73ZwJHpN5Mi1n" +
                "n/nGv3GxaO+L2sGBrZsNsM8P4XMJQDSEGggMc/uSR0Gd0hIPCy0mfgvBOE/vAgMB" +
                "AAGjggGNMIIBiTAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDArBgNVHR8EJDAi" +
                "MCCgHqAchhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNybDBmBgNVHSAEXzBdMFsG" +
                "C2CGSAGG+EUBBxcDMEwwIwYIKwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20v" +
                "Y3BzMCUGCCsGAQUFBwICMBkMF2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMBMGA1Ud" +
                "JQQMMAoGCCsGAQUFBwMDMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0" +
                "cDovL3NmLnN5bWNkLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NmLnN5bWNiLmNv" +
                "bS9zZi5jcnQwHwYDVR0jBBgwFoAUz5mp6nsm9EvJjo/X8AUm7+PSp50wHQYDVR0O" +
                "BBYEFABa8K2l1Hg19YQSqCwFDvhWG46OMBEGCWCGSAGG+EIBAQQEAwIEEDAWBgor" +
                "BgEEAYI3AgEbBAgwBgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEAiMKYWjeOW+VY" +
                "irEXwgOoW1XqjITQiZi+uJqsXWL8N4LBeKDgg6IjOpFoctTaFHdi6XK/T43xieeW" +
                "V+LGZaqMXn5U6R4J1/DDybiqQwbJO1pIYhLuuXwcG/oPcEBzDH4GNIIxyAENmRH9" +
                "jyek00hXL49uMIe93bMqnJo9vdH6cA7R2hdoxOaav7UATifLw5DeOsLeKjIRupyK" +
                "ToHPSp4Miy3RDu1d+CjNTW/rDfSZKk1lzaDapTn+0I2B8JcOyru1t5CBivn9ZD9c" +
                "akwaV8LARObC5Z7oz/iQKlfGipQSQykSNyIZaxvQiVJqhTYZmdn+UBOYwLz0Hl3r" +
                "y5zGIqia4DCCAsQwggGsoAMCAQICBA7u7u4wDQYJKoZIhvcNAQEFBQAwJDEiMCAG" +
                "A1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNjA1MTIxNDE0Mjda" +
                "Fw0xNzA1MTIxNDE0MjdaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZl" +
                "bG9wZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC46SxwQoNOwmrM" +
                "vV7beMQnEAuPHIooW+3uE8A81AAOsIwvkQHHRv6AuGnpF1j/csgclZOD90mWoIrQ" +
                "OfC1ac+GgRQAZQA/fMrDzRcnxXEZUA3DhIVDi+LH/L69haazPVdt9cEX42MTdqBP" +
                "wZBbDlsqIhK0BA2L+sllm1cOqOMYFf6NKOtKzJD5jXYtIQlLpXM7aCrd/UY9JLTz" +
                "L2tK4TF6RSucm/k+luoB1YJnBKDX0to5AfF44PMZvJkdgsigNst6LsfsnJPWRxtR" +
                "j3eNKo8a3iS7QxZbXX59mnKoI60VWmH+gOOqx/AOsxeiDTqcJln2kOqq0FNx4GJN" +
                "vNOnfwwfAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAFNmhiS450JsieadGyNPIKPC" +
                "b/M8PPXZhb5dj1rPEhuit1Wavk8eNv+LcYLR4BqRQO28MpjTds7OZkHatduIOcb1" +
                "SLxuKVIS95VA96LUC0dQqBJxAuiLqGu3Q/RiAKq98xPvNqPoUIDOgcyp6dfLOOFh" +
                "thCZkrHps2Ynvuijqz7LrP8rjbPlrU3ZZymDLGc+EjH7/zr3Lziq/1WnEzB4GZEI" +
                "TGbLvovOsgq4/rncmbRHdLkchFs9imr6AFYzTBMqEGnwghp+NfdW1suxUZ5HxLAg" +
                "2ctg6oRD9EzJ5Af74Q/La8Utl6naIiCJHtwTmD+5Zqxcz0mFEM8pVFWsUerTVRo=";
        }

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