using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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 WealthHealth
{
    /// <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 =
                "ACABIAIgB3lXAGUAYQBsAHQAaABIAGUAYQBsAHQAaABO9LthDkgJpBIAwAlzXxqU" +
                "xfcYXP/QadVxG3ikrd5CiZQu28rbGCCjCjA6Ps/VNMR47brolWnAP5BWi7PdyvA7" +
                "pZ99W/9ZZj2j46aKgrE82DJO2DWiwKB7P2fCLuITM5cRoQ1h3rlRjkKppUXeVqYi" +
                "yLWz40c5zA1zzwJww00RXxuJ6gdYBec6riaVYuXBb1+Lk+4jd9VxYcKoKeACxMvl" +
                "n9hgfDPdW8rP2eEl4NCGtrSjjRbw7X2AP8YhnpxPUqBw9f2ecVLsYp3jJCmQb6Kh" +
                "gb103Zh+V5JeEFFH9O18h3LjCE8Yy0BPGQ361uURJjWKNmvs8vpqDNIkUxqkok7J" +
                "D+mTa8ihEaeqbJr45dVDTPD6JRMNrMgUuEj3wsbpixuD7LmLA0zY3KRXhkhizWgK" +
                "4X9ImpgoIE4DAVZicLTtu1j4arb2RFoAbty3pFXuQ7TU7WPczkSSjb29Rc/h+tyv" +
                "pNTNZhB1PLOJW3wXHqBt9ebHoYKbqo2v6sNUS5/qHg9IHDOt6dlpX2IYHGuRVXv/" +
                "XDPd79omhHJkSvP3blkF1sCcH/kzSBaQMdTXMhJAnr5wtm2koSFRsWtbYpNF8NMJ" +
                "NpodLDiRMfU3DQBYNjUKxwp35hRRNz1/G1RKi8eD0Zm8kXw7RSTiE9JMM1UBlH35" +
                "ilbhmH/ZnIVS1exOAuZTFzCCBVUwggQ9oAMCAQICEEEDeNImNll6Ftsmxr0QlIsw" +
                "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" +
                "bnRlcm5hbCBEZXZlbG9wZXIwHhcNMTYwNTEyMTY0NjEzWhcNMTcwNTEyMTY0NjEz" +
                "WjAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMIIBIjANBgkq" +
                "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApUtYlLsjcHpD2qe6zLMTQqCEuinX3ahv" +
                "kEkJQlA+out0JcSJrjt68mOrJLp0Yz+Pi7S3YTClVIexYFRVHr4Krq5gEKG+DF9j" +
                "xAQVmXZR53BP6nuWtw8gXfhhnhk9ekAEOWZR0r6FxrNQgMrdUKaGPMCI/hL7VWqI" +
                "A5HP7N0pzl8gyyIGWVN7kKDPPftMz57GzZN5AfPmiG0kLlDkSAAT6lbYxIoZchoJ" +
                "WAv/KiDRpu/I6hOcP0D7KQJcRq/97ET+F6vlswzWtnxjooYUEjzLDLrQ+ZvbeAmJ" +
                "THaE0Mx29LTqfTJikokGKJpMinWlZspXwEw0ZiwzkTnWIQFK1d4p/QIDAQABMA0G" +
                "CSqGSIb3DQEBBQUAA4IBAQCVtcvLu6d272tMjrsBcvzqJdzwKEMqtKLoyTmAFNMM" +
                "LlmoIJk9pcCt2X1+VBVuV2G7OdbYt8fuXFDSdCCPWV86R0dpfo+Yl8D4w7LlAhuR" +
                "kcJ6et9ISA0ai7NHgYhu+mfcTa28kPS0PJttjPcVOC3/vPzSCWgTagTfsy6OQvJC" +
                "WvlfxEPsTU6gcXkjVBPDLT97LFgjGS0jjA5fOELGXC3G2GTRibUXEsL2YiRs5TPs" +
                "VSqFy5o7O2EiLlnojerdwUTZU6DnCP0TIWzDhbhN9rg/PHzMVAeIJ2/4JSZcaTNQ" +
                "dVtRxCRr2tKi8sBqtaXDp2jRbSPUgrukK2zdv5YZfMX3";
            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;
            }

            if (e.PrelaunchActivated == false)
            {
                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();
            }
        }

        /// <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();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    }
}
