using HTCoreServiceApp.Common;
using HTCoreServiceApp.WebApi;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Http.HttpResults;
using SqlSugar;
using System.Text.Json.Serialization;

namespace HTCoreServiceApp
{
	public class Program
	{
		public static void Main(string[] args)
		{
			var builder = WebApplication.CreateSlimBuilder(args);

			builder.Services.ConfigureHttpJsonOptions(options =>
			{
				options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
			});

			/*************ÆôÓÃSqlSugarµÄAOT*******************/
			StaticConfig.EnableAot = true;

			IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
			builder.Services.AddSingleton(new ConfigHelper(configuration));

			builder.Services.AddSingleton(new DriverService());

			builder.WebHost.UseUrls($"http://*:{builder.Configuration.GetSection("ServiceHostPort").Value}");

			//builder.WebHost.UseWebRoot($"http://*:{builder.Configuration.GetSection("ServiceHostPort").Value}");

			//builder.Services.AddControllers();

			builder.Services.AddStackExchangeRedisCache(options =>
			{
				options.Configuration = "localhost:3069";
				options.InstanceName = "test";
			});

			//builder.Services.AddEndpointsApiExplorer();

			builder.Services.AddScoped<DataLiveCache>();
			builder.Services.AddScoped<DeviceControl>();

			var app = builder.Build();

			var dataLiveCacheApi = app.MapGroup("/DataLiveCache");
			var deviceControl = app.MapGroup("/DeviceControl");

			dataLiveCacheApi.MapGet("/Get",
				async(DataLiveCache dlc) 
				=> Results.Content(await dlc.Get()));

			dataLiveCacheApi.MapGet("/{Name}", 
				async (string Name, DataLiveCache dlc) 
				=> Results.Content(await dlc.GetName(Name)));
			//tagName
			dataLiveCacheApi.MapGet("/GetLiveGroupData/groupName={groupName}",
				async (string groupName, DataLiveCache dlc) 
				=> Results.Content(await dlc.GetLiveGroupData(groupName)));

			dataLiveCacheApi.MapGet("/GetLiveTagData/groupName={groupName}&tagName={tagName}",
				async (string groupName, string tagName, DataLiveCache dlc) 
				=> Results.Content(await dlc.GetLiveTagData(groupName,tagName)));


			deviceControl.MapGet("/Get",
				async (DeviceControl dc)
				=> Results.Content(await dc.Get()));

			deviceControl.MapGet("/{Name}",
				async (string Name, DeviceControl dc)
				=> Results.Content(await dc.GetName(Name)));

			deviceControl.MapGet("/SetDeviceData/groupName={groupName}&tagName={tagName}&value={value}",
				async (string groupName, string tagName ,string value,DeviceControl dc)
				=> Results.Content(await dc.SetDeviceData(groupName, tagName, value)));

			var sampleTodos = new Todo[] {
				new(1, "Walk the dog"),
				new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
				new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
				new(4, "Clean the bathroom"),
				new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2)))
			};

			var todosApi = app.MapGroup("/todos");
			todosApi.MapGet("/", () => sampleTodos);
			todosApi.MapGet("/{id}", (int id) =>
				sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
					? Results.Ok(todo)
					: Results.NotFound());



			app.Run();
		}
	}

	public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);

	[JsonSerializable(typeof(Todo[]))]
	internal partial class AppJsonSerializerContext : JsonSerializerContext
	{

	}
}
