using GaugeSamples.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 GaugeSamples
{
    /// <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 =
                "ACABIAIgB3lHAGEAdQBnAGUAUwBhAG0AcABsAGUAcwCFukLc6JA2IRG/Y0CyoXVf" +
                "HaRCPpKApyHV1RvSExPIhelmHbBhqU6dBVI+og2ye2Q2gZYesRtv3T3AjmTV5CLo" +
                "IL5rETIlc2hOS4wWpRBX1mL1TW95+sT9CkMW7irJcA/RpopJhTFf7NBYsOlQ0Y6C" +
                "4x+ko1PhGnAaWYj5hO19CXvCfik6L+LF1B0sKCVXHiu53uW3HcDDAj2q6+FaG8j7" +
                "6NNio3GJ7LhgFP0lUSH2Vmu8xJQnKyd7mE5sOkNN7ZmgIoxk2akWK5ORptENblPL" +
                "Fe5a7OtOdkADBoIyl8dhqfzOnfjVy7R2CwRPJGlKlQ1gt5XqeWfrmdN02pOe5CBC" +
                "fJE6YPTOuvOxstxXy/a+rvg61F22DQCiIi7yUiGY8mXDh4zUzJ7Zvn8ukALKIwF3" +
                "jQjflj3urvD+SFwB0cZ6goPBDLfxKmWB0+26FS7f/vQzJy2V1373XEdVzrQjGUq/" +
                "q1B9BRPoDHvtITUBrg9WRV5L2/t8rTMcAwgt2FPvrcwPhXE/DqC7mLzktyVphajM" +
                "gtzhQKITjRW4MqjZF73a98zoBJrVhFtF8BbOJZ52a33eFsydgBARKXhfw2RPRS+W" +
                "jNTUK1pZk+QeIEnVTGgoRa/RrbjwQPSA4Yz10lybTwlY+qlTsqzIGjcrBr9T1f9A" +
                "NS6YTqwYz+GnNF7Qql1yHTCCBVUwggQ9oAMCAQICEEEDeNImNll6Ftsmxr0QlIsw" +
                "DQYJKoZIhvcNAQEFBQAwgbQxCzAJBgNVBAYTAlVTMRcwFQYDVQQKEw5WZXJpU2ln" +
                "biwgSW5jLjEfMB0GA1UECxMWVmVyaVNpZ24gVHJ1c3QgTmV0d29yazE7MDkGA1UE" +
                "CxMyVGVybXMgb2YgdXNlIGF0IGh0dHBzOi8vd3d3LnZlcmlzaWduLmNvbS9ycGEg" +
                "KGMpMTAxLjAsBgNVBAMTJVZlcmlTaWduIENsYXNzIDMgQ29kZSBTaWduaW5nIDIw" +
                "MTAgQ0EwHhcNMTQxMjExMDAwMDAwWhcNMTUxMjIyMjM1OTU5WjCBhjELMAkGA1UE" +
                "BhMCSlAxDzANBgNVBAgTBk1peWFnaTEYMBYGA1UEBxMPU2VuZGFpIEl6dW1pLWt1" +
                "MRcwFQYDVQQKFA5HcmFwZUNpdHkgaW5jLjEaMBgGA1UECxQRVG9vbHMgRGV2ZWxv" +
                "cG1lbnQxFzAVBgNVBAMUDkdyYXBlQ2l0eSBpbmMuMIIBIjANBgkqhkiG9w0BAQEF" +
                "AAOCAQ8AMIIBCgKCAQEAwQL2ymVbspWkCpEHpVUHtcmbz5rrTHvwdlY2a8COz96u" +
                "anuluHwz0di4RVNGPwfhtpfEViriLvl7mQ2vuz6cZsnlR8zoKV2pt5GxDjO9Fvqe" +
                "l+u1w4HB9g7HTCh5hB8jpXMtXOE9saNQMrqp0dkt/8Ry9Igq9Fu7cgs4TeS67HTu" +
                "BCRv76utIFTIkpdTydbxz4r72x9aodg9vwUXYhrNbGGZ8h0igM0rKOvev/AifeNB" +
                "6Omp9qaIc2xT87bopLQRy8JLkIU4oNPq+92cCR6TeTItZ5/5xr9xsWjvi9rBga2b" +
                "DbDPD+FzCUA0hBoIDHP7kkdBndISDwstJn4LwThP7wIDAQABo4IBjTCCAYkwCQYD" +
                "VR0TBAIwADAOBgNVHQ8BAf8EBAMCB4AwKwYDVR0fBCQwIjAgoB6gHIYaaHR0cDov" +
                "L3NmLnN5bWNiLmNvbS9zZi5jcmwwZgYDVR0gBF8wXTBbBgtghkgBhvhFAQcXAzBM" +
                "MCMGCCsGAQUFBwIBFhdodHRwczovL2Quc3ltY2IuY29tL2NwczAlBggrBgEFBQcC" +
                "AjAZDBdodHRwczovL2Quc3ltY2IuY29tL3JwYTATBgNVHSUEDDAKBggrBgEFBQcD" +
                "AzBXBggrBgEFBQcBAQRLMEkwHwYIKwYBBQUHMAGGE2h0dHA6Ly9zZi5zeW1jZC5j" +
                "b20wJgYIKwYBBQUHMAKGGmh0dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3J0MB8GA1Ud" +
                "IwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMB0GA1UdDgQWBBQAWvCtpdR4NfWE" +
                "EqgsBQ74VhuOjjARBglghkgBhvhCAQEEBAMCBBAwFgYKKwYBBAGCNwIBGwQIMAYB" +
                "AQABAf8wDQYJKoZIhvcNAQEFBQADggEBAIjCmFo3jlvlWIqxF8IDqFtV6oyE0ImY" +
                "vriarF1i/DeCwXig4IOiIzqRaHLU2hR3Yulyv0+N8YnnllfixmWqjF5+VOkeCdfw" +
                "w8m4qkMGyTtaSGIS7rl8HBv6D3BAcwx+BjSCMcgBDZkR/Y8npNNIVy+PbjCHvd2z" +
                "KpyaPb3R+nAO0doXaMTmmr+1AE4ny8OQ3jrC3ioyEbqcik6Bz0qeDIst0Q7tXfgo" +
                "zU1v6w30mSpNZc2g2qU5/tCNgfCXDsq7tbeQgYr5/WQ/XGpMGlfCwETmwuWe6M/4" +
                "kCpXxoqUEkMpEjciGWsb0IlSaoU2GZnZ/lATmMC89B5d68ucxiKomuAwggLEMIIB" +
                "rKADAgECAgQO7u7uMA0GCSqGSIb3DQEBBQUAMCQxIjAgBgNVBAMMGUdDLVVXUCBJ" +
                "bnRlcm5hbCBEZXZlbG9wZXIwHhcNMTYwNTEyMTQwMTU1WhcNMTcwNTEyMTQwMTU1" +
                "WjAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMIIBIjANBgkq" +
                "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh1JVaniGbXPpu3y/LHkHkPdA33si8og5" +
                "p5Zihfl428/LytNtQHtfbwGtLzGG8GCZEcpeqMZNoN4vDzVSi6u0icGM5TsX7n1y" +
                "hne7+MPrvnaT158yoQjxqYEBU2XSHZJXFuG+VPiu/cJlEKfYX4PB7hj1PpmJxahy" +
                "sO+t/wQYxUZ24TNpoD8CClk6qK7sZXM/s5GKN4xdoFp/C7ek4D4FCHUDIi2K+2sK" +
                "lsHj/kB+1XSk6FoA3nehNqRsX2+GfOjxKocpev4+X6kx9QHOY+U1QbkY4tExM3jI" +
                "xiWMqQyY/FBnAVzGZbWUIshxSee9SM5xh4jaKTLnG15JvcADWtA+BQIDAQABMA0G" +
                "CSqGSIb3DQEBBQUAA4IBAQA7KJW7nwPzl6zc6U0MdoYECNdmMLxA2w6VBtiaaeLf" +
                "BfpDdlT0/vQ6nv94VH3zdQGlWYdru6JHRhU8S1cqUskABxwu7cvXobS+FHbh+ngO" +
                "bKxCxlAyVsQ8Q4LjEVfOrWUfh7S7G+XnP3CI8kWbdlp54V40QABZ63AJRlfLdQtv" +
                "ZW2TtgL0jQpx+kF8219lPhVpUBtLC5EXKTGaAgB5qxrNnCmOMGUSz4EyDu1/WZGe" +
                "2Z34cDIbtv+WAFud7OWzIwUylXiA117fafevF97sGlIZmZmcic2gnso02FrIv4HW" +
                "TGuCOAtT9ecAK9e0n86v0MpqL9TbbeaxZZSR/XbfFe7p";
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <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("Failed to create initial page");
                }
            }
            // 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();
        }
    }
}
