#pragma warning disable SYSLIB1045
using System.Text.RegularExpressions;
using BITKit;
using IDIS.Services;

await BITAppForNet.InitializeAsync("IDIS");

var config = Path.Combine(Environment.CurrentDirectory,"appsettings.json");
if (File.Exists(config))
{
	var json = File.ReadAllText(config);
	DataParser.Set(json);
}

var builder = WebApplication.CreateBuilder(args);

var cancellationTokenSource = new System.Threading.CancellationTokenSource();

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddTransient<IDIS_Service, IDIS_Service_MySQLBased>();

builder.Services.AddTransient<IDIS_Statistics_MySQLBased>();

builder.Services.AddTransient<IDIS_TemplateService, IDIS_TemplateService_MySQLBased>();

var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();

#region 输入二级节点地址

logger.LogInformation("请输入二级节点地址,例如:88.123.99");
string? str;
while (true)
{
	str = Console.ReadLine();
	if (string.IsNullOrEmpty(str))
	{
		str = "88.123.99";
		break;
	}
	const string pattern =@"^\d{1,3}\.\d{1,3}\.\d{1,3}$"; // 正则表达式模式
	if (Regex.IsMatch(str,pattern))
	{
		break;
	}
	logger.LogInformation("错误的地址格式,请按照正确格式输入,例如:88.123.99");
}
logger.LogInformation("输入的地址符合要求。");

#endregion

var idis_Service = app.Services.GetRequiredService<IDIS_Service>();
//idis_Service.PreAddress = str!;

app.Services.GetRequiredService<IDIS_Statistics_MySQLBased>();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
	app.UseSwagger();
	app.UseSwaggerUI();
}

app.MapGet("/", () => "Hello World!");

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
