using System.Xml;

namespace Kit.Helpers.Repository.Xml;
public class XmlFileVersionInfo
{
    public XmlFileVersionInfo()
    {
        Type = string.Empty;
        Version = 0;
    }

    public string Type { get; set; }
    public long Version { get; set; }
}


public interface IXmlFileVersionRepository
{
    XmlFileVersionInfo? Get(XmlDocument document);
    void Set(XmlDocument document, XmlFileVersionInfo info);
}

public class XmlFileVersionRepository : IXmlFileVersionRepository
{
    public XmlFileVersionRepository() { }

    public XmlFileVersionInfo? Get(XmlDocument document)
    {
        XmlNode? fileVersionNode = document.DocumentElement?.SelectSingleNode("FileVersion");
        if (fileVersionNode == null) return null;

        return new XmlFileVersionInfo
        {
            Type = fileVersionNode.GetValueAttribute("type"),
            Version = fileVersionNode.GetValueAttributeInt64("version"),
        };
    }

    public void Set(XmlDocument document, XmlFileVersionInfo info)
    {
        Check.IsNotNull(info, "info is not set");
        Check.IsNotNull(document.DocumentElement, "DocumentElement is not set");

        XmlNode fileVersionNode = document.DocumentElement.SetElementValue("FileVersion", string.Empty);

        fileVersionNode.SetAttributeValue("type", info.Type);
        fileVersionNode.SetAttributeValue("version", info.Version.ToString());
    }
}