using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.IO;
using System.Reflection;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;
using System.Text.Json.Serialization;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.Http.Features;

namespace AMESCoreStudio.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IConfiguration>(Configuration);
            //services.AddDbContext<AMESContext>(
            //    opt => opt.UseLazyLoadingProxies().UseOracle(Configuration.GetConnectionString("AMESContext"), b => b.UseOracleSQLCompatibility("11")));

            services.AddDbContext<AMESContext>(
    opt => opt.UseLazyLoadingProxies().UseOracle(Configuration.GetConnectionString("AMESContext"), b => b.UseOracleSQLCompatibility("11"))
    .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())));

            services.AddDbContext<QASRVContext>(options =>
             options.UseSqlServer(Configuration.GetConnectionString("QASRVConnection"))
             );

            services.AddControllers();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "AMESCoreStudio",
                    Description = "RESTful API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Marvin Hong",
                        Email = string.Empty,
                        Url = new Uri("https://twitter.com/spboyer"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "GNU General Public Licence (GPL) ",
                        Url = new Uri("https://example.com/license"),
                    }
                });

                // Ϊ Swagger ����xml�ĵ�ע��·��
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            // ���� Invalid ModelState Filter (������ Model ���ҥ��ѷ|�L����)
            services.Configure<ApiBehaviorOptions>(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
            });

            // �NAPI��LazyLoader �ݩʤ����
            services.AddMvc().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = CustomContractResolver.Instance;
            });

            // FormPost�ƶq�W��
            services.Configure<FormOptions>(options =>
            {
                options.ValueLengthLimit = 209715200;
                options.ValueCountLimit = int.MaxValue;
            });

            // ModelBinding���ƤW��
            services.AddMvc(options =>
            {
                options.MaxModelBindingCollectionSize = int.MaxValue;
            });

            //services.AddControllers().AddJsonOptions(x =>
            //     x.JsonSerializerOptions..ReferenceHandler = ReferenceHandler.Preserve.Ignore);
            //services.AddControllers().AddNewtonsoftJson(x =>
            //x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

            //services.AddControllers().AddNewtonsoftJson(x =>
            //x.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger(c =>
            {
                c.SerializeAsV2 = true;
            });

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "AMESCoreStudio v1");
                c.RoutePrefix = "amesapi";//���ø��ڵ����
            });

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}