using CommunityToolkit.Mvvm.ComponentModel;

namespace ConeCalorimeter.ViewModels;

public sealed class ReportFieldViewModel : ObservableObject
{
    private string _value;
    private bool _isAutoFilled;
    private bool _isSettingAutoValue;

    public ReportFieldViewModel(string key, string label, string value = "")
    {
        Key = key;
        Label = label;
        _value = value;
    }

    public string Key { get; }

    public string Label { get; }

    public string Value
    {
        get => _value;
        set
        {
            if (SetProperty(ref _value, value) && !_isSettingAutoValue)
            {
                _isAutoFilled = false;
            }
        }
    }

    public void SetAutoValue(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return;
        }

        if (!string.IsNullOrWhiteSpace(Value) && !_isAutoFilled)
        {
            return;
        }

        _isSettingAutoValue = true;
        Value = value;
        _isSettingAutoValue = false;
        _isAutoFilled = true;
    }
}
