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 LineMarkerSample
{
    /// <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 =
                "ACgBKAIoB4FMAGkAbgBlAE0AYQByAGsAZQByAFMAYQBtAHAAbABlAIFRlr2fatde" +
                "R5ngW231rAoQ3T0cPrpk6AfnvGmE4/rF85Wx82hDpdpFNmRx4/EVzmO0TeS8kiW9" +
                "qvo/ClKcx03HM6/QpkBAqcBEQFGTaGhjofaL4RGBfvNoeiRBcRwp2upXQC/v1ADO" +
                "evIDg33bZhph23gT3ujIvGD1ZwXoDz59eg/s+fmX6f6o2jvMqOjHjKNbM4kTqMOM" +
                "3E+70IwkLfN/6Xb2UZwaO+KdjLhlKKc5USbXWZZSYM+T+CdCBUTwJxTsFo47gri5" +
                "W7NCTvgJ2ygpZxSLHjrVbdlsYAw7XXUjfwgmJT4Lkr3JbCltHdpwXI3MsEqhOV/5" +
                "0eAws7hj0aTAUuSYaZ6OBr+SzE2QhsHqBCNYAXqGiNZjo1bYP2xKio1TG2pLq7m2" +
                "2m3YXTfwEWZBvQsGPZjTLNux96sXOdh+wVZS+uZwKW22V59+aTsl6P7EIVUMVBfg" +
                "4rjg5dNYkqBrY/D0FKFe6i6dmSqWuO3ZZydCaXAJLBswsRjgdIXodojrfppZ532S" +
                "HqhEa5jOvcZAJ2MERkqOXB0yLBP2nMXFUvabEob/W7eFi8Lk2p6AqBGTQBBuKi3i" +
                "t9qXQoVibS2OLF0WTtnq28vJGjlbJliIjz4X0IBm+0XVA19nA9wnov12RI59fYu7" +
                "qorUjLIQ7xW69HiDeV0hhjNIxIPzXxDKMIIFVTCCBD2gAwIBAgIQQQN40iY2WXoW" +
                "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" +
                "R0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNjA1MTIxMjI0MTlaFw0xNzA1" +
                "MTIxMjI0MTlaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIw" +
                "ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCMC0rxrmXjvzyi7qt3i4LA" +
                "6N52vCK+Wg3lDtQnrujxNiZtGcuK8T4B6a9GkjHer/NuPw4/iu3F5Xi8QmeLDdFC" +
                "T+Ev0bDOCLlCQcErB1eTBEtpx6HrSVEwNGwOG629tArruH2irt/UfTeWVT8MZJGe" +
                "CdjLaofmeyJX4scQFRMxED08jMGo8md8bWeWXp2QkrWMmbIIUlSFv0lf9lnSS7ul" +
                "6V0Oa44sAOOjsyVZhuhoU2+CdT+rWI6hCiHptMicFCPJL0knF1KARwARbmbbFPEK" +
                "YAIPMJh+rU9AFEcSOMbjkXbBXs1tSQbgHLMFxGl0EVfpCuEFbQFqSE7wukae38pZ" +
                "AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAF3T7dj/5mYza1GvwaiV2DnyWnGCSO/B" +
                "arWetHKIYryBsbJP8angoZycUKuBj8DylWWEuRMCk8D5AQnz98NMPEvJ1z1xp3Fh" +
                "avmrLnqe5iiig17ADIc7DNlg4lmT0tCXxi6+pqi2rnT8l95W66W66bj+YTHSWQjl" +
                "nLd5Xp3EOD61d+7xyviIiprtOFp/4ZppCWBA7BbQwptXtfioSWkKgrpf449h5b/y" +
                "pwPK68+3TeHjtEJ7rcyInmfeON64VmOYcO7ggzxPFQvo0RDI867uhBBxWbXS0WfA" +
                "oFskSn4Mjz/k7X8kBMvd8DO9NmRRtJowG8vgUv4hikX6dGAdUY6+gTY=";
        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(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();
        }
    }
}
