using FlexChartCustomization.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 FlexChartCustomization
{
    /// <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 =
                "ADQBNAI0B5xGAGwAZQB4AEMAaABhAHIAdABDAHUAcwB0AG8AbQBpAHoAYQB0AGkA" +
            "bwBuAE0gIb7DXdlQ/gJHzk3sAZS0XCVxcBNr6yCEFYz25FUC5cywdgivN4FFBcbT" +
            "rPry8pT3slI+FFuCk6gpR2k+6Zpy51lAEj6kIT/WJz4YGoo2iUDlaYmzzkOs8SwI" +
            "dJV5cPXpm3LWPc2J/h/jLJGkW8687/PUomyg67XAGaFK+EzbNDYDZvIkYbBnfMZU" +
            "qtQ949RwpiLdXot1B2v6Ro7PFdjhh4qDaXgztGTjjhX8/OSO4Wh6TCQ5NoxgqJP2" +
            "xSxGF9058NTFfplt0ZrwAVDK8Ea04YE8Mk9lYxNyT12BhO93ulnGzC2IbsX8c1Zv" +
            "82ylO2W0DdB48EQpoQaEHseleLCqKtOQkVp2YJbI7CAiXCGVuLlCn4AXlqzhLjTr" +
            "Zz0TPEySDyxotS36VEjzCOi5Omt9PpjhzyM/E+Xdse+pXajllNz+xQo4Bzo9wFil" +
            "fey6/+cfYnssCEGFFJZkgH29n7DJKXTPczuRI6COnxqz0K1IUmRilJ+Fg3smR0yc" +
            "mDe0e/6BhwkI4192LUN1cWfb6N5vFNJ3A2QQjsAvtWZ0r2U+p0JSnFNdV35ZeVFc" +
            "y6GGMjzlz5fTRfzLcak2bJPD5d91wm9v04K+kPBT8ZrX6B6iNnL0NwvM8KY1Uyo4" +
            "eJ4uFgyaz475CeDOrMN+S1d5jfmP3B2MZhWlK7HLINbucMmXMIIFZDCCBEygAwIB" +
            "AgIQIhCyF0sLEn+7KAUuEbMlCjANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMC" +
            "VVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBU" +
            "cnVzdCBOZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93" +
            "d3cudmVyaXNpZ24uY29tL3JwYSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xh" +
            "c3MgMyBDb2RlIFNpZ25pbmcgMjAxMCBDQTAeFw0xMzA5MjQwMDAwMDBaFw0xNjEw" +
            "MjMyMzU5NTlaMIGnMQswCQYDVQQGEwJVUzEVMBMGA1UECBMMUGVubnN5bHZhbmlh" +
            "MRMwEQYDVQQHEwpQaXR0c2J1cmdoMRUwEwYDVQQKFAxDb21wb25lbnRPbmUxPjA8" +
            "BgNVBAsTNURpZ2l0YWwgSUQgQ2xhc3MgMyAtIE1pY3Jvc29mdCBTb2Z0d2FyZSBW" +
            "YWxpZGF0aW9uIHYyMRUwEwYDVQQDFAxDb21wb25lbnRPbmUwggEiMA0GCSqGSIb3" +
            "DQEBAQUAA4IBDwAwggEKAoIBAQC5y6CaqUlVapyS82tOZUyHGpHL8pe3cQcWfnMJ" +
            "OAqpOvlGgun+WeS70Q9fgIYgEbpSICO7z4JAn/nPN4jlYkFsiblxSTJxmr2twGel" +
            "/6NA2lKs2MxTls/zKwMzib2DLa4/zU/ZvAVnovlJGNTVlMrYkmtCSDWLeeYvxc7c" +
            "V7T+ytuy492WMMFJDSziieH/qpEdEEp8oGFEEMjAzi4Ob32JAy3VEJDa3rQU9iWe" +
            "senZDXAqYn+XW2dNTDhRBI2SZRnZ763p7jmH8OQjZaA0gkc7bUifPSbSTo0McqwU" +
            "H9cpTl6Ilwj6cwFwlNkhf3WF0oplTPKU9DWe6VDRtR/gM9pzAgMBAAGjggF7MIIB" +
            "dzAJBgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDBABgNVHR8EOTA3MDWgM6Axhi9o" +
            "dHRwOi8vY3NjMy0yMDEwLWNybC52ZXJpc2lnbi5jb20vQ1NDMy0yMDEwLmNybDBE" +
            "BgNVHSAEPTA7MDkGC2CGSAGG+EUBBxcDMCowKAYIKwYBBQUHAgEWHGh0dHBzOi8v" +
            "d3d3LnZlcmlzaWduLmNvbS9ycGEwEwYDVR0lBAwwCgYIKwYBBQUHAwMwcQYIKwYB" +
            "BQUHAQEEZTBjMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC52ZXJpc2lnbi5jb20w" +
            "OwYIKwYBBQUHMAKGL2h0dHA6Ly9jc2MzLTIwMTAtYWlhLnZlcmlzaWduLmNvbS9D" +
            "U0MzLTIwMTAuY2VyMB8GA1UdIwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMBEG" +
            "CWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3AgEbBAgwBgEBAAEB/zANBgkqhkiG" +
            "9w0BAQUFAAOCAQEAYc1WOc48GABY5iGtiUlm6nl0NY639qOQ6EWFoCP/uCxKSflN" +
            "qPlQCOZCGEjRqeWI6u170KLI7PwuqncKX03d24dpRBEeFwkc6aPuByvVscI9A/D9" +
            "VKFJ+Ny45WzAfxs0UYTatATfhE5Q9PgXo7KaFLLHeRkYRizTl5rQ1fvf2u4+4fbe" +
            "SRDJPviW5crFXulKXILaGPV4LmS7JuQzoUE9ECJYDiCtUEpf8IYihnTwXw+YzeP0" +
            "h7BlVGz6Qvj8Y4eck/y0pvfjapPrczEEHKW033iyrPZC4LBuFKPX7IOcDpeBTeAg" +
            "R6Ngi1xKra4st//66VDDrrVSpptWsB4YBqrLhTCCAsQwggGsoAMCAQICBA7u7u4w" +
            "DQYJKoZIhvcNAQEFBQAwJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVs" +
            "b3BlcjAeFw0xNzA1MjUxOTAxMTFaFw0zNzA1MjUxOTAxMTFaMCQxIjAgBgNVBAMM" +
            "GUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IB" +
            "DwAwggEKAoIBAQCmksGKAD6fkX1r33KlOA/2stcTOh5BvmBVO0nfShq9EfQ9k4u6" +
            "s6MIGQ3DIY5DOdRV6mkcnIoe3swjj0951qiLyOOCs27bseEO9cRQyU9hNUq8yQ8S" +
            "5kCBp/PL1N4CqRdV3AUQbwUMvTxLJi1C+6v2wVJT0S1A7ZEFehwXEjFcRIA1nnIz" +
            "UxhAtQB9EHVV6tJjxmHwFgcGoprl5jlfbMgsl7vjvhJk6TL7s21IQEoWeEIjX29M" +
            "k3CnZhviU7FW70HE/N1xCOz2FXxFPa6SvPzFLlt+iYgiwaNstAuwlc0FA2mTvhVu" +
            "rWeDdGeddX0bvlHFAFMrIiPKUeeYaGPmvlIFAgMBAAEwDQYJKoZIhvcNAQEFBQAD" +
            "ggEBAAXoS0vjFxcPnlyhFK5mDpkfdQfqUHzA3H/2tlz5iTeqEnHzE+p+f0f2/dY5" +
            "8zvpqCURlCD4c+fNuXQMm0FcSIr9U+VP1o088hwPjvx/sJzKuKuOvSXxIqnXOZth" +
            "/yLgm3ZxIr92zHoScTz71yuPs7IsLsiRAqjBy6kDET6tWNi1PAh7Q7ptsMnwoPuT" +
            "o1Q67yPCo0kkbGfibRDSwFYBsqDGUK/eoKFaB3iESOeRjt8emQXafmtiCJgw6uaq" +
            "Am/2tE/WFfRQFZMffQwdk3m9zoZXup5xgYfyUgtAHTgZkwQieZiQzbLVAwx17RjN" +
            "36A95QN0rl74G3y5bMn1UMP+kxI=";
            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();
        }
    }
}
