using BarCodeSamples.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 BarCodeSamples
{
    /// <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()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            C1.UWP.LicenseManager.Key =
                "ACQBJAIkB31CAGEAcgBDAG8AZABlAFMAYQBtAHAAbABlAHMAO7PeNh8QhtTX4M5i" +
                "U002ID7IMU3rOHCBR00dcRMO/yxJxgFIRxsRLqcfOlWaAJJ74hTCTtAijBnrQbZo" +
                "gynk+OveSbv8D+b4IQS4CGPgFxc4pY5qyNww3YPn3Km9H6bxAMEHRDwLW0xXn3X9" +
                "R5ZONFU8XpCtjsum9KXUm1Tm6m7GCU+Imm9brIxv59yIzMN0VmbXwm9ebAxAm5F1" +
                "ZgHDVtbb6a44fHJ5lRbopPBUGgVEh+BjFJ+U+NFvWPzD/RoNM3pKLDFhOWaq+Y28" +
                "ee3f+TbAzf0ueL33z053mnxUC1lAw3jwV/LO0fT8xa0BfGLGxiE1+CgPnh5EGOTD" +
                "pJ3pLVNS96Jevxe7k5fCugflKSL9J24Qry/7dNhYawfGDOp7L3Rtf07EypQeqTLs" +
                "qgwBla4NH55x3L/4lIxHWJSMG/kEmoZo92M1yGE63xs3NqXtj89VK+9n6FT2qvDn" +
                "44KlYFX6AWY0niYYwGOvdlDUH/vmwe8jHzFgFF/9Bu+M5GZ4q0az9vxg2YNaV74T" +
                "w0hT/Py5sEvquu8DIDJQ9nQ03tp/U6ftDAq9FuRDAA2BWmqbv9hu9/qTkyUDUaNl" +
                "XQJ/VjI02tXMNz93pDaIJe/9QUd3mNzVaKVRGPvqdPMZ5rlMbtLYNtmxpqkHDz7T" +
                "w8r8Iv51r5PSG6vp7vLqne/B7ZowggVVMIIEPaADAgECAhBBA3jSJjZZehbbJsa9" +
                "EJSLMA0GCSqGSIb3DQEBBQUAMIG0MQswCQYDVQQGEwJVUzEXMBUGA1UEChMOVmVy" +
                "aVNpZ24sIEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsxOzA5" +
                "BgNVBAsTMlRlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy52ZXJpc2lnbi5jb20v" +
                "cnBhIChjKTEwMS4wLAYDVQQDEyVWZXJpU2lnbiBDbGFzcyAzIENvZGUgU2lnbmlu" +
                "ZyAyMDEwIENBMB4XDTE0MTIxMTAwMDAwMFoXDTE1MTIyMjIzNTk1OVowgYYxCzAJ" +
                "BgNVBAYTAkpQMQ8wDQYDVQQIEwZNaXlhZ2kxGDAWBgNVBAcTD1NlbmRhaSBJenVt" +
                "aS1rdTEXMBUGA1UEChQOR3JhcGVDaXR5IGluYy4xGjAYBgNVBAsUEVRvb2xzIERl" +
                "dmVsb3BtZW50MRcwFQYDVQQDFA5HcmFwZUNpdHkgaW5jLjCCASIwDQYJKoZIhvcN" +
                "AQEBBQADggEPADCCAQoCggEBAMEC9splW7KVpAqRB6VVB7XJm8+a60x78HZWNmvA" +
                "js/ermp7pbh8M9HYuEVTRj8H4baXxFYq4i75e5kNr7s+nGbJ5UfM6CldqbeRsQ4z" +
                "vRb6npfrtcOBwfYOx0woeYQfI6VzLVzhPbGjUDK6qdHZLf/EcvSIKvRbu3ILOE3k" +
                "uux07gQkb++rrSBUyJKXU8nW8c+K+9sfWqHYPb8FF2IazWxhmfIdIoDNKyjr3r/w" +
                "In3jQejpqfamiHNsU/O26KS0EcvCS5CFOKDT6vvdnAkek3kyLWef+ca/cbFo74va" +
                "wYGtmw2wzw/hcwlANIQaCAxz+5JHQZ3SEg8LLSZ+C8E4T+8CAwEAAaOCAY0wggGJ" +
                "MAkGA1UdEwQCMAAwDgYDVR0PAQH/BAQDAgeAMCsGA1UdHwQkMCIwIKAeoByGGmh0" +
                "dHA6Ly9zZi5zeW1jYi5jb20vc2YuY3JsMGYGA1UdIARfMF0wWwYLYIZIAYb4RQEH" +
                "FwMwTDAjBggrBgEFBQcCARYXaHR0cHM6Ly9kLnN5bWNiLmNvbS9jcHMwJQYIKwYB" +
                "BQUHAgIwGQwXaHR0cHM6Ly9kLnN5bWNiLmNvbS9ycGEwEwYDVR0lBAwwCgYIKwYB" +
                "BQUHAwMwVwYIKwYBBQUHAQEESzBJMB8GCCsGAQUFBzABhhNodHRwOi8vc2Yuc3lt" +
                "Y2QuY29tMCYGCCsGAQUFBzAChhpodHRwOi8vc2Yuc3ltY2IuY29tL3NmLmNydDAf" +
                "BgNVHSMEGDAWgBTPmanqeyb0S8mOj9fwBSbv49KnnTAdBgNVHQ4EFgQUAFrwraXU" +
                "eDX1hBKoLAUO+FYbjo4wEQYJYIZIAYb4QgEBBAQDAgQQMBYGCisGAQQBgjcCARsE" +
                "CDAGAQEAAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQCIwphaN45b5ViKsRfCA6hbVeqM" +
                "hNCJmL64mqxdYvw3gsF4oOCDoiM6kWhy1NoUd2Lpcr9PjfGJ55ZX4sZlqoxeflTp" +
                "HgnX8MPJuKpDBsk7WkhiEu65fBwb+g9wQHMMfgY0gjHIAQ2ZEf2PJ6TTSFcvj24w" +
                "h73dsyqcmj290fpwDtHaF2jE5pq/tQBOJ8vDkN46wt4qMhG6nIpOgc9KngyLLdEO" +
                "7V34KM1Nb+sN9JkqTWXNoNqlOf7QjYHwlw7Ku7W3kIGK+f1kP1xqTBpXwsBE5sLl" +
                "nujP+JAqV8aKlBJDKRI3IhlrG9CJUmqFNhmZ2f5QE5jAvPQeXevLnMYiqJrgMIIC" +
                "xDCCAaygAwIBAgIEDu7u7jANBgkqhkiG9w0BAQUFADAkMSIwIAYDVQQDDBlHQy1V" +
                "V1AgSW50ZXJuYWwgRGV2ZWxvcGVyMB4XDTE2MDUxMjE0MTQyN1oXDTE3MDUxMjE0" +
                "MTQyN1owJDEiMCAGA1UEAwwZR0MtVVdQIEludGVybmFsIERldmVsb3BlcjCCASIw" +
                "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAIMXxACo71vIEy6YhqrhKGgreaZE" +
                "ECZO+x396B7DDgg/ESAP7kOO9WbNHOL4iRidWESV31q056GCbpQECJ9gKtic4ui4" +
                "hIelAmPUKjMZTG29uybHXjkkUhMJHNDmErUa3L+cS3/t0sAo5UQXdN0wzZCfvo5d" +
                "Bc4yUWP1liOnVmYUOhe7oO+6AaL8X5vsJEXgLYHNogFdU0tb2g1u0dx3pRy3Ad0c" +
                "Gg3qOvA3CF6wqLQEQ9htCtZCLkJNepI5QBgr60Unl0IbldXlQxEicjkALn4R1Pbb" +
                "Y3oYHp41IOjRGFMtbNmh4d9nQjbjQcoMFCyNca1YwSongk7gMrYOLmcTeRECAwEA" +
                "ATANBgkqhkiG9w0BAQUFAAOCAQEAain8mWbsjZ2PoTLu7HE6owx4+w8B1dlwYs2N" +
                "AXlX3hQGiqE+jpXHOP2li5P2izpTp+vkatruMIEa5woMYKxA8GV/uSimnWTzBiwT" +
                "pJAxfsk/dmQ/j3YhhhBxBkdKuE6kjvTN3w9poKvDgILBIKkVSsyBK7zJQbLpKqjO" +
                "7OI3KUp2US3ZbAFdDWLmgqlx1hmROuVV+V2HwQWyuaBASIcZOr699OXJffk4KIdT" +
                "iN41bi0CptTv0UeYwIKy8lysdVtsLGNg49pkZY8aZ6XjMoEvgZ6TtFtoAviHTVha" +
                "BZPL4wO10ZIEEg9oe+/zGkpk3dIUB3xMWpFq8e6PzUD6U04QvA==";
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used when the application is launched to open a specific file, to display
        /// search results, and so forth.
        /// </summary>
        /// <param name="args">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();
        }
    }
}
