using EVCB_OCPP.Domain;
using EVCB_OCPP.Domain.Models.MainDb;
using EVCB_OCPP.WSServer.Service;
using EVCB_OCPP.WSServer.Service.WsService;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EVCB_OCPP.WSServer.Jobs;

[DisallowConcurrentExecution]
public class HeartBeatCheckJob : IJob
{
    public HeartBeatCheckJob(
        ProtalServer protalServer,
        IDbContextFactory<MainDBContext> maindbContextFactory,
        IMainDbService mainDbService,
        ILogger<HeartBeatCheckJob> logger)
    {
        this.protalServer = protalServer;
        this.maindbContextFactory = maindbContextFactory;
        this.mainDbService = mainDbService;
        this.logger = logger;
    }

    private readonly ProtalServer protalServer;
    private readonly IDbContextFactory<MainDBContext> maindbContextFactory;
    private readonly IMainDbService mainDbService;
    private readonly ILogger<HeartBeatCheckJob> logger;

    public async Task Execute(IJobExecutionContext context)
    {
        //logger.LogDebug("{0} Started", nameof(HeartBeatCheckJob));
        try
        {
            Stopwatch watch = new Stopwatch();
            Dictionary<string, WsClientData> _copyClientDic = protalServer.GetClientDic();

            var cdt = DateTime.UtcNow;
            var clients = _copyClientDic.Where(x => x.Value.LastActiveTime > cdt.AddSeconds(-120)).Select(x => x.Value).ToList();

            watch.Start();

            var datas = new List<Machine>();
            foreach (var session in clients)
            {
                var machine = new Machine() { Id = session.MachineId, HeartbeatUpdatedOn = cdt, ConnectionType = session.UriScheme.Equals("wss") ? 2 : 1 };
                datas.Add(machine);
            }
            await mainDbService.UpdateHeartBeats(datas);

            watch.Stop();
            if (watch.ElapsedMilliseconds / 1000 > 5)
            {
                logger.LogCritical("Update HeartBeatCheckTrigger cost " + watch.ElapsedMilliseconds / 1000 + " seconds.");
            }
        }
        catch (Exception ex)
        {
            logger.LogError("***********************************************************");
            logger.LogError(string.Format("HeartBeatCheckTrigger  Ex:{0}", ex.ToString()));
        }
    }
}
