using Microsoft.EntityFrameworkCore;
using RB_MES_API.Context;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Microsoft.OpenApi.Models;
using RB_MES_API.Controllers;
using RB_MES_API.Controllers.Cloud;
using RB_MES_APICore.Context;

namespace RB_MES_APICore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            string rbcon = ConnectionString.GetConfig("ConnectionStrings", "RBConn"); 
            services.AddDbContext<RBContext>(options =>
                options.UseSqlServer(rbcon),ServiceLifetime.Transient);

            string mescon = ConnectionString.GetConfig("ConnectionStrings", "MES_Conn"); 
            services.AddDbContext<MESContext>(options =>
                 options.UseOracle(mescon), ServiceLifetime.Transient);

            services.AddControllers().AddJsonOptions(
                options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
            services.AddEndpointsApiExplorer();
            services.AddHttpClient("enpower", c =>
            {
                c.BaseAddress = new Uri("http://i.enpower.com:8600/k3cloud");
                c.DefaultRequestHeaders.Add("Accept", "application/json");
            });
            services.AddHttpClient();
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = Configuration["Swagger:Version"] });
                var dir = new DirectoryInfo(AppContext.BaseDirectory);
                foreach (FileInfo file in dir.EnumerateFiles("*.xml"))
                {
                    options.IncludeXmlComments(file.FullName);
                }
            });
            
            // Add the processing server as IHostedService
            services.AddMvc();
            services.AddMvcCore();
            services.AddMvcCore().AddControllersAsServices();
            services.AddTransient<IShareController, ShareController>();
            services.AddTransient<IKDSqlHelper, KDSqlHelper>();
            services.AddTransient<IKDCloudHttpClient, KDCloudHttpClient>();
            services.AddTransient<IChiledSelect, ChiledSelect>();
            services.AddSession();
            services.AddControllersWithViews();
            services.AddHttpContextAccessor();
            services.AddHostedService<PushBackgroundService>();
            //解决中文被编码
            services.AddSingleton(HtmlEncoder.Create(new[] { UnicodeRanges.All }));
        }

        [Obsolete]
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.RoutePrefix = "Swagger";
            });
            app.UseStaticFiles();
            app.UseExceptionHandler("/Error");
            app.UseHttpLogging();
            app.UseHttpsRedirection();
            app.UseCors();
            app.UseSession();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

        }

    }
}
