using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using ZionSchematics.Models;

namespace ZionSchematics
{
    public class Schematics
    {
        private string path;

        private int _minimumNumberOfPages;
        private int _maximumNumberOfPages;

        public string Title { get; set; }

        public string Path { get => this.path; set => this.path = value; }

        public int Version { get; set; }

        public string Comment { get; set; }

        public int MinimumNumberOfPages { get => _minimumNumberOfPages; set => _minimumNumberOfPages = value; }

        public int MaximumNumberOfPages { get => _maximumNumberOfPages; set => _maximumNumberOfPages = value; }

        public SearchElements SearchElements { get; set; }
        
        public Schematics(string path = null)
        {
            this.path = path;
            ZionVault.Util.Xml document = new ZionVault.Util.Xml();

            try
            {
                document.Load(path, "search-elements", false);
            }
            catch
            {
                throw new Exception(@"Arquivo de máscara inválido.");
            }

            XmlNode type = document.SelectSingleNode(@"blueprint");
            XmlNode header = type.SelectSingleNode(@"header");
            XmlNode footer = type.SelectSingleNode(@"footer");
            XmlNode search_elements = type.SelectSingleNode(@"search-elements");
            // XmlNode key = header.SelectSingleNode(@"key");

            this.Title = type.Attributes["title"].Value;
            this.Version = int.Parse(type.Attributes["version"].Value);
            this.MinimumNumberOfPages = int.Parse(type.Attributes["minimum"].Value);
            this.MaximumNumberOfPages = int.Parse(type.Attributes["maximum"].Value);
            this.Comment = header.SelectSingleNode(@"comment").InnerText;

            this.SearchElements = new SearchElements(search_elements);
        }

        public void Save()
        {
            if (this.path == null || this.path == string.Empty)
                throw new Exception("Must have a valid path!");

            var info = new System.IO.FileInfo(path);

            this.Save(info.Directory.FullName, info.Name, true);
        }

        public void Save(string Path, string FileName, bool Encrypt)
        {
            ZionVault.Util.Xml xml = new ZionVault.Util.Xml();

            string dateNow = DateTime.Now.ToString();
            xml.AppendChild(xml.CreateXmlDeclaration("1.0", @"UTF-8", null));

            XmlElement root = xml.CreateElement(@"blueprint");
            root.SetAttribute("title", this.Title);
            root.SetAttribute("version", this.Version.ToString());
            root.SetAttribute("maximum", this.MaximumNumberOfPages.ToString());
            root.SetAttribute("minimum", this.MinimumNumberOfPages.ToString());

            XmlElement header = xml.CreateElement(@"header");
            XmlElement comment = xml.CreateElement(@"comment");

            comment.InnerText = this.Comment;

            header.AppendChild(comment);
            root.AppendChild(header);

            if (this.SearchElements == null)
                this.SearchElements = new Models.SearchElements();

            root.AppendChild(this.SearchElements.Save(xml));

            xml.AppendChild(root);

            xml.Save(string.Format(@"{0}\{1}", Path, FileName), @"search-elements", Encrypt);
        }

    }
}
