using System.Xml;

namespace aeqw89.tools.Publish;

internal class PropertyGroup {
    private readonly XmlElement _element;
    public PropertyGroup(XmlElement element) => _element = element;

    public bool HasProperty(string name) => _element.SelectSingleNode($"./{name}") is XmlElement;

    public string GetProperty(string name) {
        var node = _element.SelectSingleNode($"./{name}") as XmlElement;
        return node?.InnerText ?? string.Empty;
    }

    public void SetProperty(string name, string value) {
        var node = _element.SelectSingleNode($"./{name}") as XmlElement;
        if (node == null) {
            node = _element.OwnerDocument!.CreateElement(name, _element.NamespaceURI);
            _element.AppendChild(node);
        }
        node.InnerText = value ?? string.Empty;
    }
    
    public int Count => _element.ChildNodes.OfType<XmlElement>().Count();
    
    public void Remove() => _element.ParentNode!.RemoveChild(_element);
}