using BasicLibrarySamples.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 BasicLibrarySamples
{
    /// <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 =
                "AC4BLgIuB4dCAGEAcwBpAGMATABpAGIAcgBhAHIAeQBTAGEAbQBwAGwAZQBzAK46" +
                "7PPXqLChNWPUCQ26ftg5u4yZwikfNoGQUwvBr0Qd+7hQL9bFFy3vxPcsCIfA8bBF" +
                "35RrkNRNq9EYcDlSfexr4oF6eW45PKZy+W1WHOgfSMfuP5WCT/vf7amUpKBZNGlz" +
                "oQDxqIac7wD1cdXusmK27eadUdUAzyy5zF6wZ5/IzPAH5ej34fFBr7ajZ+WUezTs" +
                "WWkCQl4DxOlhg6vP8ZKH9RpxU2j54lgUVaB4NgWHNWF/87VSktLt1P9V04PvstXs" +
                "qA7S6HjLpmEfsrDxoX9H7rhKo35ZqqaZGk5Y3DhEd4361K2y78gyxOVCvx1qrT89" +
                "bafXrHU8s1AVCfFXga99Pb8ed891ExtmshtmP6eu56kBQttHIqK5wvosQXJky14W" +
                "ouO9ILFUP6UAsbIxyHz1W4IfIDlEd/uJv/F+ED+jb9bhcPTaKbc7APqsR3ByVA3u" +
                "a/4eO2Ihmx3/flogzsG5zjQ2owfPFw9jMYrlATxPHhyxdMDHjwrP1rmfk58WN/jl" +
                "M/83GU0VYFvcQ3U9aHyQX2QMpae3tpu+vT12T1Qa2Np197dNPSBdg/vdoAFQUJzC" +
                "sibe1JGkLJkMM/q8Sua8CI4CfoswhEBAIx99h+xYqcPB3/M0WnMuFK5GIVWjWBB+" +
                "ganw5zWFso+v8MpHfc6HQzjVdd5puJ8h/3lr16juMIIFVTCCBD2gAwIBAgIQQQN4" +
                "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" +
                "A1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNjA1MTIxMzUzMTVa" +
                "Fw0xNzA1MTIxMzUzMTVaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZl" +
                "bG9wZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCx1N9LvFluZoHx" +
                "MM4BNJ8f8SteZvv9B/hk0Vp2C0u3ehUtbYgNjyqAyEPooOEWzQSz2xmjTH4m+d59" +
                "C6+K5j6UKUP0PeLuQpTQxpoZXMzRItZlLUk80wuSfjOLZV9VotJNB81rHF59JvJZ" +
                "jnMDu3FNXi2hHN/qy4V2wih5zxkaaijDOxTIO2Fq5JFMdi+tZ1lHQE8sDw+C78YT" +
                "IEB/8IrGzs8LuX6zqg12A5lhURbnMYu+Qy6CzATIEiQ7dxaVtJq5aQOkeWU2EG20" +
                "TjUPdCOKAslIG0GBTGPKcLIA3hioXPFA3l8D5+5MyQWkI/pRBIHRZh8NTSjYNhG8" +
                "V+VtfKotAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAIL3npLrzvCmLSS538Iis7f6" +
                "rypfjfrE9NLHEh+w/RcAac34EKID3SYETz6hlD+cpXEz6vGFyuIZGMWXjOT4uRmU" +
                "E9YNJtUMwewbY6PSB4cp43Ku1NCZt+IdubN/qjkQgSp1fcfj3zY6S5rQUXKeLAxG" +
                "yGMs02SpOZE+15aW7bxo/QIlBV41sp5aa99vliGNMz8UrW/vFihzYL3k8VyUGnla" +
                "Rso0xGWXeclmcpy14a5bizw9dX3khjjY714xkoKvLb/6JHa5BjFlHJQkx29Q54UU" +
                "eYuWIMKUoXhEgC23yQFbXsu0ULxw+Y2KBa591wpbld96hyc3J3w/HsCRcTQb+0g=";
        }

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