using log4net;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;

namespace NEIntelligentControl2
{
    /// <summary>
    /// 新能源智能发电集中控制系统
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        public static IServiceProvider ServiceProvider { get; set; }

        private ILog log = LogManager.GetLogger("File");
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);
            ServiceProvider = serviceCollection.BuildServiceProvider();

            var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
            mainWindow.Show();
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                e.Handled = true;
                log.Error("应用程序出现重大错误,但可继续运行!", e.Exception);
            }
            catch (Exception ex)
            {
                log.Fatal("应用程序出现致命错误,已经结束运行!", e.Exception);
            }
        }

        /// <summary>
        /// 获取服务
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns>服务</returns>
        public static T GetService<T>()
        {
            return (T)ServiceProvider.GetService(typeof(T));
        }

        /// <summary>
        /// 依赖注入
        /// </summary>
        private void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<Models.Pages.IPageAction, Models.Pages.PageManager>()
                .AddSingleton<Service.WebSocket.MessageBridge>()
                .AddSingleton<Models.Messages.WEBHelper>()
                .AddSingleton<Service.Windturbine.CacheManager>()
                .AddSingleton<Service.WebSocket.UrlManager>()
                .AddSingleton<Service.User.UserManager>()
                .AddSingleton<Service.Voice.VoiceManager>()
                .AddSingleton<Service.AGC.AGCManager>()
                .AddSingleton<Service.Alarm.AlarmManager>()
                .AddSingleton<Service.Windturbine.InfoManager>()
                .AddSingleton<Service.Windturbine.SuggestionManager>()
                .AddSingleton<Service.Windturbine.ControlManager>()
                .AddSingleton<Models.Pages.TagManager>()
                .AddSingleton<Service.Station.StationManager>()
                .AddSingleton<MainWindow>()
                .AddTransient<Windows.ConfirmWindow>()
                .AddTransient<Windows.WindturbineInfoWindow>()
                .AddTransient<Windows.UserWindow>()
                .AddTransient< Pages.User.PageLogin>()
                .AddTransient< Pages.User.PageUserEdit>()
                .AddSingleton<Pages.Matrix.PageMatrix>()
                .AddTransient<Pages.AGC.PageAGC>()
                .AddSingleton<Pages.Help.PageHelp>()
                .AddSingleton<Pages.BoostStation.PageBoostStation>()
                .AddSingleton<Pages.BoostStation.PageBoostStation2>()
                .AddTransient<Pages.BoostStation.PageStation>()
                .AddTransient<Pages.BoostStation.PageStation2>()
                .AddSingleton<Pages.Home.HomePage>()
                .AddSingleton<Pages.Home.StateTimePage>()
                .AddSingleton<Pages.Alarm.PageAlarmCenter>()
                .AddTransient<Pages.Matrix.PageMatrixAll>()
                .AddTransient<Pages.Home.PageWindturbineAGC>()
                .AddTransient<Pages.Matrix.PageMatrixStation>()
                .AddTransient<Windows.ParameterComparisonWindow>()
                .AddTransient<Windows.Alarm.DeviceDetailAlarmWindow>()
                .AddTransient<Windows.Alarm.HistoryAlarmWindow>()
                .AddTransient<Windows.Alarm.HistoryFaultWindow>()
                .AddTransient<Windows.Alarm.RealTimeAlarmWindow>()
                .AddTransient<Windows.ControlTrackWindow>()
                .AddTransient<Windows.RecomendWindow>()
                .AddTransient<Windows.HistoryDataWindow>()
                .AddTransient<Windows.PV.InverterInfoWindow>()
                .AddTransient<Windows.PV.SUN2000InfoWindow>();
        }
    }
}