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 FlexPdfViewer
{
    /// <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 =
                "ACIBIgIiB3tGAGwAZQB4AFAAZABmAFYAaQBlAHcAZQByAEy+mT5Eagtddq+jflaZ" +
                "NJ4AeqPtfRy2rKvFbOtn5VBDQGC6WYNl/V8MS4LVsipDNtBZP7uNvNaTNijYspmg" +
                "Tk/FFUdWTNQX1bKpbM6rsEaNFhVAXTdKlCJ0UVLdobW+agop2JkSaiBJrbvcKmS/" +
                "RQRN9ohl/HILHh4vDTOagusQwnQCckbM2nzdcfxYYay96G7iqAdNsDF4+aZJLhS2" +
                "BqJSnM4BKb22/fg9nWWDz7UTWNYYoPdieqG2Wek18FhA7XRjvpI5eHf033hwIk8Q" +
                "2dtsPPi87u0tPo/fucyyto8Ck7ms6oMxSSp8p4awzlhfRVHegRD29NBAccSX/IuB" +
                "sHUtBQ2UVRmcPbVuREtTNeS72FOHhzqUbSjwqmIOHjUo5FDJ47V6AvXeBv8y7K37" +
                "uEWUrqPbV7G6rY/ADygm0EGiTluTscXYilIeGH4343Ja/XS1Fbv3kmq01KJJ88xq" +
                "IDGmLNZk9BCazj96qgJe6cutiqmZ6YLR0pOcugkXTMU9Aw838MHOh29d+dlm+BrR" +
                "t+BrwjfKwWFd54jAZc0LVcLX1UG8QpHqxT/7Hd0vzpOMvNN8mOS5fnlyv5/gQf0T" +
                "TOuMNq4KXljyKDATYrwQSmoRTnIFY2fFebhA6CZjcG/CBxAPKq7mU5i2pE+jKy9/" +
                "7+yAYgtDUkpG9krVJQMNGl8HMIIFVTCCBD2gAwIBAgIQQQN40iY2WXoW2ybGvRCU" +
                "izANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlT" +
                "aWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTswOQYD" +
                "VQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3Jw" +
                "YSAoYykxMDEuMCwGA1UEAxMlVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNpZ25pbmcg" +
                "MjAxMCBDQTAeFw0xNDEyMTEwMDAwMDBaFw0xNTEyMjIyMzU5NTlaMIGGMQswCQYD" +
                "VQQGEwJKUDEPMA0GA1UECBMGTWl5YWdpMRgwFgYDVQQHEw9TZW5kYWkgSXp1bWkt" +
                "a3UxFzAVBgNVBAoUDkdyYXBlQ2l0eSBpbmMuMRowGAYDVQQLFBFUb29scyBEZXZl" +
                "bG9wbWVudDEXMBUGA1UEAxQOR3JhcGVDaXR5IGluYy4wggEiMA0GCSqGSIb3DQEB" +
                "AQUAA4IBDwAwggEKAoIBAQDBAvbKZVuylaQKkQelVQe1yZvPmutMe/B2VjZrwI7P" +
                "3q5qe6W4fDPR2LhFU0Y/B+G2l8RWKuIu+XuZDa+7PpxmyeVHzOgpXam3kbEOM70W" +
                "+p6X67XDgcH2DsdMKHmEHyOlcy1c4T2xo1AyuqnR2S3/xHL0iCr0W7tyCzhN5Lrs" +
                "dO4EJG/vq60gVMiSl1PJ1vHPivvbH1qh2D2/BRdiGs1sYZnyHSKAzSso696/8CJ9" +
                "40Ho6an2pohzbFPztuiktBHLwkuQhTig0+r73ZwJHpN5Mi1nn/nGv3GxaO+L2sGB" +
                "rZsNsM8P4XMJQDSEGggMc/uSR0Gd0hIPCy0mfgvBOE/vAgMBAAGjggGNMIIBiTAJ" +
                "BgNVHRMEAjAAMA4GA1UdDwEB/wQEAwIHgDArBgNVHR8EJDAiMCCgHqAchhpodHRw" +
                "Oi8vc2Yuc3ltY2IuY29tL3NmLmNybDBmBgNVHSAEXzBdMFsGC2CGSAGG+EUBBxcD" +
                "MEwwIwYIKwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUGCCsGAQUF" +
                "BwICMBkMF2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMBMGA1UdJQQMMAoGCCsGAQUF" +
                "BwMDMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3NmLnN5bWNk" +
                "LmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NmLnN5bWNiLmNvbS9zZi5jcnQwHwYD" +
                "VR0jBBgwFoAUz5mp6nsm9EvJjo/X8AUm7+PSp50wHQYDVR0OBBYEFABa8K2l1Hg1" +
                "9YQSqCwFDvhWG46OMBEGCWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3AgEbBAgw" +
                "BgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEAiMKYWjeOW+VYirEXwgOoW1XqjITQ" +
                "iZi+uJqsXWL8N4LBeKDgg6IjOpFoctTaFHdi6XK/T43xieeWV+LGZaqMXn5U6R4J" +
                "1/DDybiqQwbJO1pIYhLuuXwcG/oPcEBzDH4GNIIxyAENmRH9jyek00hXL49uMIe9" +
                "3bMqnJo9vdH6cA7R2hdoxOaav7UATifLw5DeOsLeKjIRupyKToHPSp4Miy3RDu1d" +
                "+CjNTW/rDfSZKk1lzaDapTn+0I2B8JcOyru1t5CBivn9ZD9cakwaV8LARObC5Z7o" +
                "z/iQKlfGipQSQykSNyIZaxvQiVJqhTYZmdn+UBOYwLz0Hl3ry5zGIqia4DCCAsQw" +
                "ggGsoAMCAQICBA7u7u4wDQYJKoZIhvcNAQEFBQAwJDEiMCAGA1UEAwwZR0MtVVdQ" +
                "IEludGVybmFsIERldmVsb3BlcjAeFw0xNjA3MDMxMjI5MjFaFw0yMTA3MDMxMjI5" +
                "MjFaMCQxIjAgBgNVBAMMGUdDLVVXUCBJbnRlcm5hbCBEZXZlbG9wZXIwggEiMA0G" +
                "CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCI6oTUZ0EH97kYFtn9KGLaSTbCcKFj" +
                "GxKJAGR35U2gIDy3U3tpqAFUBFwHj0fXAefdrsh0+mmIIulS7zS4vBMXiUpHBFkR" +
                "xzF5yupY17A6AJbnS8bda0T3Q/9Mq33nVecIXRbnb5NcVa4DI1RQq3xBHgMzl22n" +
                "pFzktKmuHuwXSe42wrHSEUt70xKHg4bhHITDmc7UMAjCh0Ff0NJJErt6/vaEekVo" +
                "p2YatURQWn2x4iT0k/qrOZDSqhLg1YSiy4CVNehL8l9gnRgDK4FnDC/9O2VTWPO3" +
                "oysfKmIOOPnKoHUxXtMkiemuXF4uMyicX+YfKw7LVywdR+8x3FFtFFA5AgMBAAEw" +
                "DQYJKoZIhvcNAQEFBQADggEBAGdqk67MD7guL6fdpWlaUzRR7xt/VysT3zlP/ji+" +
                "7D6KLfb1MEZfum35zDQPcmtHiZHMkEoavO3KxlYZoQtc2t9L7CQeLiEI4FCeRk89" +
                "Upnvlsc3JI5xjUquE9f2RxuWex0jXScKtjH2K2r40BzgRKHZ9BHqUX6qQQWwAIOS" +
                "cG/6teszHYqwV3gxsz7Ai908qienAqUafRh1CNnCGopyF79oVL2h8v6Eo/C7vdAw" +
                "0WDxUgaG6AiGzwm0f1o/W6W6MlGt2AL+NPIR/w+FBFpUGB6saA/Yr85HL8iYCOch" +
                "xqlpHMFkA1t1LHYiY2TWhUkRoDDNP3HFooliDOuIAxCXw10=";

            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 (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();
            var frame = Window.Current.Content as Frame;
            if (frame != null && frame.Content is MainPage)
            {
                ((MainPage)frame.Content).App_Suspending();
            }
            deferral.Complete();
        }
    }
}
