using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using WebAPIServer.Common;

namespace WebAPIServer
{
    public class Program
    {
        public record STU(string nnn, string bbb);
        public static void Main(string[] args)
        {

            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddMemoryCache();
            builder.Services.AddControllers();


            builder.Services.AddCors(options =>
            {
                options.AddPolicy(name: "Vue3",
                                  policy =>
                                  {
                                      //policy.WithOrigins("http://localhost:5180", 
                                      //    "http://localhost:8809/", 
                                      //    "http://www.contoso.com", 
                                      //    "http://new.uts-data.com:6688/", "http://new.uts-data.com")
                                      policy
                                       .AllowAnyOrigin()
                                       .AllowAnyHeader()
                                       .AllowAnyMethod();
                                  });
            });

            builder.Services.AddAuthorization();
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(option =>
                {
                    var sec = Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"]);

                    option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,

                        ValidateIssuerSigningKey = true,
                        ValidIssuer = builder.Configuration["JwT:Issuer"],
                        ValidAudience = builder.Configuration["JwT:Audience"],
                        IssuerSigningKey = new SymmetricSecurityKey(sec)
                    };

                    //option.Events = new JwtBearerEvents
                    //{
                    //    OnMessageReceived = context =>
                    //    {
                    //        var token = context.Request.Headers["token"].FirstOrDefault();
                    //        if (string.IsNullOrEmpty(token))
                    //        {
                    //            // 如果没有找到 token 头部，则继续检查 Authorization 头部
                    //            token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
                    //        }
                    //        // 如果找到了 token，则将其设置到 HttpContext 中
                    //        if (!string.IsNullOrEmpty(token))
                    //        {
                    //            context.Token = token;
                    //        }
                    //        return Task.CompletedTask;
                    //    }
                    //};
                });
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            app.UseCors("Vue3");

            app.UseAuthentication(); // 添加认证中间件
            app.UseAuthorization(); // 使用授权中间件

            app.MapControllers();

            StaticData.GetWebAPIMethod();
            app.Run();
        }
    }
}
