using SparklineSamples.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;

// The Grid App template is documented at http://go.microsoft.com/fwlink/?LinkId=234226

namespace SparklineSamples
{
    /// <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 =
            "ACgBKAIoB4FTAHAAYQByAGsAbABpAG4AZQBTAGEAbQBwAGwAZQBzAFNKm6ZvoBxI" +
            "mIsc/nGJXaRNlOWwMzPFhZA4E71w3InDd0/vRaCf7wpY5F/UW+xnYK6rpL+QIb+m" +
            "lbCNXtTzD6LrVHNtgISgTFW8/mpzim0IqQVYlYV2WlQS+YmZam0uXTl4HT5PKGhE" +
            "hwmQSF+IJeJyi1C31K/ehsB+M3ZmsdA2mDMU1koMKTlfqKkgKe42GFga9GlwObaU" +
            "LdWYzc1LujjJR5IV7db0UsGns0yMVToG8S9O52jLMR7fysJQqp4gvT6c4/0q35Yq" +
            "YexJTCI7ZLw0P4R+4lZupi7yRDE0/wmGpSM+Ks032s8tyZ8xOC3kkNLVMfT/40b2" +
            "ikT/qGdN5Zqhs82oD+q+0GXxYVfdmnM4T4aTR5edo/tYNiz2BhlO55nkns3X/OIR" +
            "FzHgnjQHJOjJBt+m/UrI0rTGprynVLpW9rTnYPzRs5B3hlEpHp8mtScAY7SVB4hv" +
            "IHWE6GqEIK4p+MLZh2cbzqcoLl7sZeAh06119ATv9JXihawqQMCMxrFsZUht+08G" +
            "JHDb6oXI3x3OiwVdTrkn5rWpr5BjeBYwXlk/SwIrxL5xdkb/uAww3Y/5PVShOmz4" +
            "K4SoJmF4Ybb7MbEuYUrCNPYiZ60znwoUuJ5TbMNkf3wCM5auRXqoKwKvTOKbpeof" +
            "kF6kmos5CD27C+dSE51EYKmUH6p82d21MIIFVTCCBD2gAwIBAgIQQQN40iY2WXoW" +
            "2ybGvRCUizANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCVVMxFzAVBgNVBAoT" +
            "DlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3Jr" +
            "MTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24u" +
            "Y29tL3JwYSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNp" +
            "Z25pbmcgMjAxMCBDQTAeFw0xNDEyMTEwMDAwMDBaFw0xNTEyMjIyMzU5NTlaMIGG" +
            "MQswCQYDVQQGEwJKUDEPMA0GA1UECBMGTWl5YWdpMRgwFgYDVQQHEw9TZW5kYWkg" +
            "SXp1bWkta3UxFzAVBgNVBAoUDkdyYXBlQ2l0eSBpbmMuMRowGAYDVQQLFBFUb29s" +
            "cyBEZXZlbG9wbWVudDEXMBUGA1UEAxQOR3JhcGVDaXR5IGluYy4wggEiMA0GCSqG" +
            "SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBAvbKZVuylaQKkQelVQe1yZvPmutMe/B2" +
            "VjZrwI7P3q5qe6W4fDPR2LhFU0Y/B+G2l8RWKuIu+XuZDa+7PpxmyeVHzOgpXam3" +
            "kbEOM70W+p6X67XDgcH2DsdMKHmEHyOlcy1c4T2xo1AyuqnR2S3/xHL0iCr0W7ty" +
            "CzhN5LrsdO4EJG/vq60gVMiSl1PJ1vHPivvbH1qh2D2/BRdiGs1sYZnyHSKAzSso" +
            "696/8CJ940Ho6an2pohzbFPztuiktBHLwkuQhTig0+r73ZwJHpN5Mi1nn/nGv3Gx" +
            "aO+L2sGBrZsNsM8P4XMJQDSEGggMc/uSR0Gd0hIPCy0mfgvBOE/vAgMBAAGjggGN" +
            "MIIBiTAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDArBgNVHR8EJDAiMCCgHqAc" +
            "hhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNybDBmBgNVHSAEXzBdMFsGC2CGSAGG" +
            "+EUBBxcDMEwwIwYIKwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUG" +
            "CCsGAQUFBwICMBkMF2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMBMGA1UdJQQMMAoG" +
            "CCsGAQUFBwMDMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3Nm" +
            "LnN5bWNkLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5j" +
            "cnQwHwYDVR0jBBgwFoAUz5mp6nsm9EvJjo/X8AUm7+PSp50wHQYDVR0OBBYEFABa" +
            "8K2l1Hg19YQSqCwFDvhWG46OMBEGCWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3" +
            "AgEbBAgwBgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEAiMKYWjeOW+VYirEXwgOo" +
            "W1XqjITQiZi+uJqsXWL8N4LBeKDgg6IjOpFoctTaFHdi6XK/T43xieeWV+LGZaqM" +
            "Xn5U6R4J1/DDybiqQwbJO1pIYhLuuXwcG/oPcEBzDH4GNIIxyAENmRH9jyek00hX" +
            "L49uMIe93bMqnJo9vdH6cA7R2hdoxOaav7UATifLw5DeOsLeKjIRupyKToHPSp4M" +
            "iy3RDu1d+CjNTW/rDfSZKk1lzaDapTn+0I2B8JcOyru1t5CBivn9ZD9cakwaV8LA" +
            "RObC5Z7oz/iQKlfGipQSQykSNyIZaxvQiVJqhTYZmdn+UBOYwLz0Hl3ry5zGIqia" +
            "4DCCAsQwggGsoAMCAQICBA7u7u4wDQYJKoZIhvcNAQEFBQAwJDEiMCAGA1UEAwwZ" +
            "R0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNjA4MTgxNjQ4MTNaFw0xNzA4" +
            "MTgxNjQ4MTNaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIw" +
            "ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCUT0wPILilvxOS2LNffvak" +
            "hoVeW87JleZ7sGUu5A+sdBKjDA0uQjqmViV1QNik7lccBfU27APUDCXg83D0H38Y" +
            "9KIxFzlL5xzuihHnQoFJ45BoRzEEMUP5EvebQvSMwrYc1rJIRU5sx0KpsZ0VpHI9" +
            "rmEZRHUoxBiFw1JBzpO2yJaMsUI6T6AMGWhy/6zKjXVaUITpZjskPDvE65E0PgTa" +
            "eNsbN2Il6uKlvSXZTPHDs7oXK+a06Pt9gI2jQr+vJe/dWWDtvRLLTKsDCNmyqah7" +
            "t4C2ZC93v0p8TAYN27yKkuhgb3AINhyCDJOIzRhemsI2uE2wgL2xGEnnfKGW43nt" +
            "AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBACkJQUysexN32waIPusqfTwaZI/bEQqk" +
            "dhI7qqwuLi6Tc9AK/GKF19DBewXc9c+0PWNn4ch8NoofKpnewg2laN/xOP+RxM4F" +
            "sBhHZL+pypEFbtQ+gzQ981FozmDVZDMpYi4PnNxH+piR2L13umbsT7fAUY0xzGQ2" +
            "R260aeiAlRqo97TH+YabzYyMOLGGqRlUMe5lDrUp9e1xo95vHS+lTOiDHc7199wi" +
            "iFoyQ//9agLJR+m9JlJlIVm7WBaKBrYy1fWZX6/bm6/XO5wu/RVwDt5CElR4JUS2" +
            "ugU91hhQv4wliFspF6S+w3dWTrSHygLlzXZ5YAN3mklYOR+IJnZ/m9I=";
            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 async override 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(ItemPage), "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();
        }
    }
}
