using BPA.SingleDevice.Business;
using BPA.SingleDevice.Interface;
using BPA.SingleDevice.Services;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;
using System.Windows;

namespace BPA.SingleDevice
{
    /// <summary>Interaction logic for App.xaml</summary>
    public partial class App : Application
    {
        public App()
        {
            Services = ConfigurServices();
        }

        public new static App Current => (App)Application.Current;

        public IServiceProvider Services { get; }
        public EventWaitHandle ProgramStarted { get; set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            bool createNew;
            ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "SingleDevice", out createNew);
            if (!createNew)
            {
                MessageBox.Show("程序已启动！");
                App.Current.Shutdown();
                Environment.Exit(0);
            }

            base.OnStartup(e);
            SystemHelper.GetInstance.CreateDesktopShortcut();
            SqlHelper.GetInstance.Init();
            //MainControl.GetInstance.Start();

            #region 注册调试日志。

            ILogService logService = App.Current.Services.GetService<ILogService>()!;
            MessageLog.GetInstance.NotifyShow = (string str) =>
            {
                logService?.LogDebugInfo(str);
            };

            #endregion 注册调试日志。

            Current.Services.GetService<IProcessControl>()?.InitalAsync();
            MainView mv = new MainView();
            mv.Show();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);

            //MainControl.GetInstance.Stop();
        }

        private static IServiceProvider ConfigurServices()
        {
            var services = new ServiceCollection();

            //services.AddSingleton<ISqlHelper, SqlHelper>();
            services.AddSingleton<ILogService, LogService>();
            services.AddSingleton<IProcessControl, ProcessControl>();
            services.AddSingleton<GlobalData>();

            //services.AddTransient<RawMaterialManagementViewModel>();
            services.AddTransient<OrderMainViewModel>();
            services.AddTransient<RecipeManagementViewModel>();

            //services.AddSingleton<ParamsSetViewModel>();
            services.AddSingleton<RunLogViewModel>();
            //services.AddSingleton<AlarmLogViewModel>();
            services.AddSingleton<RecipeCompleteViewModel>();
            services.AddSingleton<UserLogViewModel>();
            services.AddSingleton<DebugLogViewModel>();
            services.AddSingleton<VarMonitorViewModel>();
            services.AddSingleton<DebugViewModel>();

            services.AddSingleton<RecipeStatusViewModel>();
            services.AddTransient<MaterialManagementViewModel>();
            services.AddTransient<MaterialBindingViewModel>();

            

            return services.BuildServiceProvider();
        }
    }
}