using BBWYB.Common.Extensions;
using BBWYB.Common.Http;
using BBWYB.Common.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using U.Models;
using Utils;

namespace U
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public IServiceProvider ServiceProvider { get; private set; }
        public IConfiguration Configuration { get; private set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            var pjzsExeList = Process.GetProcessesByName("PJZS");
            if (pjzsExeList != null && pjzsExeList.Count() > 1)
            {
                Environment.Exit(Environment.ExitCode);
            }

//            var gl = new GlobalContext();
//            string userToken = string.Empty;
//#if DEBUG
//            //齐越山鸡
//            userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNTM1MzMwMzI4ODkyMTQ5NzYwIiwidGVhbUlkIjoiMTUxNjk3NDI1MDU0MjUwMTg4OCIsInNvblRlYW1JZHMiOiIxNDM2Mjg4NTAwMjM1MjQzNTIwIiwiZXhwIjoxNjk0NjY5NjkxfQ.cSwro-7bGwOu92YejH9JhMenTai7Mvf99i2paQCmxIw";
//#else

//            var tokenResult = ReadMMF();
//            if (tokenResult.isOk)
//                userToken = tokenResult.content;
//            else
//            {
//                MessageBox.Show($"读取内存数据失败\r\n{tokenResult.content}", "提示");
//                Environment.Exit(0);
//            }
//#endif
//            gl.UserToken = userToken;



            //var applicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //var builder = new ConfigurationBuilder().SetBasePath(applicationPath).AddJsonFile("appSettings.json", false, true);
            //Configuration = builder.Build();

            //gl.BBYWApiHost = Configuration.GetSection("BBWYApiHost").Value;
            //gl.MDSApiHost = Configuration.GetSection("MDSApiHost").Value;
            //IServiceCollection serviceCollection = new ServiceCollection();
            //serviceCollection.AddHttpClient();

            //serviceCollection.AddSingleton<RestApiService>();
            //serviceCollection.AddSingleton(gl);
            //serviceCollection.BatchRegisterServices(new Assembly[] { Assembly.Load("PJZS") }, typeof(IDenpendency));
            //serviceCollection.AddMapper(new MappingProfile());
            //ServiceProvider = serviceCollection.BuildServiceProvider();
            base.OnStartup(e);
        }

        public (bool isOk, string content) ReadMMF()
        {

            try
            {
                var token = MemoryHelper.GetMemoryToken();
                if (string.IsNullOrEmpty(token))
                    return (false, "token为空");
                else
                    return (true, token);
            }
            catch (Exception ex)
            {
                return (false, ex.Message);
            }
        }
    }
}