using Serilog;
using SlnMesnac.Serilog;
using System.Timers;

namespace SlnMesnac
{
    /// <summary>
    /// 启动类
    /// </summary>
    public class Program
    {
        //删除日志定时器
        private static System.Timers.Timer deleteLogTimer;

        private static SerilogHelper? serilogHelper;

        /// <summary>
        /// Main方法
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();
            //   host.Run();
            var hostTask = host.RunAsync();
            serilogHelper = host.Services.GetRequiredService<SerilogHelper>();
            Console.WriteLine("==========================上海物流AGV项目Web Api服务启动!==========================");
            serilogHelper.Info("==========================上海物流AGV项目Web Api服务启动!==========================");

            #region 定时删除日志

            //DeleteLogsLogic();
            //deleteLogTimer = new System.Timers.Timer(24 * 60 * 60 * 1000);
            //deleteLogTimer.Elapsed += OnTimedEvent;
            //deleteLogTimer.AutoReset = true;
            //deleteLogTimer.Enabled = true;

            #endregion 定时删除日志

            while (true)
            {
                Console.ReadLine();
            }
        }

        #region 定时删除日志处理

        /// <summary>
        /// 定时器触发的事件处理方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            DeleteLogsLogic();
        }

        /// <summary>
        /// 清除日志文件
        /// </summary>
        private static void DeleteLogsLogic()
        {
            try
            {
                // 获取当前程序运行目录下的 Log 文件夹
                string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");

                // 检查 Log 文件夹是否存在
                if (!Directory.Exists(logPath))
                {
                    // 如果 Log 文件夹不存在，可以选择记录日志或者直接返回
                    Console.WriteLine($"Log 文件夹 {logPath} 不存在。");
                    return;
                }

                // 获取 Log 文件夹下的所有子文件夹
                string[] logDirectories = Directory.GetDirectories(logPath);

                foreach (string directoryPath in logDirectories)
                {
                    string directoryName = Path.GetFileName(directoryPath);
                    if (!string.IsNullOrEmpty(directoryName) && DateTime.TryParse(directoryName, out DateTime directoryDate))
                    {
                        // 检查文件夹日期是否早于当前日期三天之前
                        if (directoryDate < DateTime.Now.AddDays(-3))
                        {
                            try
                            {
                                // 删除符合条件的文件夹
                                Directory.Delete(directoryPath, true);
                                Console.WriteLine($"已删除文件夹: {directoryPath}");
                            }
                            catch (Exception ex)
                            {
                                // 记录删除文件夹时的异常信息
                                Console.WriteLine($"删除文件夹 {directoryPath} 时出错: {ex.Message}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // 记录整体操作的异常信息
                Console.WriteLine($"执行删除日志逻辑时出错: {ex.Message}");
            }
        }

        #endregion 定时删除日志处理

        /// <summary>
        /// CreateHostBuilder
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                 .UseSerilog((context, config) =>
                 {
                 })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://0.0.0.0:7024");
                    webBuilder.UseStartup<Startup>();
                });
    }
}