using EVCB_OCPP.Domain;
using EVCB_OCPP.Packet.Messages.RemoteTrigger;
using EVCB_OCPP.WSServer.Message;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OCPPServer.Protocol;
using Quartz;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EVCB_OCPP.WSServer.Jobs;

[DisallowConcurrentExecution]
public class ServerUpdateJob : IJob
{
    private readonly ProtalServer protalServer;
    private readonly IDbContextFactory<MainDBContext> maindbContextFactory;
    private readonly ILogger<ServerUpdateJob> logger;

    public ServerUpdateJob(
        ProtalServer protalServer, 
        IDbContextFactory<MainDBContext> maindbContextFactory,
        ILogger<ServerUpdateJob> logger)
    {
        this.protalServer = protalServer;
        this.maindbContextFactory = maindbContextFactory;
        this.logger = logger;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        //logger.LogDebug("{0} Started", nameof(ServerUpdateJob));
        BasicMessageHandler msgAnalyser = new BasicMessageHandler();
        Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
        var checkUpdateDt = DateTime.UtcNow;
        using (var db = maindbContextFactory.CreateDbContext())
        {
            //var needUpdateChargers = db.Machine.Where(x => x.FW_AssignedMachineVersionId.HasValue == true &&
            //    x.FW_AssignedMachineVersionId != x.FW_VersionReport && x.Online == true)
            //    .Select(x => new { x.Id, x.ChargeBoxId, x.FW_AssignedMachineVersionId }).ToList();

            var needUpdateChargers = db.Machine.Where(x => x.FW_AssignedVersion.HasValue == true &&
              x.FW_AssignedVersion != x.FW_VersionReport && x.Online == true)
              .Select(x => x.ChargeBoxId).AsNoTracking().ToList();

            foreach (var chargeBoxId in needUpdateChargers)
            {
                try
                {
                    ClientData session;
                    if (_copyClientDic.TryGetValue(chargeBoxId, out session))
                    {

                        string requestId = Guid.NewGuid().ToString();
                        // using (var db = maindbContextFactory.CreateDbContext())

                        if (session.IsCheckIn && !session.ISOCPP20)
                        {

                            var _request = new TriggerMessageRequest()
                            {
                                requestedMessage = Packet.Messages.SubTypes.MessageTrigger.FirmwareStatusNotification
                            };

                            var uuid = session.queue.store(_request);
                            string rawRequest = BasicMessageHandler.GenerateRequest(uuid, _request.Action, _request);
                            protalServer.SendMsg(session, rawRequest, string.Format("{0} {1}", _request.Action, "Request"), "");

                            #region OCTT   ,測試韌體更新方式
                            //--------------------> OCTT   ,測試韌體更新方式
                            //{
                            //    var machine = db.Machine.Where(x => x.FW_AssignedMachineVersionId.HasValue == true &&
                            //        x.FW_AssignedMachineVersionId != x.FW_VersionReport && x.ChargeBoxId == session.ChargeBoxId)
                            //        .Select(x => new { x.Id, x.FW_AssignedMachineVersionId }).FirstOrDefault();

                            //    if (machine != null)
                            //    {
                            //        var mv = db.MachineVersion.Include(c => c.PublishVersion)
                            //         .Include(c => c.PublishVersion.PublishVersionFiles)
                            //         .Include(c => c.PublishVersion.PublishVersionFiles.Select(z => z.UploadFile))
                            //         .Where(c => c.Id == machine.FW_AssignedMachineVersionId.Value).First();

                            //        string downloadUrl = mv.PublishVersion.PublishVersionFiles.FirstOrDefault().UploadFile.FileUrl;

                            //        var _updateFWrequest = new UpdateFirmwareRequest()
                            //        {
                            //            location = new Uri(downloadUrl),
                            //            retries = 3,
                            //            retrieveDate = DateTime.UtcNow,
                            //            retryInterval = 10
                            //        };
                            //        db.MachineOperateRecord.Add(new MachineOperateRecord()
                            //        {
                            //            CreatedOn = DateTime.UtcNow,
                            //            ChargeBoxId = session.ChargeBoxId,
                            //            SerialNo = requestId,
                            //            RequestContent = JsonConvert.SerializeObject(_updateFWrequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
                            //            EVSE_Status = 0,
                            //            EVSE_Value = "Fw Version:" + machine.FW_AssignedMachineVersionId,
                            //            Status = 0,
                            //            RequestType = 0,

                            //        });

                            //        db.ServerMessage.Add(new ServerMessage()
                            //        {
                            //            ChargeBoxId = session.ChargeBoxId,
                            //            CreatedBy = "Server",
                            //            CreatedOn = DateTime.UtcNow,
                            //            OutAction = _updateFWrequest.Action.ToString(),
                            //            OutRequest = JsonConvert.SerializeObject(_updateFWrequest, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
                            //            SerialNo = requestId,
                            //            InMessage = string.Empty

                            //        });

                            //        db.SaveChanges();

                            //    }

                            //}
                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError("serverUpdateTrigger ChargeBoxId:{0}  Ex:{1}", chargeBoxId, ex.ToString());
                }
            }
        }
    }
}
