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 CurrencyComparison
{
    /// <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 =
                "ACwBLAIsB4VDAHUAcgByAGUAbgBjAHkAQwBvAG0AcABhAHIAaQBzAG8AbgBwhchE" +
                "oj2MYcT64GVrn/ZCGwCIzSdYFSfvSrLvv1yXQ2tNTbOHWbOqlkSue4pLTv0dqU+l" +
                "/ZPTRLZXriQvU6H5sucrC9gxSHpm6yVRdu0UbOmcZ+HhPTxpQLA16b9I8gOjIL9l" +
                "tEOopTlp4S4Q+v2TWhy+zL3FPlxmZpo9m9Zm44erlMdSmSVxJspN7msaM1TR0+v/" +
                "dTthGlpnFNfLxH7Q1UjvV7AmWN0L4eNznoZhaTPtwcF8tb11taDSiTbMbh2IfhND" +
                "Au3IY55AEL1oU8vZ4rMUmrShD8/MerbRlBXOcxpRpNZYvFbBTAVIDHERZXKfF54y" +
                "EhMeT5abrcZdxn6pjk+lSEdWma0ZMt04LTuk2P8EkPjU6N+5zHR5Uc6izi/tsRwm" +
                "Fq7rw/wsRM86PC+fOv59DmjbID9lI9fKpuaA8zfOQCicVWGecatkT8pOM/sSsvtE" +
                "+XZq6/FSZKB0loHl+h0R3ERZZiQJuMLuUMceoHAXhON1v3yrZdDhD3VNBEtW18FO" +
                "+kr3oHFaWc/V9BECJmjSJRJscAahPKJzWpI02DusJZ2CY77AdUpdRnJ+2LQfucys" +
                "FSADhcztpSSQoH4w2io9fw4pmN21acuN3SPK5YTFBjlfIk8uyUU9ss906W1tphz9" +
                "skDPFRrTWLJm5XYItpbLFeHCyQYLMC4p2cVLfDCCBVUwggQ9oAMCAQICEEEDeNIm" +
                "Nll6Ftsmxr0QlIswDQYJKoZIhvcNAQEFBQAwgbQxCzAJBgNVBAYTAlVTMRcwFQYD" +
                "VQQKEw5WZXJpU2lnbiwgSW5jLjEfMB0GA1UECxMWVmVyaVNpZ24gVHJ1c3QgTmV0" +
                "d29yazE7MDkGA1UECxMyVGVybXMgb2YgdXNlIGF0IGh0dHBzOi8vd3d3LnZlcmlz" +
                "aWduLmNvbS9ycGEgKGMpMTAxLjAsBgNVBAMTJVZlcmlTaWduIENsYXNzIDMgQ29k" +
                "ZSBTaWduaW5nIDIwMTAgQ0EwHhcNMTQxMjExMDAwMDAwWhcNMTUxMjIyMjM1OTU5" +
                "WjCBhjELMAkGA1UEBhMCSlAxDzANBgNVBAgTBk1peWFnaTEYMBYGA1UEBxMPU2Vu" +
                "ZGFpIEl6dW1pLWt1MRcwFQYDVQQKFA5HcmFwZUNpdHkgaW5jLjEaMBgGA1UECxQR" +
                "VG9vbHMgRGV2ZWxvcG1lbnQxFzAVBgNVBAMUDkdyYXBlQ2l0eSBpbmMuMIIBIjAN" +
                "BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwQL2ymVbspWkCpEHpVUHtcmbz5rr" +
                "THvwdlY2a8COz96uanuluHwz0di4RVNGPwfhtpfEViriLvl7mQ2vuz6cZsnlR8zo" +
                "KV2pt5GxDjO9Fvqel+u1w4HB9g7HTCh5hB8jpXMtXOE9saNQMrqp0dkt/8Ry9Igq" +
                "9Fu7cgs4TeS67HTuBCRv76utIFTIkpdTydbxz4r72x9aodg9vwUXYhrNbGGZ8h0i" +
                "gM0rKOvev/AifeNB6Omp9qaIc2xT87bopLQRy8JLkIU4oNPq+92cCR6TeTItZ5/5" +
                "xr9xsWjvi9rBga2bDbDPD+FzCUA0hBoIDHP7kkdBndISDwstJn4LwThP7wIDAQAB" +
                "o4IBjTCCAYkwCQYDVR0TBAIwADAOBgNVHQ8BAf8EBAMCB4AwKwYDVR0fBCQwIjAg" +
                "oB6gHIYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcmwwZgYDVR0gBF8wXTBbBgtg" +
                "hkgBhvhFAQcXAzBMMCMGCCsGAQUFBwIBFhdodHRwczovL2Quc3ltY2IuY29tL2Nw" +
                "czAlBggrBgEFBQcCAjAZDBdodHRwczovL2Quc3ltY2IuY29tL3JwYTATBgNVHSUE" +
                "DDAKBggrBgEFBQcDAzBXBggrBgEFBQcBAQRLMEkwHwYIKwYBBQUHMAGGE2h0dHA6" +
                "Ly9zZi5zeW1jZC5jb20wJgYIKwYBBQUHMAKGGmh0dHA6Ly9zZi5zeW1jYi5jb20v" +
                "c2YuY3J0MB8GA1UdIwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMB0GA1UdDgQW" +
                "BBQAWvCtpdR4NfWEEqgsBQ74VhuOjjARBglghkgBhvhCAQEEBAMCBBAwFgYKKwYB" +
                "BAGCNwIBGwQIMAYBAQABAf8wDQYJKoZIhvcNAQEFBQADggEBAIjCmFo3jlvlWIqx" +
                "F8IDqFtV6oyE0ImYvriarF1i/DeCwXig4IOiIzqRaHLU2hR3Yulyv0+N8Ynnllfi" +
                "xmWqjF5+VOkeCdfww8m4qkMGyTtaSGIS7rl8HBv6D3BAcwx+BjSCMcgBDZkR/Y8n" +
                "pNNIVy+PbjCHvd2zKpyaPb3R+nAO0doXaMTmmr+1AE4ny8OQ3jrC3ioyEbqcik6B" +
                "z0qeDIst0Q7tXfgozU1v6w30mSpNZc2g2qU5/tCNgfCXDsq7tbeQgYr5/WQ/XGpM" +
                "GlfCwETmwuWe6M/4kCpXxoqUEkMpEjciGWsb0IlSaoU2GZnZ/lATmMC89B5d68uc" +
                "xiKomuAwggLEMIIBrKADAgECAgQO7u7uMA0GCSqGSIb3DQEBBQUAMCQxIjAgBgNV" +
                "BAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwHhcNMTYwNTEyMTMwMzAwWhcN" +
                "MTcwNTEyMTMwMzAwWjAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxv" +
                "cGVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjcAquUKuWZRUmCZu" +
                "nYBvVTw+izZX46iLEgt8NjbwUTtvtkcOfPl2CTd9WW55Vssw3EuaBPd6QfAmlOum" +
                "H90sxRqBXTGKZDk8hZTGJdoB9fkY25AfANe7NZ3Ehk2DOtTPYUH2ZacXaFsaxujH" +
                "Zs+i/yTGLvQ2NNFXD15gLaB/rn2r3UlLkK3b90XAuD2534ILZUjFFGKY8kdkJA7A" +
                "xrGuc3rxklGS0aWLgGVRdoU9WtwBTYEFF7e4VJ77AqRPXO1KxzQUPBwfqBdRDOxD" +
                "GaL0eKCMmN407s0QdaPznE1Y4/7AGC8c4/Yr7K0sNVkIbl+qZ3sGjywTEf+9AaxZ" +
                "VEZRsQIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQBdskraKDeNgYg/+ehIm4r+2/zI" +
                "oh/Gh6PJ6rWC2lIxTCUUELmvDl8pPWu5v93E1J7uEnp7Ol1iLOfcv1mHKiEXj9j5" +
                "QS9G5M5Wn7lUEOtJILwPwM2ZEHG4dAeI+vzXcJyvKiX6RPlGTyaZ61P6eMiIlC+9" +
                "pnH7QOx3niIhWlC+QkNPDlz2U9yR/6Xrm9Pj3JeL/C6rUM9MGfszbSzDgk3nzLU0" +
                "HxwQlpoJD18SXf5ARGXzeMjjQP7FG+wUOU/h1M9V4br7w6HHqvGR+YD6Yhxv47Qq" +
                "AZpPGxDAyp8eCXaIMA9QkrPlDmc/Cer2g6kKXFWa3NRaJxwT2idZuDhdMBB7";
        Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
                Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
                Microsoft.ApplicationInsights.WindowsCollectors.Session);
            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(CurrencyComparisonDemo), 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();
        }
    }
}
