using EasyCaching.Core;
using Hangfire;
using Hangfire.Storage;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Application.Service;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace IRaCIS.Application.Services.BackGroundJob
{

    public interface IIRaCISHangfireJob
    {

        Task MemoryCacheTrialStatusAsync();

        Task InitHangfireJobTaskAsync();

    }
    public class IRaCISCHangfireJob : IIRaCISHangfireJob
    {
        public static string JsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources);

        private readonly IRepository<Trial> _trialRepository;
        private readonly IEasyCachingProvider _provider;
        private readonly ILogger<IRaCISCHangfireJob> _logger;

        private readonly IRepository<TrialEmailNoticeConfig> _trialEmailNoticeConfigRepository;
        private readonly IRepository<Internationalization> _internationalizationRepository;



        public IRaCISCHangfireJob(IRepository<Trial> trialRepository, ILogger<IRaCISCHangfireJob> logger, IEasyCachingProvider provider, IRepository<TrialEmailNoticeConfig> trialEmailNoticeConfigRepository, IRepository<Internationalization> internationalizationRepository)
        {
            _trialRepository = trialRepository;
            _provider = provider;
            _logger = logger;
            _trialEmailNoticeConfigRepository = trialEmailNoticeConfigRepository;
            _internationalizationRepository = internationalizationRepository;
        }

        public async Task InitHangfireJobTaskAsync()
        {
            _logger.LogInformation("项目启动 hangfire 任务初始化 执行开始~");


            //项目状态 立即加载到缓存中
            await MemoryCacheTrialStatusAsync();


            //创建项目缓存 定时任务
            HangfireJobHelper.AddOrUpdateInitCronJob<IIRaCISHangfireJob>("RecurringJob_Cache_TrialState", t => t.MemoryCacheTrialStatusAsync(), Cron.Daily());

            //初始化

            await InitInternationlizationDataAndWatchJsonFileAsync();

            //创建邮件定时任务
            await InitSysAndTrialCronJobAsync();


            _logger.LogInformation("项目启动 hangfire 任务初始化 执行结束");
        }

        /// <summary>
        /// 缓存项目状态
        /// </summary>
        /// <returns></returns>
        public async Task MemoryCacheTrialStatusAsync()
        {

            var list = await _trialRepository.Select(t => new { TrialId = t.Id, TrialStatusStr = t.TrialStatusStr })
                 .ToListAsync();

            list.ForEach(t => _provider.Set(t.TrialId.ToString(), t.TrialStatusStr, TimeSpan.FromDays(7)));

        }


        #region 国际化 初始化


        public async Task InitInternationlizationDataAndWatchJsonFileAsync()
        {
            //查询数据库的数据
            var toJsonList = await _internationalizationRepository.Where(t => t.InternationalizationType == 1).Select(t => new
            {
                t.Code,
                t.Value,
                t.ValueCN
            }).ToListAsync();

            //组织成json 文件

            var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json);
            var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json);


            //本地静态文件国际化需要
            foreach (var tojsonItem in toJsonList)
            {
                StaticData.En_US_Dic[tojsonItem.Code] = tojsonItem.Value;
                StaticData.Zh_CN_Dic[tojsonItem.Code] = tojsonItem.ValueCN;
            }

            File.WriteAllText(usJsonPath, JsonConvert.SerializeObject(StaticData.En_US_Dic));
            File.WriteAllText(cnJsonPath, JsonConvert.SerializeObject(StaticData.Zh_CN_Dic));


            //监测Json文件变更 实时刷新数据
            WatchJsonFile(usJsonPath);
            WatchJsonFile(cnJsonPath);

        }

        public void WatchJsonFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new BusinessValidationFailedException(StaticData.International("IRaCISCHangfireJob_FileNotFound"));
            }

            FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath));
            watcher.Changed += (sender, e) => LoadJsonFile(filePath);
            watcher.EnableRaisingEvents = true;

        }


        private void LoadJsonFile(string filePath)
        {

            IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile(filePath);

            IConfigurationRoot enConfiguration = builder.Build();

            foreach (IConfigurationSection section in enConfiguration.GetChildren())
            {
                if (filePath.Contains(StaticData.En_US_Json))
                {
                    StaticData.En_US_Dic[section.Key] = section.Value;

                }
                else
                {
                    StaticData.Zh_CN_Dic[section.Key] = section.Value;
                }
            }
        }

        #endregion

        public async Task InitSysAndTrialCronJobAsync()
        {
            //var deleteJobIdList = await _trialEmailNoticeConfigRepository.Where(t => t.Trial.TrialStatusStr != StaticData.TrialState.TrialOngoing && t.EmailCron != string.Empty && t.IsAutoSend)
            //   .Select(t => t.TrialId + "_" + t.Id)
            //    .ToListAsync();

            //foreach (var jobId in deleteJobIdList)
            //{
            //    HangfireJobHelper.RemoveCronJob(jobId);
            //}


            var taskInfoList = await _trialEmailNoticeConfigRepository.Where(t => t.Trial.TrialStatusStr == StaticData.TrialState.TrialOngoing && t.EmailCron != string.Empty && t.IsAutoSend)
                  .Select(t => new { t.Id, t.Code, t.EmailCron, t.BusinessScenarioEnum, t.TrialId })
                  .ToListAsync();

            foreach (var task in taskInfoList)
            {
                //利用主键作为任务Id
                var jobId = $"{task.TrialId}_{task.Id}";

                HangfireJobHelper.AddOrUpdateTrialCronJob(jobId, task.TrialId, task.BusinessScenarioEnum, task.EmailCron);
            }

            var addOrUpdateJobIdList = taskInfoList.Select(t => $"{t.TrialId}_{t.Id}").ToList();

            var list = JobStorage.Current.GetConnection().GetRecurringJobs().ToList();

            //项目定时任务都在default 队列
            //var dbJobIdList = JobStorage.Current.GetConnection().GetRecurringJobs().Where(t => t.Queue == "default").Select(t => t.Id).ToList();

            //var deleteList=  dbJobIdList.Except(addOrUpdateJobIdList).ToList();

            //  foreach (var jobId in deleteList)
            //  {
            //      HangfireJobHelper.RemoveCronJob(jobId);
            //  }


        }
    }


}
