using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Quartz;
using System;
using System.Linq;
using wispro.sp.api.Job;

namespace wispro.sp.api
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var serviceScope = host.Services.CreateScope())
            {
                var services = serviceScope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService<spDbContext>();

                    var result = context.Database.EnsureCreated();

                    if (result)
                    {
                        System.Diagnostics.Debug.Write("数据库创建成功！");
                    }
                    else
                    {
                        System.Diagnostics.Debug.Write("数据库创建失败！");
                    }

                }
                catch (Exception ex)
                {
                    //var logger = services.GetRequiredService<ILogger<Program>>();
                    //logger.LogError(ex, "An error occurred.");
                }
            }

            #region 每月获取绩效数据Job
            JobKey jobKey = new JobKey("ImportReportData");
            var trigger = TriggerBuilder.Create()
                .WithDescription("导入每月报表")
                .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("IPEasySetting:ScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                .Build();


            _ = QuartzUtil.Add(typeof(ImportReportJob), jobKey, trigger);
            #endregion

            #region 每月获取绩效数据Job
            JobKey jobProject = new JobKey("ImportProject");
            var triggerProject = TriggerBuilder.Create()
                .WithDescription("同步专案信息")
                .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("ImportProjectScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                .Build();


            _ = QuartzUtil.Add(typeof(ImportProjectInfoJob), jobProject, triggerProject);
            #endregion

            #region 每天更新绩效数据
            JobKey jobKey1 = new JobKey("UpdateSchedule");
            var trigger1 = TriggerBuilder.Create()
                .WithDescription("更新绩效数据")
                .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("UpdateScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                .Build();


            _ = QuartzUtil.Add(typeof(UpdateJXDataFromIPEasyJob), jobKey1, trigger1);
            #endregion
            #region 疑似绩效数据通知
            JobKey jobKey2 = new JobKey("InvalidData");
            var trigger2 = TriggerBuilder.Create()
                .WithDescription("疑似绩效数据通知")
                .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("InvalidDataScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                .Build();


            _ = QuartzUtil.Add(typeof(InvalidDataMessageJob), jobKey2, trigger2);
            #endregion

            #region 通知代理人反馈
            JobKey jobKey3 = new JobKey("AgentMessage");
            var trigger3 = TriggerBuilder.Create()
                .WithDescription("通知代理人开始反馈")
                .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("AgentMessageScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                .Build();

            _ = QuartzUtil.Add(typeof(AgentMessageJob), jobKey3, trigger3);
            #endregion

            #region 每天计算绩效数据
            JobKey jobCalJX = new JobKey("jobCalJX");
            var triggerCalJX = TriggerBuilder.Create()
                .WithDescription("绩效数据计算")
                .WithSchedule(CronScheduleBuilder.CronSchedule("0 30 5 * * ?").WithMisfireHandlingInstructionDoNothing())
                .Build();

            _ = QuartzUtil.Add(typeof(CalJXJob), jobCalJX, triggerCalJX);
            #endregion

            host.Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

        
    }
}
