using EVCB_OCPP.Domain;
using EVCB_OCPP.Domain.Models.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OCPPServer.Protocol;
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,
        ILogger<HeartBeatCheckJob> logger)
    {
        this.protalServer = protalServer;
        this.maindbContextFactory = maindbContextFactory;
        this.logger = logger;
    }

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

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

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

            watch.Start();

            using (var db = maindbContextFactory.CreateDbContext())
            {
                foreach (var session in clients)
                {
                    var machine = new Machine() { Id = session.MachineId };
                    if (machine != null)
                    {
                        //db.Configuration.AutoDetectChangesEnabled = false;
                        //db.Configuration.ValidateOnSaveEnabled = false;
                        db.Machine.Attach(machine);
                        machine.HeartbeatUpdatedOn = DateTime.UtcNow;
                        machine.ConnectionType = session.UriScheme.Equals("wss") ? 2 : 1;
                        db.Entry(machine).Property(x => x.HeartbeatUpdatedOn).IsModified = true;
                        db.Entry(machine).Property(x => x.ConnectionType).IsModified = true;
                        db.SaveChanges();
                        db.ChangeTracker.Clear();
                    }
                }
            }

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