using adas_core.Domain.Models.MongoModels;
using adas_core.Domain.Utils;

namespace adas_core.Domain.Models.Observations;

/// <summary>
/// Represents a medication-related observation, extending the base <see cref="Observation"/> type.
/// </summary>
/// <remarks>
/// Serves as a specialized observation for capturing and modeling medication-specific data within the inheritance hierarchy.
/// </remarks>
public class Medication : Observation
{
    public Medication()
    {
    }

    public Medication(string id, Dictionary<string, PumpElement> obj)
    {
        if (obj.TryGetValue("MDC_184504", out var mode)) PumpMode = new PumpMode(mode);

        if (obj.TryGetValue("MDC_184519", out var status)) PumpStatus = new PumpStatus(status);

        if (obj.TryGetValue("MDC_157784", out var flow)) FlowFluid = new FlowFluid(flow);

        if (obj.TryGetValue("MDC_157993", out var fluid)) VolFluidTotal = new VolumeFluidDeliveryTotal(fluid);

        if (obj.TryGetValue("MDCX_PUMP_DELIVERY_PRESSURE", out var pressure))
            PumpDeliveryPressure = new PumpDeliveryPressure(pressure);

        if (obj.TryGetValue("MDC_DEV_PUMP_ACTIVE_SOURCES", out var source))
            PumpActiveSources = new PumpActiveSources(source);

        if (obj.TryGetValue("MDC_184514", out var drug)) Drug = new Drug(drug);

        if (obj.TryGetValue("MDC_157872", out var volume)) VolumeFluidTbiRemain = new VolumeFluidTbiRemain(volume);

        if (obj.TryGetValue("MDC_157916", out var remain)) VolumeFluidTimeRemain = new VolumeFluidTimePdRemain(remain);

        Id = id;
    }

    public string? Id { get; set; }
    public PumpMode? PumpMode { get; set; }
    public PumpStatus? PumpStatus { get; set; }
    public FlowFluid? FlowFluid { get; set; }
    public VolumeFluidDeliveryTotal? VolFluidTotal { get; set; }
    public VolumeFluidTbiRemain? VolumeFluidTbiRemain { get; set; }
    public VolumeFluidTimePdRemain? VolumeFluidTimeRemain { get; set; }
    public PumpDeliveryPressure? PumpDeliveryPressure { get; set; }
    public PumpActiveSources? PumpActiveSources { get; set; }
    public Drug? Drug { get; set; }
}

/// <summary>
/// Represents a value or data entry associated with a drug, such as dosage, concentration, or other pharmaceutical-related information.
/// </summary>
public class DrugValue
{
    public DrugValue()
    {
    }

    public DrugValue(PumpElement pumpElement)
    {
        Id = pumpElement.Id;
        Text = pumpElement.Text;
        Time = pumpElement.Time;
        Units = pumpElement.Units;
        CodingSystem = pumpElement.CodingSystem;
        Result = pumpElement.Result;
    }

    public string? Id { get; set; }
    public string? Text { get; set; }
    public string? Units { get; set; }
    public DateTime? Time { get; set; }
    public string? CodingSystem { get; set; }
    public char? Result { get; set; } = 'R';

    /// <summary>
    /// Copies the values from the specified <see cref="DrugValue"/> instance into the current instance, replacing the existing Id, Text, Units, Time, CodingSystem, and Result properties.
    /// </summary>
    /// <param name="common">The <see cref="DrugValue"/> instance whose property values will be copied to the current instance.</param>
    public void Copy(DrugValue common)
        {
            Id = common.Id;
            Text = common.Text;
            Units = common.Units;
            Time = common.Time;
            CodingSystem = common.CodingSystem;
            Result = common.Result;
        }
}

