using Hncore.Infrastructure.Service;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.BaseInfo.Map;
using Hncore.Pass.BaseInfo.Models;
using Hncore.Pass.BaseInfo.Service;
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.BaseInfo
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

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

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

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

            services.AddHttpClient();

            services.AddScoped<ManageService>();
            services.AddScoped<UserService>();
            services.AutoAddService();

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

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