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 Binding
{
    /// <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=
                "ABYBFgIWB29CAGkAbgBkAGkAbgBnAElqOmZDFypWI7ch2TIYx/oPuYUMRwdNrlaL" +
                "wYVbSLPYry6QGhzBIjFf/Kmx0m5qR3bYfHoHI3wq4TlcjanXM6Ugl6SGErz5iwov" +
                "mS6Zh75FVrtwfIe/vFBHwfwy9qPAhyEH+h4OdhTc4belMmaR0xVWaHhgPl5mzSQC" +
                "89sqraEW3Hz/KQo5a3WdXdQY6gYUHKK/uaBJ5eNTseulRhCM2rqOfVmfLu4evnp0" +
                "YpAlJlASLI9AK7vr+owH9vfCZ+y0o/weSjLg04qv24YqHts97RwLR0GTNCxSumMf" +
                "YARNuPy/eEfAyZIcPTVemCCuhxpquLAQNW3iIeTm9EiVyybGkxM89GOFj3oDqr0I" +
                "2kk8PbPMHeyYivw3DksliVzWXcy9iMHqqg1Orld6jDvIHYWWrDo31ttdHNMaBuc+" +
                "ycfMHZYXd6t25AeM+VmdKSexXF7uDbvnRmGX1g4PF6gRoT9XPmGGOd+8ExB7i+y/" +
                "j/BK+2pWhJ4AoowacBo7Q3tqMI1XWY3MGN+vr+cviQuCDnBaEdBN+JqTrMUXONx+" +
                "OazXIlgylKSP8LUn6igYwVZZDgmzCg2A6ZoBqsJQatLrBQj6TkKMhOOik5crtQtI" +
                "s9IhT2XeYD4V9q6i2y8o5a3JjgDRjMNyYQkXmJFvy23kuAX3gFHiOw6D4LBqTI84" +
                "ieTdUBcqMIIFVTCCBD2gAwIBAgIQQQN40iY2WXoW2ybGvRCUizANBgkqhkiG9w0B" +
                "AQUFADCBtDELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8w" +
                "HQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBv" +
                "ZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JwYSAoYykxMDEuMCwG" +
                "A1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNpZ25pbmcgMjAxMCBDQTAeFw0x" +
                "NDEyMTEwMDAwMDBaFw0xNTEyMjIyMzU5NTlaMIGGMQswCQYDVQQGEwJKUDEPMA0G" +
                "A1UECBMGTWl5YWdpMRgwFgYDVQQHEw9TZW5kYWkgSXp1bWkta3UxFzAVBgNVBAoU" +
                "DkdyYXBlQ2l0eSBpbmMuMRowGAYDVQQLFBFUb29scyBEZXZlbG9wbWVudDEXMBUG" +
                "A1UEAxQOR3JhcGVDaXR5IGluYy4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" +
                "AoIBAQDBAvbKZVuylaQKkQelVQe1yZvPmutMe/B2VjZrwI7P3q5qe6W4fDPR2LhF" +
                "U0Y/B+G2l8RWKuIu+XuZDa+7PpxmyeVHzOgpXam3kbEOM70W+p6X67XDgcH2DsdM" +
                "KHmEHyOlcy1c4T2xo1AyuqnR2S3/xHL0iCr0W7tyCzhN5LrsdO4EJG/vq60gVMiS" +
                "l1PJ1vHPivvbH1qh2D2/BRdiGs1sYZnyHSKAzSso696/8CJ940Ho6an2pohzbFPz" +
                "tuiktBHLwkuQhTig0+r73ZwJHpN5Mi1nn/nGv3GxaO+L2sGBrZsNsM8P4XMJQDSE" +
                "GggMc/uSR0Gd0hIPCy0mfgvBOE/vAgMBAAGjggGNMIIBiTAJBgNVHRMEAjAAMA4G" +
                "A1UdDwEB/wQEAwIHgDArBgNVHR8EJDAiMCCgHqAchhpodHRwOi8vc2Yuc3ltY2Iu" +
                "Y29tL3NmLmNybDBmBgNVHSAEXzBdMFsGC2CGSAGG+EUBBxcDMEwwIwYIKwYBBQUH" +
                "AgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUGCCsGAQUFBwICMBkMF2h0dHBz" +
                "Oi8vZC5zeW1jYi5jb20vcnBhMBMGA1UdJQQMMAoGCCsGAQUFBwMDMFcGCCsGAQUF" +
                "BwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3NmLnN5bWNkLmNvbTAmBggrBgEF" +
                "BQcwAoYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcnQwHwYDVR0jBBgwFoAUz5mp" +
                "6nsm9EvJjo/X8AUm7+PSp50wHQYDVR0OBBYEFABa8K2l1Hg19YQSqCwFDvhWG46O" +
                "MBEGCWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3AgEbBAgwBgEBAAEB/zANBgkq" +
                "hkiG9w0BAQUFAAOCAQEAiMKYWjeOW+VYirEXwgOoW1XqjITQiZi+uJqsXWL8N4LB" +
                "eKDgg6IjOpFoctTaFHdi6XK/T43xieeWV+LGZaqMXn5U6R4J1/DDybiqQwbJO1pI" +
                "YhLuuXwcG/oPcEBzDH4GNIIxyAENmRH9jyek00hXL49uMIe93bMqnJo9vdH6cA7R" +
                "2hdoxOaav7UATifLw5DeOsLeKjIRupyKToHPSp4Miy3RDu1d+CjNTW/rDfSZKk1l" +
                "zaDapTn+0I2B8JcOyru1t5CBivn9ZD9cakwaV8LARObC5Z7oz/iQKlfGipQSQykS" +
                "NyIZaxvQiVJqhTYZmdn+UBOYwLz0Hl3ry5zGIqia4DCCAsQwggGsoAMCAQICBA7u" +
                "7u4wDQYJKoZIhvcNAQEFBQAwJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERl" +
                "dmVsb3BlcjAeFw0xNjA1MTIxNDUyMTJaFw0xNzA1MTIxNDUyMTJaMCQxIjAgBgNV" +
                "BAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwggEiMA0GCSqGSIb3DQEBAQUA" +
                "A4IBDwAwggEKAoIBAQCBskyyWQbN5Zir5xW1xtYNACFpXdDTEyt2ZHconVKZaUF3" +
                "eWQgQlmuiLKKygogfGnodbjxRoihjegLdWVvfwDYS5SATN2tmczFiSwfBMP6cNGX" +
                "3EFmiMH/SgZbi8eI9CqqFe2mJEuKYz/6EAp3NwqszUnUi8YLRRqbWWDH/Kw8IGh0" +
                "1eHjiSAqwsCm+LNdYfBpaM/js0GJuIndcDWsUbDyOVhuEHvt3hWmkiAm/r1FmrCu" +
                "wmd54bpnELBGktz+Pa4qrbTqZOdT7LEF9bKPpsC+WrvAFNEfuvhCR12RVR9UimTG" +
                "vwygekVn8OnpYXG+gLCVi8WFi9lDwJEAL7H7XetlAgMBAAEwDQYJKoZIhvcNAQEF" +
                "BQADggEBAArew82owPGx6r3KbD5HmCLNYDoSLpcl7rKbSx7VDcQHxgIxVtcn7cTM" +
                "aSvPB9FvEnWTuxYQB9FNyreCFHsXyEIn8lH7f7qe2T2ghhuip1eP8pzBFPVwvA9m" +
                "33HXdpnZALV3kkn6qrRb7XnqkivVBlKM9k8U4OSLSnJh2TFGih0N1VW66Vit3uE1" +
                "3Ct2ekTiwN1KX2meEIkHrvUxaeONwm1/KEWRc4E1bHwdYMni8UDhYgn37gFtRnYo" +
                "x3mPT2hyDwq96eWQFKNBcod3pWKic/5fMpOcEsgAyoVsVr73Hhxam32FnpJLfPWF" +
                "tZa+EgGrERcKXcHn+iEx2GARi/bZtKI=";
            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)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            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 (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();
        }
    }
}
