using System.Text.Json.Serialization; namespace AIStudio.Assistants.BeyondFactsLab; /// /// What the person entered into the form "Your details" of one step, stored as form.json in the folder of the step. /// /// /// The values are stored under the IDs of the fields of BflFieldCatalog. A value of a field this version does not /// know, e.g., one a newer version added, stays as it is and is written back unchanged. Version 2 added the card sort, /// version 3 the simulation. /// public sealed class BflStepFormData { /// /// The version of the stored form this version of AI Studio writes. Raise it with every new property. /// public const int CURRENT_SCHEMA_VERSION = 3; /// /// The character which separates the options of a field with several choices. /// private const char OPTION_SEPARATOR = ','; /// /// Gets or sets the version of the stored form. /// public int SchemaVersion { get; set; } = CURRENT_SCHEMA_VERSION; /// /// Gets or sets the values, by the IDs of their fields. /// public Dictionary Values { get; set; } = new(StringComparer.Ordinal); /// /// Gets or sets the card sort of the step "Values and perspectives", see BflCardSort. /// /// /// Null for every other step, and until the person sorts a value there; the file then leaves it out. /// [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public BflCardSort? CardSort { get; set; } /// /// Gets or sets whom the chat of the step "Test situation" plays, see BflSimulation. /// /// /// Null for every other step, and until the person chooses a mode there; the file then leaves it out. /// [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public BflSimulation? Simulation { get; set; } /// /// Gets the value of a field, or an empty string when the field has none. /// /// The ID of the field. public string Get(string fieldId) => this.Values.GetValueOrDefault(fieldId, string.Empty); /// /// Sets the value of a field; an empty value removes it. /// /// The ID of the field. /// The value. public void Set(string fieldId, string? value) { if (string.IsNullOrWhiteSpace(value)) this.Values.Remove(fieldId); else this.Values[fieldId] = value; } /// /// Gets the options chosen in a field with several choices. /// /// The ID of the field. public IReadOnlySet GetChoices(string fieldId) => this.Get(fieldId).Split(OPTION_SEPARATOR, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToHashSet(StringComparer.Ordinal); /// /// Sets the options chosen in a field with several choices. /// /// The field. /// The IDs of the chosen options. public void SetChoices(BflFieldDefinition field, IEnumerable optionIds) { var chosen = optionIds.ToHashSet(StringComparer.Ordinal); // Stored in the order of the options, so that the same choice always gives the same value: this.Set(field.Id, string.Join(OPTION_SEPARATOR, field.Options.Select(option => option.Id).Where(chosen.Contains))); } }