using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZionRobot.Models.Source
{
    public class Origin : ZionVault.Util.Xml
    {
        private string mainTag = @"Workflow";
        private string path;

        public string Title { get; set; }
        public string Domain { get; set; }
        public string Root { get; set; }
        public string OutputFolder { get; set; }
        public List<Models.Search.Element> SearchElements { get; set; }
        public List<Models.Source.ActionsElement> ActionsElements { get; set; }
        public List<Models.Source.BlockElement> BlockElements { get; set; }

        public Origin(string xmlPath)
        {
            try
            {
                this.path = xmlPath;

                this.Load(this.path, mainTag, false, false);

                System.Xml.XmlNode root = this.SelectSingleNode(mainTag);

                Title = root.Attributes[@"Title"]?.Value ?? null;
                Domain = root.Attributes[@"Domain"]?.Value ?? null;
                Root = root.Attributes[@"Root"]?.Value ?? null;
                OutputFolder = root.Attributes[@"OutputFolder"]?.Value ?? null;

                ActionsElements = new List<Models.Source.ActionsElement>();

                foreach (System.Xml.XmlElement action in root.SelectNodes(@"Actions"))
                {
                    ActionsElements.Add(new Models.Source.ActionsElement(action));
                }

                BlockElements = new List<Models.Source.BlockElement>();

                foreach (System.Xml.XmlElement block in root.SelectNodes(@"Block"))
                {
                    BlockElements.Add(new Models.Source.BlockElement(block));
                }

                SearchElements = new List<Models.Search.Element>();

                foreach (System.Xml.XmlElement element in root.SelectNodes(@"SearchElements/SearchElement"))
                {
                    SearchElements.Add(new Models.Search.Element(element));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

    }
}
