using FinancialChartExplorer.Common;
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 FinancialChartExplorer
{
    /// <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 =
                "ADQBNAI0B41GAGkAbgBhAG4AYwBpAGEAbABDAGgAYQByAHQARQB4AHAAbABvAHIA" +
                "ZQByABgF5XE+NNeZ136sm9r2r/wooJg7ArvUhU2IDN3H/w5gKuL/p/J80OtcGn4h" +
                "Of35Op77+rvPctsridrd/FvwN1WxJUr0LB+w44kRXCaYTa65MYXofn3S80wDHW8y" +
                "OrncqxpM/sCXJ/EEs2MXO02+7aIxgLHyy30ivLIG6V99YwHtT0FyPPtBAYfHjb6G" +
                "JM6tfZ0csvcX8xR3rFViBjoMlMKlO0EgNCw0s7GH63epEfwJclYiltLo2z6KWPUB" +
                "RqTh7OrDDYNDGjnT1afWvPsDQMRmvJ/xDcZgG0Sx+WMths/e6SuYrOYFZW0Lu46G" +
                "TsjwB6oyaqFC4Hyr8Lo1bEFAxp6TMS8+7GRvDKbz7QQUZOwR2zoOFxJ+6+RUJHFT" +
                "WUNUvF1J6dTgeTKizofNhHIjm3BK0gj/331XG00xYePK6Q44MO8n/igvrg+wQ2hs" +
                "C9Uemqt1/Nqh6h7K4JkkJ11obDLahTtO4DxUCcTi/9Y2Gx3ICQ9UBULUou3CddAo" +
                "uukMJ27jU9O1T4iNQwakXaq91iUWNxHEVmwoLNqC6dYVrATCnmomm2qGAXeWhDiy" +
                "L5QOiBeTpCeEQo1bRGQvQtP+wwSKxB0TjrkNLdNvkbs09qq2TxzQSqN//l+eQMbQ" +
                "9Cb75aodBlazrhCxPweeoQsZNcWMmxD+yhMV77k168NQ37TCMIIFVTCCBD2gAwIB" +
                "AgIQQQN40iY2WXoW2ybGvRCUizANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMC" +
                "VVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBU" +
                "cnVzdCBOZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93" +
                "d3cudmVyaXNpZ24uY29tL3JwYSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xh" +
                "c3MgMyBDb2RlIFNpZ25pbmcgMjAxMCBDQTAeFw0xNDEyMTEwMDAwMDBaFw0xNTEy" +
                "MjIyMzU5NTlaMIGGMQswCQYDVQQGEwJKUDEPMA0GA1UECBMGTWl5YWdpMRgwFgYD" +
                "VQQHEw9TZW5kYWkgSXp1bWkta3UxFzAVBgNVBAoUDkdyYXBlQ2l0eSBpbmMuMRow" +
                "GAYDVQQLFBFUb29scyBEZXZlbG9wbWVudDEXMBUGA1UEAxQOR3JhcGVDaXR5IGlu" +
                "Yy4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBAvbKZVuylaQKkQel" +
                "VQe1yZvPmutMe/B2VjZrwI7P3q5qe6W4fDPR2LhFU0Y/B+G2l8RWKuIu+XuZDa+7" +
                "PpxmyeVHzOgpXam3kbEOM70W+p6X67XDgcH2DsdMKHmEHyOlcy1c4T2xo1AyuqnR" +
                "2S3/xHL0iCr0W7tyCzhN5LrsdO4EJG/vq60gVMiSl1PJ1vHPivvbH1qh2D2/BRdi" +
                "Gs1sYZnyHSKAzSso696/8CJ940Ho6an2pohzbFPztuiktBHLwkuQhTig0+r73ZwJ" +
                "HpN5Mi1nn/nGv3GxaO+L2sGBrZsNsM8P4XMJQDSEGggMc/uSR0Gd0hIPCy0mfgvB" +
                "OE/vAgMBAAGjggGNMIIBiTAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDArBgNV" +
                "HR8EJDAiMCCgHqAchhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNybDBmBgNVHSAE" +
                "XzBdMFsGC2CGSAGG+EUBBxcDMEwwIwYIKwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1j" +
                "Yi5jb20vY3BzMCUGCCsGAQUFBwICMBkMF2h0dHBzOi8vZC5zeW1jYi5jb20vcnBh" +
                "MBMGA1UdJQQMMAoGCCsGAQUFBwMDMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcw" +
                "AYYTaHR0cDovL3NmLnN5bWNkLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NmLnN5" +
                "bWNiLmNvbS9zZi5jcnQwHwYDVR0jBBgwFoAUz5mp6nsm9EvJjo/X8AUm7+PSp50w" +
                "HQYDVR0OBBYEFABa8K2l1Hg19YQSqCwFDvhWG46OMBEGCWCGSAGG+EIBAQQEAwIE" +
                "EDAWBgorBgEEAYI3AgEbBAgwBgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEAiMKY" +
                "WjeOW+VYirEXwgOoW1XqjITQiZi+uJqsXWL8N4LBeKDgg6IjOpFoctTaFHdi6XK/" +
                "T43xieeWV+LGZaqMXn5U6R4J1/DDybiqQwbJO1pIYhLuuXwcG/oPcEBzDH4GNIIx" +
                "yAENmRH9jyek00hXL49uMIe93bMqnJo9vdH6cA7R2hdoxOaav7UATifLw5DeOsLe" +
                "KjIRupyKToHPSp4Miy3RDu1d+CjNTW/rDfSZKk1lzaDapTn+0I2B8JcOyru1t5CB" +
                "ivn9ZD9cakwaV8LARObC5Z7oz/iQKlfGipQSQykSNyIZaxvQiVJqhTYZmdn+UBOY" +
                "wLz0Hl3ry5zGIqia4DCCAsQwggGsoAMCAQICBA7u7u4wDQYJKoZIhvcNAQEFBQAw" +
                "JDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNjA1MTIx" +
                "MjI0MTlaFw0xNzA1MTIxMjI0MTlaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5h" +
                "bCBEZXZlbG9wZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCb8MSL" +
                "WgnnD6+z4tCmLiV8fH6/mSW+5xAYvwcJoiAGqDAd1xYcGjd+tH4a8oZIoopz4wAU" +
                "bxe2jIe9E5IMQV93sCETWqgxoOwm5+FvqHAoCJSRPQMXw111euP9mjiTuWOFIjB/" +
                "pO2Xlpoz6K5wOvBfnL4RIxQGCU1y65xHqfEpZdej9pWvW0RRUn60q2xda9q0kZoO" +
                "41ev1dRqXKrPKTxVcOptOLQqV9yj/NHM/R115HmlgX9EdUpgOPasIUfWxoAe24o+" +
                "cfK5h8XZHlhYtZEaLh8lsjf3lN/nUKoF/q0tTDqOXk+PKcV0QgDRe2Uoj/z/tHsA" +
                "a/fc1n+XK7up2fN9AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAFgYNbZkgbqHDYJ1" +
                "4/WsQUJn/LzDk6Hn553gnxlpsUuGAGJ2nvpGBK5VZQxDQpzySi/AmXZuCEPS+l2E" +
                "yzcAF9cPENu2dKQNA77FzfVYzUizg9KsPFnPrr0h2Kzu1uykvR6Btibb2BBr3OYl" +
                "YJt34POZiG+VtpYS4XJxYKdT6jin63RFTfHYQTsCoeUqtqnKJ1wSVWmhIVkqCeTT" +
                "6tQlJaMPMUobUay8tK7kuh9W9qo9TSkrrTLnPAgtADyRV89pBVZ1r0TedBA/+OOB" +
                "JNqIQsqLz5mfy1j1569KAo2/8TgGm6gTLKuQsaPvCmPSB8s5OnMYzfuVIyV+ssPj" +
                "z7FdnXU=";
            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 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();
        }
    }
}
