using BaseModule;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Services.Dialogs;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Txgy.EWS.Client.BLL;
using Txgy.EWS.Client.Common;
using Txgy.EWS.Client.Common.Helpers;
using Txgy.EWS.Client.DAL;
using Txgy.EWS.Client.IBLL;
using Txgy.EWS.Client.IDAL;
using Txgy.EWS.Client.ILog;
using Txgy.EWS.Client.Log;
using Txgy.EWS.Client.MainModule;
using Txgy.EWS.Client.PageModule;
using Txgy.EWS.Client.Start.ViewModels;
using Txgy.EWS.Client.Start.Views;
using Txgy.EWS.Client.SysModule;

namespace Txgy.EWS.Client.Start
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : PrismApplication
    {
        private bool _loginSucceeded;

        public App()
        {
            // 日志Log4Net初始化
            LogConfiguration log = new LogConfiguration();
            log.Initialize();

            this.Exit += App_Exit;
            //this.DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            try
            {
                GlobalData.IsRunning = false;
                Task.WaitAll(GlobalData.TaskList.ToArray());
                //RealtimeViewModel.GetInstance().Dispose();
            }
            catch { }
        }
        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //CommonLogHelper.Fatal(e.Exception);//添加系统全局抓取日志记录
            //e.Handled = true;
        }

        protected override Window CreateShell()
        {
            var config = BusinessConfigManager.Current;
            GlobalConfig.InitializeRuntimeSettings(config.Runtime);
            GlobalConfig.ReadConfig();
            //Debug.WriteLine(GlobalConfig.LoadDataTimeLenMins);
            //Debug.WriteLine(GlobalConfig.DataCacheTimeLenMins);
            ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
            _loginSucceeded = Container.Resolve<LoginView>().ShowDialog() == true;
            if (!_loginSucceeded)
            {
                Shutdown();
                return null;
            }

            return Container.Resolve<MainWindow>();
        }
        protected override void InitializeShell(Window shell)
        {
            if (!_loginSucceeded || shell == null)
            {
                return;
            }

            //if (GlobalData.IsDesign)
            //{                
            //    base.InitializeShell(shell);
            //}
            //else
            {
                //GlobalConfig.ReadConfig();
                //CommonLogHelper.Debug("====Start=====>");
                ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
                base.InitializeShell(shell);
            }
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            /// 全局对象注册
            containerRegistry.Register<Dispatcher>(() => Application.Current.Dispatcher);
            //注册View
            containerRegistry.RegisterDialog<LoginView>();

            //注册DAL
            containerRegistry.Register<IWebDataAccess, WebDataAccess>();
            containerRegistry.Register<ILocalDataAccess, LocalDataAccess>();
            containerRegistry.Register<IRemoteMySQLDataAccess, RemoteMySQLDataAccess>();
            containerRegistry.Register<IRemoteMySqlDownload, RemoteMySqlDownload>();
            containerRegistry.Register<IUpgradeDal, UpgradeDal>();
            containerRegistry.Register<IMenuDal, MenuDal>();
            containerRegistry.Register<IUserDal, UserDal>();
            containerRegistry.Register<IRoleDal, RoleDal>();

            containerRegistry.RegisterSingleton<FreeSqlTencent>();
            //containerRegistry.RegisterSingleton<IFreeSql, FreeDB>();
            //注册BLL
            containerRegistry.Register<IUpgradeFileBLL, UpgradeFileBLL>();
            containerRegistry.Register<ILoginBLL, LoginBLL>();
            containerRegistry.Register<IMenuBLL, MenuBLL>();
            containerRegistry.Register<IUserBLL, UserBLL>();
            containerRegistry.Register<IRoleBLL, RoleBLL>();
            containerRegistry.Register<ISearchMsEventBLL, SearchMsEventBLL>();
            //containerRegistry.Register<IRemoteDownloadDataBLL, RemoteDownloadDataBLL>();
            //注册对话框
            containerRegistry.Register<IDialogHostService, DialogHostService>();

            //注册日志
            containerRegistry.RegisterSingleton<ILogHelper, LogHelper>();
            //containerRegistry.RegisterDialog<LoginView, LoginViewModel>();
            //throw new NotImplementedException();
        }
        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            base.ConfigureModuleCatalog(moduleCatalog);
            // 自动扫描目录

            // 手动添加
            moduleCatalog.AddModule<MainModuleProfile>();
            moduleCatalog.AddModule<PageModuleProfile>();
            moduleCatalog.AddModule<SysModuleProfile>();
            moduleCatalog.AddModule<BaseModuleProfiel>();
            //moduleCatalog.AddModule<SystemModule>();
            //moduleCatalog.AddModule<BaseInfoModule>();
            //moduleCatalog.AddModule<AutoInfoModule>();
            //moduleCatalog.AddModule<ReportInfoModule>();

        }
    }
}
