using FloatingBarChart.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 FloatingBarChart
{
    /// <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 =
                "ACgBKAIoB5BGAGwAbwBhAHQAaQBuAGcAQgBhAHIAQwBoAGEAcgB0AHz9svH84G7F" +
            "CXKzE9pLNz4RxwC7rBb2/HbFulY16l2hwmBrkz4AI6dJsl0cbEQ8iuZ7cDb7CKgB" +
            "26Geaql/6KfIBXfP2qUKQewpIpZJBqWRLjHO+cd+xqwilspSz2rn8XLlg8nO3qgS" +
            "glTeFxWBt3d9/DQykW97p9rSbVj5UUEXGc8xYXFTJQjc4btrkiKUnMKYHSqM4uI/" +
            "og/7S5Hhi8O1iZD1hw1jUO64zNq5oT1t7lXlqRtO1wb2JB3mKXlt2VDEZuHd1v9Z" +
            "jMb4e6GlZqItDDaAySTvBtGvwZDk+RXpfRRNhqwymzpbNN4Vgp5U7haFmrHZWNFh" +
            "DiZXYjoHKqB3kG1gUrs7lgy5UoX1MPTc+BdldJTtYBnI9mpDE2b8hQWkZ5yg5S4d" +
            "zRIXhWeP10q3DAVgfg25BJlowyAP/dr1yCA4R8VqmpN4LzFWwDXPhUHdGa9p05XX" +
            "Y8i3zzv472wuly1+QGrFaNOr9A9XFqeEvXLwe7LROCtsptzwZttAGeUFC+cWud2J" +
            "TwgJYfFZCx6xC9214xkEYE+e2xy6J3PzibFLf3tOlenY2ljN+QV0LSHtECdtMsHr" +
            "9yID3T6j9yXy0JsHqEN0wazUo7ItLx2eI9ZAIK26+nhTSopcwDLx77w4DkJh50Xc" +
            "bHexbHOm51cuLExHR/Hbx6ZVT+0SzsXeMIIFZDCCBEygAwIBAgIQIhCyF0sLEn+7" +
            "KAUuEbMlCjANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCVVMxFzAVBgNVBAoT" +
            "DlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3Jr" +
            "MTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24u" +
            "Y29tL3JwYSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNp" +
            "Z25pbmcgMjAxMCBDQTAeFw0xMzA5MjQwMDAwMDBaFw0xNjEwMjMyMzU5NTlaMIGn" +
            "MQswCQYDVQQGEwJVUzEVMBMGA1UECBMMUGVubnN5bHZhbmlhMRMwEQYDVQQHEwpQ" +
            "aXR0c2J1cmdoMRUwEwYDVQQKFAxDb21wb25lbnRPbmUxPjA8BgNVBAsTNURpZ2l0" +
            "YWwgSUQgQ2xhc3MgMyAtIE1pY3Jvc29mdCBTb2Z0d2FyZSBWYWxpZGF0aW9uIHYy" +
            "MRUwEwYDVQQDFAxDb21wb25lbnRPbmUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw" +
            "ggEKAoIBAQC5y6CaqUlVapyS82tOZUyHGpHL8pe3cQcWfnMJOAqpOvlGgun+WeS7" +
            "0Q9fgIYgEbpSICO7z4JAn/nPN4jlYkFsiblxSTJxmr2twGel/6NA2lKs2MxTls/z" +
            "KwMzib2DLa4/zU/ZvAVnovlJGNTVlMrYkmtCSDWLeeYvxc7cV7T+ytuy492WMMFJ" +
            "DSziieH/qpEdEEp8oGFEEMjAzi4Ob32JAy3VEJDa3rQU9iWesenZDXAqYn+XW2dN" +
            "TDhRBI2SZRnZ763p7jmH8OQjZaA0gkc7bUifPSbSTo0McqwUH9cpTl6Ilwj6cwFw" +
            "lNkhf3WF0oplTPKU9DWe6VDRtR/gM9pzAgMBAAGjggF7MIIBdzAJBgNVHRMEAjAA" +
            "MA4GA1UdDwEB/wQEAwIHgDBABgNVHR8EOTA3MDWgM6Axhi9odHRwOi8vY3NjMy0y" +
            "MDEwLWNybC52ZXJpc2lnbi5jb20vQ1NDMy0yMDEwLmNybDBEBgNVHSAEPTA7MDkG" +
            "C2CGSAGG+EUBBxcDMCowKAYIKwYBBQUHAgEWHGh0dHBzOi8vd3d3LnZlcmlzaWdu" +
            "LmNvbS9ycGEwEwYDVR0lBAwwCgYIKwYBBQUHAwMwcQYIKwYBBQUHAQEEZTBjMCQG" +
            "CCsGAQUFBzABhhhodHRwOi8vb2NzcC52ZXJpc2lnbi5jb20wOwYIKwYBBQUHMAKG" +
            "L2h0dHA6Ly9jc2MzLTIwMTAtYWlhLnZlcmlzaWduLmNvbS9DU0MzLTIwMTAuY2Vy" +
            "MB8GA1UdIwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMBEGCWCGSAGG+EIBAQQE" +
            "AwIEEDAWBgorBgEEAYI3AgEbBAgwBgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEA" +
            "Yc1WOc48GABY5iGtiUlm6nl0NY639qOQ6EWFoCP/uCxKSflNqPlQCOZCGEjRqeWI" +
            "6u170KLI7PwuqncKX03d24dpRBEeFwkc6aPuByvVscI9A/D9VKFJ+Ny45WzAfxs0" +
            "UYTatATfhE5Q9PgXo7KaFLLHeRkYRizTl5rQ1fvf2u4+4fbeSRDJPviW5crFXulK" +
            "XILaGPV4LmS7JuQzoUE9ECJYDiCtUEpf8IYihnTwXw+YzeP0h7BlVGz6Qvj8Y4ec" +
            "k/y0pvfjapPrczEEHKW033iyrPZC4LBuFKPX7IOcDpeBTeAgR6Ngi1xKra4st//6" +
            "6VDDrrVSpptWsB4YBqrLhTCCAsQwggGsoAMCAQICBA7u7u4wDQYJKoZIhvcNAQEF" +
            "BQAwJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjAeFw0xNzA4" +
            "MjgxMzIzNDlaFw0zNzA4MjgxMzIzNDlaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRl" +
            "cm5hbCBEZXZlbG9wZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC5" +
            "cTYZo0JgwKviLuAwajRUQpDWdLpr8G4R+Qyn4xm1Yfa9vD0FlC5Th5ENhnHKdwMa" +
            "w1Sl2pDG2nYDFMd2qOasRe+mQ28+Utr9LU8BVZMyiG8JcxycFDObPhaXWh82QvI2" +
            "NPAfuwS0m/HHjKK4C3K8X/FusgwbJhVW3rkpzw8+55Hk+98Z5u17N5pAE50Md1wO" +
            "l59J78F2Bnix64W5d3Rr1SbhY/+J1q26QA2Dh5bjOp2GlM7pMsAGL4OhyPmm+l0X" +
            "SMVOUVTbc8YLZWh4aijZmLY7QgBsOUNSr+Oq6xz15VM1jwryctQZ77BBH0Xtb4LQ" +
            "b2mrijBkb23sGaqoZvRbAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBACY9lDofW2Ym" +
            "yymUoQ/GLokP5LtNB3Q+vy2I2qr5GFlMsSx+qYdabdLnUjEBi3mL7edsmvcSZKVx" +
            "5JBuIp1os1fuAC8ImV87A3IEvLTk3ePJpkq1r+jvGJNzJ9gwmhSg8+9ATIML9GA9" +
            "Eans26FWb2krU6gCOTzpO9faiDL3DFryEp6+oVgXuN9b7eCaUpwqweaIgtgU8dtT" +
            "i6gfn9AH7PlbXF1HInIftYEHWNT4CAvoTfc6nOBOp9NdlJrMyoXjLHQYMIbz93aD" +
            "ieuOtUxw8mCQJ6uTdsYcaugrWVg7ostJqLqMSTM54GasHyvKGQdu3z8a24Gaj6rp" +
            "DFxvJiHvrh4=";
            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();
        }
    }
}