/// <summary>
/// Represents an abstract base class for drug values that are expressed as double-precision floating-point numbers, providing a common foundation for numerical drug-related data.
/// </summary>
/// <remarks>
/// As an abstract class, it must be inherited by a derived class that supplies the concrete implementation for the double-based drug value behavior.
/// </remarks>
public abstract class DrugDoubleValue : DrugValue
{
    protected DrugDoubleValue(PumpElement pumpElement) : base(pumpElement)
    {
        if (double.TryParse(pumpElement.Value?.ToString(), out var valueParsed))
            Value = valueParsed;
    }

    public double? Value { get; set; }
}

/// <summary>
/// Serves as an abstract base class for drug values represented as strings, extending <see cref="DrugValue"/>.
/// </summary>
/// <remarks>
/// Inherits from <see cref="DrugValue"/> and is intended to be derived by concrete classes that encapsulate string-typed drug data associated with a <see cref="PumpElement"/>.
/// </remarks>
public abstract class DrugStringValue(PumpElement pumpElement) : DrugValue(pumpElement)
{
    public string? Value { get; set; } = pumpElement.Value?.ToString();
}

/// <summary>
/// Represents a double-precision drug value that describes the flow of a fluid for the specified <see cref="PumpElement"/>.
/// </summary>
public class FlowFluid(PumpElement pumpElement) : DrugDoubleValue(pumpElement);

/// <summary>
/// Represents the total volume of fluid delivered as a drug-related double value associated with a specific pump element.
/// </summary>
/// <remarks>
/// Inherits from <see cref="DrugDoubleValue"/>, sharing the pump element context with its base type.
/// </remarks>
public class VolumeFluidDeliveryTotal(PumpElement pumpElement) : DrugDoubleValue(pumpElement);

/// <summary>
/// Represents the delivery pressure of a pump as a drug-related double precision value.
/// </summary>
/// <remarks>
/// Inherits from <see cref="DrugDoubleValue"/> and is constructed with a <see cref="PumpElement"/> instance that is forwarded to the base constructor.
/// </remarks>
public class PumpDeliveryPressure(PumpElement pumpElement) : DrugDoubleValue(pumpElement);

/// <summary>
/// Represents the remaining volume of fluid to be infused (TBI) associated with a pump element, implemented as a drug-related double value.
/// </summary>
public class VolumeFluidTbiRemain(PumpElement pumpElement) : DrugDoubleValue(pumpElement);

/// <summary>
/// Represents the remaining time for volume fluid delivery, derived from a drug double value associated with a pump element.
/// </summary>
/// <remarks>
/// Inherits from <see cref="DrugDoubleValue"/> and is constructed with a <see cref="PumpElement"/> to capture time-related fluid volume metrics.
/// </remarks>
public class VolumeFluidTimePdRemain(PumpElement pumpElement) : DrugDoubleValue(pumpElement);

/// <summary>
/// Represents the active sources of a pump element as a drug string value.
/// Inherits from <see cref="DrugStringValue"/>, encapsulating source information for pump operations.
/// </summary>
public class PumpActiveSources(PumpElement pumpElement) : DrugStringValue(pumpElement)
{
    public string? ValueId { get; set; } = pumpElement.ValueId;
    public string? ValueCodingSystem { get; set; } = pumpElement.ValueCodingSystem;
}

/// <summary>
/// Represents a drug value associated with a pump element, inheriting from the DrugStringValue base class.
/// </summary>
public class Drug(PumpElement pumpElement) : DrugStringValue(pumpElement);

/// <summary>
/// Represents a pump mode as a drug-related string value, inheriting string-based value semantics from <see cref="DrugStringValue"/>.
/// </summary>
public class PumpMode : DrugStringValue
{
    public PumpMode(PumpElement pumpElement) : base(pumpElement)
    {
        Value = pumpElement.Value?.ToString()?.SubstringAfter("pump-mode-").Replace("-", "_");
    }
}

/// <summary>
/// Represents the operational status of a pump as a drug-related string value.
/// </summary>
public class PumpStatus : DrugStringValue
{
    public PumpStatus(PumpElement pumpElement) : base(pumpElement)
    {
        Value = pumpElement.Value?.ToString()?.SubstringAfter("pump-status-").Replace("-", "_");
    }
}