using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using System.Collections;
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 ControlExplorer
{
    /// <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 =
            "ACYBJgImB39DAG8AbgB0AHIAbwBsAEUAeABwAGwAbwByAGUAcgCE9rBxFg4AIMUA" +
            "YWu9UsN7VAFskVQ945YZG+TP6c4+Zl1vg1brcG0uWJ5jpC/klltR/C0hwLZ9im6s" +
            "6smQaUBTqrZy4Q+rup+E1Qvryi9k97qFInj0IYTi6sawC24USub6HsEST1xZsllw" +
            "9obGSo8OLMiRB0RnnDAe9zkB4nvp1sBUx3eKlIaxWqkK8ayEhnp1WDdco31SWR2q" +
            "zn+C14+oSJJ8XZ6MABvEAxU2+2iSu0t/eyx0bOaknFElWjjVHf11NDo0B4KSI0yM" +
            "BhJqR70cJfgZYIUKEKNSVpa/d0Dvm1nNozkIo8eE5pCVGaT95CRrtdCRpj1dtf36" +
            "rmpBJH6LtiYnpV7e0KdGxUdzZPmvh+6pJWe2YBbT85BExoNFaZz/4CWdDZNnNnuy" +
            "g6TFaXa7CLiH2Naiy1Xx1T80WjIxbKRMF9cVjMSvkKFYwVksWw4bMFq9b/fkBcLc" +
            "uGiYd3UreJremFJl3Gwz21qxvzMSXMcUVxZSzs+w4Gf2Nhc0nN0eWG9OWUW3KdfO" +
            "VcSnAuaPTuiQwZDsBlN+hKDs0OX8EyotVFROT/5h6t+bewzfA0ybSoo1o5kIGO2H" +
            "b9JnvXlUhEhdA9QN0RNGpCYYPE0eq+D1OYD3NS2LYda0xELvzg2bAs3XpPlql2Kk" +
            "RF77H96FvXK3MDnUdJTcda3+8GDqGjCCBVUwggQ9oAMCAQICEEEDeNImNll6Ftsm" +
            "xr0QlIswDQYJKoZIhvcNAQEFBQAwgbQxCzAJBgNVBAYTAlVTMRcwFQYDVQQKEw5W" +
            "ZXJpU2lnbiwgSW5jLjEfMB0GA1UECxMWVmVyaVNpZ24gVHJ1c3QgTmV0d29yazE7" +
            "MDkGA1UECxMyVGVybXMgb2YgdXNlIGF0IGh0dHBzOi8vd3d3LnZlcmlzaWduLmNv" +
            "bS9ycGEgKGMpMTAxLjAsBgNVBAMTJVZlcmlTaWduIENsYXNzIDMgQ29kZSBTaWdu" +
            "aW5nIDIwMTAgQ0EwHhcNMTQxMjExMDAwMDAwWhcNMTUxMjIyMjM1OTU5WjCBhjEL" +
            "MAkGA1UEBhMCSlAxDzANBgNVBAgTBk1peWFnaTEYMBYGA1UEBxMPU2VuZGFpIEl6" +
            "dW1pLWt1MRcwFQYDVQQKFA5HcmFwZUNpdHkgaW5jLjEaMBgGA1UECxQRVG9vbHMg" +
            "RGV2ZWxvcG1lbnQxFzAVBgNVBAMUDkdyYXBlQ2l0eSBpbmMuMIIBIjANBgkqhkiG" +
            "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwQL2ymVbspWkCpEHpVUHtcmbz5rrTHvwdlY2" +
            "a8COz96uanuluHwz0di4RVNGPwfhtpfEViriLvl7mQ2vuz6cZsnlR8zoKV2pt5Gx" +
            "DjO9Fvqel+u1w4HB9g7HTCh5hB8jpXMtXOE9saNQMrqp0dkt/8Ry9Igq9Fu7cgs4" +
            "TeS67HTuBCRv76utIFTIkpdTydbxz4r72x9aodg9vwUXYhrNbGGZ8h0igM0rKOve" +
            "v/AifeNB6Omp9qaIc2xT87bopLQRy8JLkIU4oNPq+92cCR6TeTItZ5/5xr9xsWjv" +
            "i9rBga2bDbDPD+FzCUA0hBoIDHP7kkdBndISDwstJn4LwThP7wIDAQABo4IBjTCC" +
            "AYkwCQYDVR0TBAIwADAOBgNVHQ8BAf8EBAMCB4AwKwYDVR0fBCQwIjAgoB6gHIYa" +
            "aHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcmwwZgYDVR0gBF8wXTBbBgtghkgBhvhF" +
            "AQcXAzBMMCMGCCsGAQUFBwIBFhdodHRwczovL2Quc3ltY2IuY29tL2NwczAlBggr" +
            "BgEFBQcCAjAZDBdodHRwczovL2Quc3ltY2IuY29tL3JwYTATBgNVHSUEDDAKBggr" +
            "BgEFBQcDAzBXBggrBgEFBQcBAQRLMEkwHwYIKwYBBQUHMAGGE2h0dHA6Ly9zZi5z" +
            "eW1jZC5jb20wJgYIKwYBBQUHMAKGGmh0dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3J0" +
            "MB8GA1UdIwQYMBaAFM+Zqep7JvRLyY6P1/AFJu/j0qedMB0GA1UdDgQWBBQAWvCt" +
            "pdR4NfWEEqgsBQ74VhuOjjARBglghkgBhvhCAQEEBAMCBBAwFgYKKwYBBAGCNwIB" +
            "GwQIMAYBAQABAf8wDQYJKoZIhvcNAQEFBQADggEBAIjCmFo3jlvlWIqxF8IDqFtV" +
            "6oyE0ImYvriarF1i/DeCwXig4IOiIzqRaHLU2hR3Yulyv0+N8YnnllfixmWqjF5+" +
            "VOkeCdfww8m4qkMGyTtaSGIS7rl8HBv6D3BAcwx+BjSCMcgBDZkR/Y8npNNIVy+P" +
            "bjCHvd2zKpyaPb3R+nAO0doXaMTmmr+1AE4ny8OQ3jrC3ioyEbqcik6Bz0qeDIst" +
            "0Q7tXfgozU1v6w30mSpNZc2g2qU5/tCNgfCXDsq7tbeQgYr5/WQ/XGpMGlfCwETm" +
            "wuWe6M/4kCpXxoqUEkMpEjciGWsb0IlSaoU2GZnZ/lATmMC89B5d68ucxiKomuAw" +
            "ggLEMIIBrKADAgECAgQO7u7uMA0GCSqGSIb3DQEBBQUAMCQxIjAgBgNVBAMMGUdD" +
            "LVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwHhcNMTYwODE5MTM0NDUxWhcNMTcwODE5" +
            "MTM0NDUxWjAkMSIwIAYDVQQDDBlHQy1VV1AgSW50ZXJuYWwgRGV2ZWxvcGVyMIIB" +
            "IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmzwMgM+mJYIfERdDUc6n1KqF" +
            "GabJDCPWab40ynzIvJ390gJrJbk5j7JMNB9i9wuV3RYYK/PO7amWm82LScBkmTtJ" +
            "cCz5Zhxsg6XmDXrzgEA9nfMup3XRrEqg0lho/Cb0DTP47VVnvuBTtWY0NasC46xr" +
            "fA51k4O428U0oDePzZ6GESoMHERXBxxavDHB6zSojRap1kzlMd5l6pk55qCNiwLV" +
            "ARgk31tUumG7FiZQbjHDeDLhwFHAL0N9Ah1HVU/cHk7WXVlEzyk5G7DmZ5y68Zf5" +
            "rOal/AJDIkGGP5Nd4KBb8CF39esw15jG4LYE4EYnLW83lre7uGGRJ4yxS/TSZwID" +
            "AQABMA0GCSqGSIb3DQEBBQUAA4IBAQAThrRIALG4QT4b4e8CXl9ZIykKw0aX5H9t" +
            "XPJSOwYvgUUfh1eSEXUoxP2AB33LUfmoRH3+zR5K0KCzDpCvYN1LDoU3tEI5kbud" +
            "F3fi2whD0Fdx/2lQ+TANlK+BpMxI+amXBrz18G3nlYgnTmOWLZVqIVs4m7ox98UO" +
            "yHXE14GawjrdDEc5L8rdcv3Uk8kNKMmvhott8DiWjcXllS+gZpALuMv0aAh4fjEL" +
            "osFyiNw2Pttz6FokMjDTrIMVSYVYpXl9AbcY6u2HCWPkvU20AkCKKQY0LmvCskbW" +
            "pBZHc78+7e5lAxJy56yPkPegAKtTPnrt68R5lbTxUp+CPzAWANnn";
            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 e)
        {
            HomePage homePage = Window.Current.Content as HomePage;
            var viewModel = MainViewModel.Instance;
            if (homePage == null)
            {
                homePage = new HomePage();
                await viewModel.Load();
                homePage.DefaultViewModel["Groups"] = viewModel.Groups;
            }

            Window.Current.Content = homePage;
            if (homePage.AppFrame.Content == null)
            {
                homePage.AppFrame.Navigate(typeof(ControlsPage), new object[] { viewModel, homePage.NavigationHelper });
            }

            if (!PlatformUtils.IsWindowsPhoneDevice())
            {
                Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.FullScreen;
            }

            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(Strings.InitializationException + 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();
        }
    }
}
