using Common;
using Commonlib;
using Newtonsoft.Json;
using NLog;
using Quartz;
using RestSharp;

namespace BLW_Log.services
{
    public class ShengJiCaoZuoB : IJob
    {
        public static int ShengJiCaoZuoBCount = 0;
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();
        private readonly Dictionary<string, IRestClient> _clients;
        private readonly IConfiguration configuration;

        public ShengJiCaoZuoB(Dictionary<string, IRestClient> clients, IConfiguration configuration)
        {
            _clients = clients;
            this.configuration = configuration;
        }

        async public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var timer_es = configuration["ShengJi:JianGe"];
                var RoomTypeID_FirstVersion = configuration["ShengJi:RoomTypeID_FirstVersion"];
                var First_GuJian_Name = configuration["ShengJi:First_GuJian_Name"];
                var RoomTypeID_SecondVerrsion = configuration["ShengJi:RoomTypeID_SecondVerrsion"];
                var Second_GuJian_Name = configuration["ShengJi:Second_GuJian_Name"];
                var HostIdList = configuration["ShengJi:HostIdList"];
                int jiange = int.Parse(timer_es);

                var client = _clients["NoStopShengJi"];
                string Url = "api/WebChatUpgrade";
                var request = new RestRequest(Url);
                //2438
                //研发测试房型_1
                //固件:BLV-C5 || Launcher_C1F_V04 || C1F_A_L4_41_250605_NM.bin
                //request.AddParameter("roomTypeID", "2438");
                //request.AddParameter("hostid_lists", "[24540]");
                //request.AddParameter("upgradefileName", "C1F_A_L4_41_250605_NM.bin");

                request.AddParameter("roomTypeID", RoomTypeID_SecondVerrsion);
                request.AddParameter("hostid_lists", HostIdList);
                request.AddParameter("upgradefileName", Second_GuJian_Name);

                _logger.Error("ShengJiCaoZuo 研发测试房型_1 开始：");
                var response = await client.ExecutePostAsync(request);
                _logger.Error("ShengJiCaoZuo 研发测试房型_1 执行结果：" + response.Content);

                ShengJiCaoZuoBCount = ShengJiCaoZuoBCount + 1;
                if (ShengJiCaoZuoBCount >= 2)
                {

                    // 直接启动JobA
                    var jobA = JobBuilder.Create<ShengJiCaoZuoA>().Build();
                    var triggerA = TriggerBuilder.Create()
                        .StartAt(DateTimeOffset.Now.AddMinutes(jiange))
                        .Build();

                    await context.Scheduler.ScheduleJob(jobA, triggerA);
                }
                else
                {
                    // 直接启动JobB
                    var jobB = JobBuilder.Create<ShengJiCaoZuoB>().Build();
                    var triggerB = TriggerBuilder.Create()
                        .StartAt(DateTimeOffset.Now.AddMinutes(jiange))
                        .Build();

                    await context.Scheduler.ScheduleJob(jobB, triggerB);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("ShengJiCaoZuoB 执行失败：" + ex.Message);
            }
        }
    }
    public class ShengJiCaoZuoA : IJob
    {
        public static int ShengJiCaoZuoACount = 0;
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();
        public readonly IConfiguration configuration;
        private readonly Dictionary<string, IRestClient> _clients;

        public ShengJiCaoZuoA(Dictionary<string, IRestClient> clients, IConfiguration _configuration)
        {
            _clients = clients;
            this.configuration = _configuration;
        }
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {

                var timer_es = configuration["ShengJi:JianGe"];
                var RoomTypeID_FirstVersion = configuration["ShengJi:RoomTypeID_FirstVersion"];
                var First_GuJian_Name = configuration["ShengJi:First_GuJian_Name"];
                var RoomTypeID_SecondVerrsion = configuration["ShengJi:RoomTypeID_SecondVerrsion"];
                var Second_GuJian_Name = configuration["ShengJi:Second_GuJian_Name"];
                var HostIdList = configuration["ShengJi:HostIdList"];
                int jiange = int.Parse(timer_es);

                var client = _clients["NoStopShengJi"];
                string Url = "api/WebChatUpgrade";
                var request = new RestRequest(Url);


                //1412
                //固件:BLV-V9 || Launcher_C1F_V04 || C1F_A_L4_31_240723_NM.bin
                //100331-YHF_T
                //YHF_T

                //2438
                //研发测试房型_1
                //固件:BLV-C5 || Launcher_C1F_V04 || C1F_A_L4_41_250605_NM.bin
                //request.AddParameter("roomTypeID", "1412");
                //request.AddParameter("hostid_lists", "[24540]");
                //request.AddParameter("upgradefileName", "C1F_A_L4_31_240723_NM.bin");

                request.AddParameter("roomTypeID", RoomTypeID_FirstVersion);
                request.AddParameter("hostid_lists", HostIdList);
                request.AddParameter("upgradefileName", First_GuJian_Name);
                _logger.Error("ShengJiCaoZuo YHF_T 开始:");
                var response = await client.ExecutePostAsync(request);
                _logger.Error("ShengJiCaoZuo YHF_T 执行成功：" + response.Content);


                ShengJiCaoZuoACount = ShengJiCaoZuoACount + 1;

                if (ShengJiCaoZuoACount >= 2)
                {
                    var jobB = JobBuilder.Create<ShengJiCaoZuoB>().Build();
                    var triggerB = TriggerBuilder.Create()
                        .StartAt(DateTimeOffset.Now.AddMinutes(jiange))
                        .Build();

                    await context.Scheduler.ScheduleJob(jobB, triggerB);
                }
                else
                {
                    var jobA = JobBuilder.Create<ShengJiCaoZuoA>().Build();
                    var triggerA = TriggerBuilder.Create()
                        .StartAt(DateTimeOffset.Now.AddMinutes(jiange))
                        .Build();
                    await context.Scheduler.ScheduleJob(jobA, triggerA);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("ShengJiCaoZuoA 执行失败：" + ex.Message);
            }
        }
    }
}
