using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace AIPractice.Shared.Labels
{
///
/// All labels in this group can be multi-selected.
///
public class MultiSelectionLabelGroup:LabelGroup
{
public MultiSelectionLabelGroup(string id, string title, GroupType type, IEnumerable labels) : base(
id, title, type, labels)
{
}
public static MultiSelectionLabelGroup FromXml(string xmlContent)
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
{
using (var r = XmlReader.Create(ms))
{
r.Read();
var id = r.GetAttribute("id");
var title = r.GetAttribute("title");
var type = (GroupType) int.Parse(r.GetAttribute("type") ?? throw new InvalidOperationException());
var labels = new List();
while (r.NodeType == XmlNodeType.Element &&
(r.Name == nameof(DescriptionLabel) ||
r.Name == nameof(ConclusionLabel)) || r.Read())
{
if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(MultiSelectionLabelGroup))
{
break;
}
if (r.Name == nameof(DescriptionLabel))
{
labels.Add(DescriptionLabel.FromXml(r.ReadOuterXml()));
}
if (r.Name == nameof(ConclusionLabel))
{
labels.Add(ConclusionLabel.FromXml(r.ReadOuterXml()));
}
}
return new MultiSelectionLabelGroup(id, title, type, labels);
}
}
}
public override string ToXml()
{
var sb = new StringBuilder();
sb.Append($"<{nameof(MultiSelectionLabelGroup)} id=\"{Id}\" title=\"{Title}\" type=\"{(int)Type}\">");
foreach (var label in Labels)
{
sb.Append(label.ToXml());
}
sb.Append($"{nameof(MultiSelectionLabelGroup)}>");
return sb.ToString();
}
}
}