using adas_core.Domain.Enums;
using adas_core.Domain.Models.MongoModels;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace adas_core.Domain.Models;

/// <summary>
/// Represents a patient observation, extending the base patient observation value functionality.
/// </summary>
/// <remarks>
/// This class serves as a concrete implementation of patient observation data, inheriting common observation value behavior from <see cref="BasePatientObservationValue"/>.
/// </remarks>
public class PatientObservation : BasePatientObservationValue
{
    private string? _result;

    public PatientObservation()
    {
    }

    public PatientObservation(ObjectId patientId, object value, string? name)
    {
        PatientId = patientId;
        Value = value;
        Name = name;
    }

    public double? Max { get; set; }

    public double? Min { get; set; }

    public double? MaxWarn { get; set; }

    public double? MinWarn { get; set; }

    public string? WarnColor { get; set; }

    public string? AlertColor { get; set; }

    public bool ShowOnExpired { get; set; } = true;
    public ObservationEnum.InsertMode InsertMode { get; set; } = ObservationEnum.InsertMode.Auto;

    public bool? Persist { get; set; }

    public string? ColorOnExpired { get; set; }

    //MSH Time of HL7
    public DateTime MessageTime { get; set; }

    public string? Result
    {
        get => _result;
        set => _result = value == "F" ? null : value;
    }

    /// <summary>
    /// Returns a string representation of the <see cref="PatientObservation"/>, including its value, optional min/max/warning thresholds, result, status, expiration, and persistence settings. Optional properties are only included in the output when they have non-null or, for strings, non-empty values.
    /// </summary>
    /// <returns>A formatted string in the form <c>PatientObservation[property1: value1, ...]</c> containing the populated properties of the observation.</returns>
    public override string ToString()
    {
        var items = ToListString();
        items.Add($"value: {Value}");
        if (Max != null) items.Add($"max: {Max}");
        if (Min != null) items.Add($"min: {Min}");
        if (MaxWarn != null) items.Add($"maxWarn: {MaxWarn}");
        if (MinWarn != null) items.Add($"minWarn: {MinWarn}");
        if (!string.IsNullOrEmpty(Result)) items.Add($"result: {Result}");
        items.Add($"status: {Status}");
        if (Expires != null) items.Add($"expires: {Expires}");
        if (!ShowOnExpired) items.Add($"showOnExpired: {ShowOnExpired}");
        if (ColorOnExpired != null) items.Add($"colorOnExpired: {ColorOnExpired}");
        if (Persist != null) items.Add($"persist: {Persist}");
        return "PatientObservation[" + string.Join(", ", items) + "]";
    }

    /// <summary>
    /// Creates a copy of the current <see cref="PatientObservation"/> instance by performing a shallow copy via MemberwiseClone and casting the cloned object back to <see cref="PatientObservation"/>.
    /// </summary>
    /// <returns>A <see cref="PatientObservation"/> instance that is a shallow copy of the current object.</returns>
    public PatientObservation DeepCopy()
    {
        return (PatientObservation)MemberwiseClone();
    }


    #region calculated properties

    public StatusEnum.Type Status { get; set; } = StatusEnum.Type.Ok;

    public int Level { get; set; }

    public int? Expires { get; set; } //seconds


    [BsonDefaultValue(false)] public bool Expired { get; set; }

    public Dictionary<string, object>? UiConfiguration { get; set; }

    public AlarmConfig? Alarm { get; set; }

    #endregion
}