using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
namespace Khd.Core.Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {

            //UI线程未捕获异常处理事件（UI主线程）
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;

            //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常

            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json")
                .Build();

            SystemData.PlcConfigs = configuration.GetSection("PlcConfigs").Get<List<PlcConfig>>();
            ConnectionStrings.ConnectionString = configuration["ConnectionStrings:DefaultConnection"].ToString();
            ////配置文件 加载 Thrift 推送端口
            //ThriftConfig.ThriftIpAddress = configuration["ThriftConfig:ThriftIpAddress"].ToString();
            //ThriftConfig.JiesShouDuanKou = int.Parse(configuration["ThriftConfig:JiesShouDuanKou"]);
            //ThriftConfig.TuiSongDuankou = int.Parse(configuration["ThriftConfig:TuiSongDuankou"]);
            //ThriftConfig.UpSite = configuration["ThriftConfig:upsite"].ToString();
            //FlagConfig.UpFlagNum = int.Parse(configuration["FlagConfig:UpFlagNum"]);
        }
        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                HandleException(e.Exception);
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                e.Handled = true;
            }
        }
        private static void HandleException(Exception ex)
        {
            //Logger log = new Logger();
            //log.Log(ex.ToString());
        }
        //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var exception = e.ExceptionObject as Exception;
                if (exception != null)
                {
                    HandleException(exception);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                //ignore
            }
        }
        private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                var exception = e.Exception as Exception;
                if (exception != null)
                {
                    HandleException(exception);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                e.SetObserved();
            }
        }
    }
}
