using Dapper;
using EVCB_OCPP.Domain;
using EVCB_OCPP.Domain.ConnectionFactory;
using EVCB_OCPP.Packet.Features;
using EVCB_OCPP.Packet.Messages.Core;
using EVCB_OCPP.WSServer.Dto;
using EVCB_OCPP.WSServer.Helper;
using EVCB_OCPP.WSServer.Message;
using EVCB_OCPP.WSServer.Service;
using EVCB_OCPP.WSServer.Service.WsService;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace EVCB_OCPP.WSServer.Jobs;

[DisallowConcurrentExecution]
public class ServerSetFeeJob : IJob
{
    private readonly ProtalServer protalServer;
    private readonly ServerMessageService messageService;
    private readonly ISqlConnectionFactory<WebDBConetext> webDbConnectionFactory;
    private readonly WebDbService webDbService;
    private readonly IMainDbService mainDbService;
    private readonly ILogger<ServerSetFeeJob> logger;
    //private readonly string webConnectionString;

    public ServerSetFeeJob(
        ProtalServer protalServer,
        ServerMessageService messageService,
        //IConfiguration configuration,
        ISqlConnectionFactory<WebDBConetext> sqlConnectionFactory,
        WebDbService webDbService,
        IMainDbService mainDbService,
        ILogger<ServerSetFeeJob> logger)
    {
        this.protalServer = protalServer;
        this.messageService = messageService;
        this.webDbConnectionFactory = sqlConnectionFactory;
        this.webDbService = webDbService;
        this.mainDbService = mainDbService;
        this.logger = logger;
        //this.webConnectionString = configuration.GetConnectionString("WebDBContext");
    }

    public async Task Execute(IJobExecutionContext context)
    {
        //logger.LogDebug("{0} Started", nameof(ServerSetFeeJob));
        //BasicMessageHandler msgAnalyser = new BasicMessageHandler();
        Dictionary<string, WsClientData> _copyClientDic = protalServer.GetClientDic();
        //using var db = maindbContextFactory.CreateDbContextAsync();
        foreach (var item in _copyClientDic)
        {
            try
            {
                WsClientData session = item.Value;
                if (!session.IsCheckIn)
                {
                    continue;
                }

                string displayPriceText = await webDbService.SetDefaultFee(session);
                if (string.IsNullOrEmpty(displayPriceText) || displayPriceText == session.DisplayPrice)
                {
                    continue;
                }

                protalServer.UpdateClientDisplayPrice(item.Key, displayPriceText);

                await messageService.SendChangeConfigurationRequest(
                    session.ChargeBoxId, key: "DefaultPrice", value: displayPriceText);

                if (session.CustomerId == new Guid("10C7F5BD-C89A-4E2A-8611-B617E0B41A73"))
                {
                    await messageService.SendChangeConfigurationRequest(
                        session.ChargeBoxId, key: "ConnectionTimeOut", value: "120");

                    await messageService.SendChangeConfigurationRequest(
                        session.ChargeBoxId, key: "MeterValueSampleInterval", value: "3");
                }
            }
            catch (Exception ex)
            {
                logger.LogError("ServerSetFeeTrigger ChargeBoxId:{0}  Ex:{1}", item.Key, ex.ToString());
            }
        }
    }
}
