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

namespace ZionRobot.Models.Search
{
    public class Element : IDisposable
    {
        private string value;

        public string Name { get; set; }
        public string Where { get; set; }
        public string GoTo { get; set; }
        public string Attribute { get; set; }
        public List<string> Replaces { get; set; }
        public List<string> RemoveChars { get; set; }
        public bool IsHtml { get; set; }

        public string Value { get => Apply(value); set => this.value = value; }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        public Element(string name, string where, string goTo, string attribute, bool isHtml)
        {
            this.Name = name;
            this.Where = where;
            this.GoTo = goTo;
            this.Attribute = attribute;
            this.IsHtml = isHtml;

            RemoveChars = new List<string>();
            Replaces = new List<string>();
        }

        public Element(string name, string where, string value, string goTo, bool isHtml, string attribute = null)
        {
            this.Name = name;
            this.Where = where;
            this.value = value;
            this.GoTo = goTo;
            this.Attribute = string.IsNullOrEmpty(attribute) ? string.Empty : attribute;
            this.IsHtml = isHtml;

            RemoveChars = new List<string>();
            Replaces = new List<string>();
        }

        public Element(System.Xml.XmlElement node)
        {
            Name = node.GetAttribute(@"Name");
            Where = node.GetAttribute(@"Where");
            GoTo = node.GetAttribute(@"GoTo");
            Attribute = node.GetAttribute(@"Attribute") ?? null;
            value = node.InnerText;

            IsHtml = string.IsNullOrEmpty(node.GetAttribute(@"IsHtml")) ?
                false : Boolean.Parse(node.GetAttribute(@"IsHtml"));

            RemoveChars = new List<string>();

            string removeChars = node.GetAttribute(@"RemoveChars") ?? null;
            if (!string.IsNullOrEmpty(removeChars))
            {
                removeChars.Split(';').ToList().ForEach(@char => {
                    RemoveChars.Add(@char);
                });
            }

            Replaces = new List<string>();
            string replaces = node.GetAttribute(@"Replaces") ?? null;
            if (!string.IsNullOrEmpty(replaces))
            {
                replaces.Split(';').ToList().ForEach(replace => {
                    Replaces.Add(replace);
                });
            }
        }

        private string Apply(string value)
        {
            if (RemoveChars.Count() > 0)
            {
                value = ZionVault.Util.StringFormat.RemoveChars(value, RemoveChars);
            }

            if (Replaces.Count() > 0)
            {
                value = ZionVault.Util.StringFormat.Replace(value, Replaces);
            }

            return value;
        }

        public System.Xml.XmlNode AppendTo(System.Xml.XmlDocument xDoc)
        {
            System.Xml.XmlElement element = xDoc.CreateElement(@"Element");
            element.SetAttribute(@"Name", Name);
            element.SetAttribute(@"Goto", GoTo);
            element.SetAttribute(@"Attribute", Attribute);
            element.SetAttribute(@"IsHtml", IsHtml.ToString());
            element.InnerText = value;

            return element;
        }
    }
}
