using System;
using System.Xml;
using ZionVault.Basic;

namespace ZionVault.Binder.Output
{
    public class Field : IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #region Output data

        public string Name { get; set; }

        public string FullName { get; set; }

        public string Type { get; set; }

        public string Value { get; set; }

        public int Quality { get; set; }

        public Location Position { get; set; }

        public string Location { get { if (Position is null) return string.Empty; return Position.ToString(); } }

        public bool Exportable { get; set; }

        public bool Verifiable { get; set; }

        public bool isFound { get; set; }

        public string TypeConst
        {
            get
            {
                switch (Type)
                {
                    default:
                        return "STR";
                }
            }
        }

        #endregion

        public Field(string Name,
            string FullName,
            string Type,
            bool export,
            bool verify,
            bool isFound = false,
            string Value = @"",
            int Quality = 0,
            Location Location = null)
        {
            this.Name = Name;
            this.FullName = FullName;
            this.Type = Type;
            this.Value = Value;
            this.Quality = Quality;
            this.Position = Location;
            this.isFound = isFound;
            this.Exportable = export;
            this.Verifiable = verify;
        }

        public Field(XmlElement element)
        {
            this.Name = element.GetAttribute(@"Name");
            this.FullName = element.GetAttribute(@"FullName");
            this.Type = element.GetAttribute(@"Type");
            this.Quality = int.Parse(element.GetAttribute(@"Quality"));
            this.Position = new Location(element.GetAttribute(@"Location"));
            this.Value = element.InnerText;
            this.Exportable = bool.Parse(element.GetAttribute(@"Exportable"));
            this.Verifiable = bool.Parse(element.GetAttribute(@"Verifiable") != string.Empty ? element.GetAttribute(@"Verifiable") : @"False");
            this.isFound = bool.Parse(element.GetAttribute(@"isFound"));
        }


        public XmlElement Export(XmlDocument context)
        {
            XmlElement tag = context.CreateElement(@"field");
            tag.SetAttribute(@"Name", this.Name);
            tag.SetAttribute(@"FullName", this.FullName);
            tag.SetAttribute(@"Type", this.Type);
            tag.SetAttribute(@"Quality", this.Quality.ToString());
            tag.SetAttribute(@"Location", this.Position?.ToString());
            tag.InnerText = this.Value;
            tag.SetAttribute(@"Exportable", this.Exportable.ToString());
            tag.SetAttribute(@"Verifiable", this.Verifiable.ToString());
            tag.SetAttribute(@"isFound", this.isFound.ToString());
            return tag;
        }

        public Field Clone()
        {
            return new Field(
                this.Name,
                this.FullName,
                this.Type,
                this.Exportable,
                this.Verifiable,
                this.isFound,
                this.Value,
                this.Quality,
                this.Position
                );
        }
    }
}
