using Hotline.Caching.Interfaces;
using Hotline.JudicialManagement;
using Hotline.Orders;
using Hotline.Realtimes;
using Hotline.Settings;
using Hotline.Share.Dtos.JudicialManagement;
using Hotline.Share.Enums.Order;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using XF.Domain.Constants;
using XF.Domain.Repository;

namespace Hotline.Application.Bigscreen
{
    internal class EnforcementDataScreenRefreshService : BackgroundService
    {
        private readonly IServiceScopeFactory _serviceScopeFactory;

        public EnforcementDataScreenRefreshService(IServiceScopeFactory serviceScopeFactory)
        {
            _serviceScopeFactory = serviceScopeFactory;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using var scope = _serviceScopeFactory.CreateScope();
            var realtimeService = scope.ServiceProvider.GetRequiredService<IRealtimeService>();
            var _enforcementOrdersRepository = scope.ServiceProvider.GetRequiredService<IRepository<EnforcementOrders>>();
            var _mapper = scope.ServiceProvider.GetRequiredService<IMapper>();
            var systemSettingCacheManager = scope.ServiceProvider.GetRequiredService<ISystemSettingCacheManager>();

            int times = int.Parse(systemSettingCacheManager.GetSetting(SettingConstants.EnforcementDataOrderChangedTimes)?.SettingValue[0]);
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {

                    var list = await _enforcementOrdersRepository.Queryable()
                  .LeftJoin<Order>((x, o) => x.Id == o.Id)
                  .LeftJoin<SystemArea>((x, o, p) => o.AreaCode == p.Id)
                  .Where((x, o, p) => o.Id != null)
                  .Where((x, o) => o.Status > EOrderStatus.WaitForAccept && o.StartTime.Value.Date == DateTime.Now.Date)
                  .Select((x, o, p) => new OrderOverviewDto
                  {
                      Id = o.Id,
                      WorkflowId = o.WorkflowId,
                      SourceChannel = o.SourceChannel,
                      SourceChannelCode = o.SourceChannelCode,
                      Title = o.Title,
                      AcceptType = o.AcceptType,
                      AcceptTypeCode = o.AcceptTypeCode,
                      HotspotId = o.HotspotId,
                      HotspotName = o.HotspotName,
                      ActualHandleOrgName = o.ActualHandleOrgName,
                      ActualHandleOrgCode = o.ActualHandleOrgCode,
                      Status = o.Status,
                      ActualHandleTime = o.ActualHandleTime,
                      ExpiredTime = o.ExpiredTime,
                      NearlyExpiredTime = o.NearlyExpiredTime,
                      StartTime = o.StartTime,
                      AreaName = p.AreaName
                  })
                  .MergeTable()
                  .OrderByDescending(d => d.StartTime)
                  .Take(50)
                  .ToListAsync();

                    var orderlist = _mapper.Map<List<OrderOverviewDto>>(list);

                    await realtimeService.EnforcementOrderHandlingDetailAsync(orderlist, stoppingToken);
                }
                catch { }

                await Task.Delay(times);
            }
        }
    }
}
