using MediaBrowser.Common.Events;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.ServerApplication.Splash;
using System;
using System.Diagnostics;
using System.Windows;
namespace MediaBrowser.ServerApplication
{
    /// 
    /// Interaction logic for App.xaml
    /// 
    public partial class App : Application
    {
        /// 
        /// Gets or sets the logger.
        /// 
        /// The logger.
        private readonly ILogger _logger;
        /// 
        /// Gets or sets the composition root.
        /// 
        /// The composition root.
        private readonly ApplicationHost _appHost;
        public event EventHandler AppStarted;
        public bool IsRunningAsService { get; private set; }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The logger.
        public App(ApplicationHost appHost, ILogger logger, bool isRunningAsService)
        {
            _appHost = appHost;
            _logger = logger;
            IsRunningAsService = isRunningAsService;
            InitializeComponent();
        }
        /// 
        /// Gets the name of the uninstaller file.
        /// 
        /// The name of the uninstaller file.
        protected string UninstallerFileName
        {
            get { return "MediaBrowser.Server.Uninstall.exe"; }
        }
        public void OnUnhandledException(Exception ex)
        {
            MessageBox.Show("Unhandled exception: " + ex.Message);
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            LoadApplication();
        }
        /// 
        /// Loads the kernel.
        /// 
        protected async void LoadApplication()
        {
            try
            {
                if (!IsRunningAsService)
                {
                    ShowSplashWindow();
                }
                await _appHost.Init();
                var task = _appHost.RunStartupTasks();
                if (!IsRunningAsService)
                {
                    HideSplashWindow();
                }
                if (!IsRunningAsService)
                {
                    ShowMainWindow();
                }
                EventHelper.FireEventIfNotNull(AppStarted, this, EventArgs.Empty, _logger);
                await task;
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error launching application", ex);
                MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
                // Shutdown the app with an error code
                Shutdown(1);
            }
        }
        private MainWindow _mainWindow;
        private void ShowMainWindow()
        {
            var host = _appHost;
            var win = new MainWindow(host.LogManager, host,
                                     host.ServerConfigurationManager, host.UserManager,
                                     host.LibraryManager, host.JsonSerializer,
                                     host.DisplayPreferencesRepository);
            win.Show();
            _mainWindow = win;
        }
        private void HideMainWindow()
        {
            if (_mainWindow != null)
            {
                _mainWindow.Hide();
                _mainWindow = null;
            }
        }
        private SplashWindow _splashWindow;
        private void ShowSplashWindow()
        {
            var win = new SplashWindow(_appHost.ApplicationVersion);
            win.Show();
            _splashWindow = win;
        }
        private void HideSplashWindow()
        {
            if (_splashWindow != null)
            {
                _splashWindow.Hide();
                _splashWindow = null;
            }
        }
        public void ShutdownApplication()
        {
            Dispatcher.Invoke(Shutdown);
        }
    }
}