using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace ViewShotExample.Net46
{
    /// 
    /// Provides application-specific behavior to supplement the default Application class.
    /// 
    public partial class App : Application
    {
        private readonly AppReactPage _reactPage = new AppReactPage();
        /// 
        /// 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().
        /// 
        public App()
        {
        }
        /// 
        /// Override method fired prior to the Startup event when the Run method of the Application object is called...
        /// 
        /// 
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            OnCreate(e.Args);
        }
        /// 
        /// Called whenever the app is opened to initialized...
        /// 
        /// 
        private void OnCreate(string[] arguments)
        {
            var shellWindow = Application.Current.MainWindow;
            if (shellWindow == null)
            {
                shellWindow = new Window
                {
                    ShowActivated = true,
                    ShowInTaskbar = true,
                    Title = "ViewShotExample.Net46",
                    Height = 768,
                    Width = 1024,
                    WindowStartupLocation = WindowStartupLocation.CenterScreen
                };
                Application.Current.MainWindow = shellWindow;
            }
            //Show Window if it is not already active...
            if (!shellWindow.IsLoaded)
            {
                shellWindow.Show();
            }
            var rootFrame = shellWindow.Content as Frame;
            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                _reactPage.OnCreate(arguments);
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                rootFrame.NavigationFailed += OnNavigationFailed;
                // Place the frame in the current Window
                shellWindow.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
                rootFrame.Content = _reactPage;
            }
            // Ensure the current window is active
            shellWindow.Activate();
        }
        /// 
        /// Invoked when Navigation to a certain page fails
        /// 
        /// The Frame which failed navigation
        /// Details about the navigation failure
        private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page...");
        }
    }
}