using System.Collections.Generic;
using System.Xml;

namespace ZionVault.Binder.Log
{
    public class Document
    {
        private string name;
        private string root;
        private List<ElementsGroup> elementsGroups;

        public string Name { get => name; set => name = value; }
        public string Root { get => name; }
        public List<ElementsGroup> ElementsGroups { get => elementsGroups; set => elementsGroups = value; }

        public Document()
        {
            this.elementsGroups = new List<ElementsGroup>();
        }

        public Document(string name)
        {
            this.name = name;
            this.elementsGroups = new List<ElementsGroup>();
        }

        public void Save(string path, bool append = true, bool encrypted = true)
        {
            Util.Xml xml = new Util.Xml();

            XmlNode root;
            if (System.IO.File.Exists(path) && append)
            {
                xml.Load(path, Root, false, encrypted);
                root = xml.SelectSingleNode(Root);
            }
            else
            {
                xml.AppendChild(xml.CreateXmlDeclaration("1.0", @"UTF-8", null));
                root = xml.CreateElement(Root);
            }

            ElementsGroups.ForEach(group =>
            {
                group.AppendTo(xml, root);
            });

            xml.AppendChild(root);
            xml.Save(path, Root, false, false);
        }

    }
}
