using Hncore.Infrastructure.Service;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Map;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.WeiFuTong;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace Hncore.Pass.PaymentCenter
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IHostingEnvironment env)
        {
            Configuration = env.UseAppsettings();
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<PaymentContext>(opt => { opt.UseMySql(Configuration["MySql"]); });

            RedisHelper.Initialization(new CSRedis.CSRedisClient(Configuration["Redis"]));

            services.AddHttpClient();
            services.AutoAddService();

            services.AddSingleton<WeiFuTongClient>(); 
            services.AddSingleton<WxPayClient>();            

            return services.Init(Configuration, CompatibilityVersion.Version_2_2, new ServiceOption
            {
                UseGlobalManageAuthFilter = false,
            });
        }

        public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime,
            ILoggerFactory loggerFactory)
        {
            app.Init(loggerFactory, applicationLifetime);
            MapConfig.Config();
        }
    }
}