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

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

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

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

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

            services.AddServiceClient(Configuration["Service_BaseUrl"]);

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

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