using adas_core.Application.Services.Interfaces;
using adas_core.Domain.Enums;
using adas_core.Domain.Models;
using adas_core.Domain.Models.Pumps;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;

namespace adas_core.Application.Customizations.BD;

public class CalculatedObservations(IServiceProvider serviceProvider) : ICalculatedObservations
{
    private readonly ILogger<CalculatedObservations> _logger =
        serviceProvider.GetRequiredService<ILogger<CalculatedObservations>>();

    private readonly Lazy<IObservationService> _observationService =
        serviceProvider.GetRequiredService<Lazy<IObservationService>>();

    public Task<T?> Map<T>(T obs, bool onlyByName = false) where T : BasePatientObservation
    {
        return Task.FromResult(obs)!;
    }

    public Task<PatientTreatment> Map(PatientTreatment treatment)
    {
        return Task.FromResult(treatment);
    }

    public Task<PatientDiagnosis> Map(PatientDiagnosis diagnosis)
    {
        return Task.FromResult(diagnosis);
    }

    public async Task<PumpObservation> Map(PumpObservation pumpObservation)
    {
        if (pumpObservation.Code == "IHE PCD-04")
            await CreatePumpAlarmObservation(pumpObservation);
        return pumpObservation;
    }

    public Task CalculateMedicineObservation(List<Medicine> activeMedicines, ObjectId patientId)
    {
        return Task.CompletedTask;
    }

    public Task CalculateActiveBolus(ObjectId patientId)
    {
        return Task.CompletedTask;
    }

    public Task<IEnumerable<PatientTreatment?>> GetActiveTreatmentsByPatient(ObjectId id)
    {
        throw new NotImplementedException();
    }

    public Task<PatientObservation?> FixTimeInconsistencyWithLast(PatientObservation newObservation)
    {
        throw new NotImplementedException();
    }

    public Task<List<PatientObservation>> PreMapList(List<PatientObservation> listToInsert)
    {
        return Task.FromResult(listToInsert);
    }

    public Task<PatientObservation> MapSourceAlarm(PatientObservation obs, PatientObservationAlarm alarmToInsert)
    {
        throw new NotImplementedException();
    }

    public Task SendAlarm(PatientObservation obs, string name, AlarmEnum.Name? code)
    {
        throw new NotImplementedException();
    }

    private Task CreatePumpAlarmObservation(PumpObservation pumpObservation)
    {
        PatientObservation patientObservationAlarm = new()
        {
            CodingSystem = "ADAS_ALARM",
            Code = pumpObservation.AlarmState,
            Name = $"Alarm_Pump_{pumpObservation.AlarmType}",
            Value = pumpObservation.DeviceId != null
                ? $"Device Id: {pumpObservation.DeviceId}"
                : $"Infusion Id {pumpObservation.InfusionId}",
            Time = pumpObservation.Time
        };
        
        if (pumpObservation.PatientId.HasValue) patientObservationAlarm.PatientId = pumpObservation.PatientId.Value;
        

        //CheckAlarmConfig(patientObservationAlarm);
        try
        {
            _ = _observationService.Value.InsertObservation(patientObservationAlarm, mapObs: false);
        }
        catch (Exception e)
        {
            _logger.LogError("Error inserting Pump Observation Alarm. Exception: {e}", e);
        }

        return Task.CompletedTask;
    }
}