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 FlexChartMvvmDemo
{
    /// <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 =
                "ACoBKgIqB4NGAGwAZQB4AEMAaABhAHIAdABNAHYAdgBtAEQAZQBtAG8AlzII3WCM" +
                "W2DUpG1X+UuNZ/a4uYkcgW8oxvlw4TKq9fXG+RvQhDsUVNk3wgWkpQyKJtKJ5OX1" +
                "smbVthsFegVL20cYqiCqsEStPG67+5OovnsW+HOPPLjEPn7sSjaFcH1bHQTFTQq2" +
                "Or432IrxRZVNs4iYEuoEOm4VO3Rv9rKOgUxQYkP4pQ/sd+KRjLzWy/96Oju2tliJ" +
                "WibTKSssRjdlQxbZk+Tcg/yNWB1bhsp1huRzPqnoIeZCaSSjpyGwJQnC4LsW0aYf" +
                "GQdcA3uqIaOE48G+pnu+lYSJUB/T46+kMJBYdCwm7NfCuWCtmQ6HE8cmYzuEkUB6" +
                "6Vi8KKccnuwXw1WNfQpGK09/+gZSTi8hAKj6s+DfLE1sNWoZGKHzSt8eoUQyMZC/" +
                "tj+Jfurqk4J/fGRnNntNSeZFZQa4twLrbgl+qc9RlcE89D5+3nZ6sDM/eDmg4/Vk" +
                "sg76BInXHheE7a81b0renZN8VDNgumrYQsgbjHYETPw/b9TDS+bn8YC6IZgMJzI5" +
                "izCSoZxr0AelvGmdjDdDU4IfAvj8hIVXWsgXTymkIo6gkBKpyBizXvfcwkYg5Aiq" +
                "LABOs897Z3jJ3K/hRL6c+4570S9/ASRtqkBdVhC4QHdq5KHsZnm2o6dZHcCQTm+M" +
                "U4Gbzz3n08NyVP39i6v2iQakENTZt5q4m6UwggVVMIIEPaADAgECAhBBA3jSJjZZ" +
                "ehbbJsa9EJSLMA0GCSqGSIb3DQEBBQUAMIG0MQswCQYDVQQGEwJVUzEXMBUGA1UE" +
                "ChMOVmVyaVNpZ24sIEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdv" +
                "cmsxOzA5BgNVBAsTMlRlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy52ZXJpc2ln" +
                "bi5jb20vcnBhIChjKTEwMS4wLAYDVQQDEyVWZXJpU2lnbiBDbGFzcyAzIENvZGUg" +
                "U2lnbmluZyAyMDEwIENBMB4XDTE0MTIxMTAwMDAwMFoXDTE1MTIyMjIzNTk1OVow" +
                "gYYxCzAJBgNVBAYTAkpQMQ8wDQYDVQQIEwZNaXlhZ2kxGDAWBgNVBAcTD1NlbmRh" +
                "aSBJenVtaS1rdTEXMBUGA1UEChQOR3JhcGVDaXR5IGluYy4xGjAYBgNVBAsUEVRv" +
                "b2xzIERldmVsb3BtZW50MRcwFQYDVQQDFA5HcmFwZUNpdHkgaW5jLjCCASIwDQYJ" +
                "KoZIhvcNAQEBBQADggEPADCCAQoCggEBAMEC9splW7KVpAqRB6VVB7XJm8+a60x7" +
                "8HZWNmvAjs/ermp7pbh8M9HYuEVTRj8H4baXxFYq4i75e5kNr7s+nGbJ5UfM6Cld" +
                "qbeRsQ4zvRb6npfrtcOBwfYOx0woeYQfI6VzLVzhPbGjUDK6qdHZLf/EcvSIKvRb" +
                "u3ILOE3kuux07gQkb++rrSBUyJKXU8nW8c+K+9sfWqHYPb8FF2IazWxhmfIdIoDN" +
                "Kyjr3r/wIn3jQejpqfamiHNsU/O26KS0EcvCS5CFOKDT6vvdnAkek3kyLWef+ca/" +
                "cbFo74vawYGtmw2wzw/hcwlANIQaCAxz+5JHQZ3SEg8LLSZ+C8E4T+8CAwEAAaOC" +
                "AY0wggGJMAkGA1UdEwQCMAAwDgYDVR0PAQH/BAQDAgeAMCsGA1UdHwQkMCIwIKAe" +
                "oByGGmh0dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3JsMGYGA1UdIARfMF0wWwYLYIZI" +
                "AYb4RQEHFwMwTDAjBggrBgEFBQcCARYXaHR0cHM6Ly9kLnN5bWNiLmNvbS9jcHMw" +
                "JQYIKwYBBQUHAgIwGQwXaHR0cHM6Ly9kLnN5bWNiLmNvbS9ycGEwEwYDVR0lBAww" +
                "CgYIKwYBBQUHAwMwVwYIKwYBBQUHAQEESzBJMB8GCCsGAQUFBzABhhNodHRwOi8v" +
                "c2Yuc3ltY2QuY29tMCYGCCsGAQUFBzAChhpodHRwOi8vc2Yuc3ltY2IuY29tL3Nm" +
                "LmNydDAfBgNVHSMEGDAWgBTPmanqeyb0S8mOj9fwBSbv49KnnTAdBgNVHQ4EFgQU" +
                "AFrwraXUeDX1hBKoLAUO+FYbjo4wEQYJYIZIAYb4QgEBBAQDAgQQMBYGCisGAQQB" +
                "gjcCARsECDAGAQEAAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQCIwphaN45b5ViKsRfC" +
                "A6hbVeqMhNCJmL64mqxdYvw3gsF4oOCDoiM6kWhy1NoUd2Lpcr9PjfGJ55ZX4sZl" +
                "qoxeflTpHgnX8MPJuKpDBsk7WkhiEu65fBwb+g9wQHMMfgY0gjHIAQ2ZEf2PJ6TT" +
                "SFcvj24wh73dsyqcmj290fpwDtHaF2jE5pq/tQBOJ8vDkN46wt4qMhG6nIpOgc9K" +
                "ngyLLdEO7V34KM1Nb+sN9JkqTWXNoNqlOf7QjYHwlw7Ku7W3kIGK+f1kP1xqTBpX" +
                "wsBE5sLlnujP+JAqV8aKlBJDKRI3IhlrG9CJUmqFNhmZ2f5QE5jAvPQeXevLnMYi" +
                "qJrgMIICxDCCAaygAwIBAgIEDu7u7jANBgkqhkiG9w0BAQUFADAkMSIwIAYDVQQD" +
                "DBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjEyMjQxOVoXDTE3" +
                "MDUxMjEyMjQxOVowJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3Bl" +
                "cjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ2LUTEScTFHN8qAihpi" +
                "2cU2SgSioYCxxaF2cbibh9bSgMTSkK78QwTZP7JoatkPOW3O9CZRn3zbyuFM5aSI" +
                "/N6tgdFJnp5sn46sPa7IDkvexhG6sPFSJUj4unwIFK5EaTZeRuRMOiJg5qYFCoG2" +
                "jVPstXD0xOpUYfwIDg6HcGOWsKG46VHq2wYmvAhN0yXGRrBll5vHlzwXObxFof0x" +
                "JK+SkUqmQrs0TtvW29KKpcT4pLJfBGhcJG1SmjFG+4L2Mw7BV0VDMwUEpCOz2ICh" +
                "Up0kowKzjK5hHSJiOuaFbT/l8Rt9xdZ4MDuxGndZPn284DQWrnCHSGIMex3PeRxu" +
                "lnMCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAA90j4/CbbMi8b+tI/QvVilYPuPcj" +
                "wJJnQ5pfCwr8TX5FumH5q2z3PyLTN0hBFUytdAHS2MlEtNmaOOBzuzBa/k4F9I8O" +
                "sYa6HPqz8/JLNgdvR0ZAU8lz6dF3spiKsm1Rga+yb10XKZJ3gGihNslCz7r/VGEJ" +
                "0/iy/RbI2HmnJS/xSRyT/lqwZAixZK/fE/SxE8lN6udY58GJJnMFyKYqwJOSYcqm" +
                "Ftjajz0BnDHMVs8uaNTQ2b9SPTldT6OmlxArlydafEM9nokAX90hOg8A6GE2h8va" +
                "DBxXRVqeZv9tf2enNVw+uOG6OUV/GLvxeFv5tEt/POOFB7m3hRX3PLch/A==";
            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);

                // Attach view model
                rootFrame.DataContext = new ViewModel.OrdersViewModel();
            }
            // 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();
        }
    }
}
