using System;
using System.Xml;

namespace ZionSchematics.Models
{
    [Serializable]
    public class Field
    {
        public string Name { get; set; }

        public string FullName { get; set; }

        public string Comment { get; set; }

        public bool Export { get; set; }

        public bool Verification { get; set; }

        public string ValidationScript { get; set; }

        public SearchControl SearchControl { get; set; }

        public SearchOptions SearchOptions { get; set; }

        public SearchConstraints SearchConstraints { get; set; }

        public SearchRelations SearchRelations { get; set; }

        public Field()
        {
            this.SearchControl = new SearchControl();
            this.SearchOptions = new SearchOptions(null, null);
            this.SearchConstraints = new SearchConstraints();
            this.SearchRelations = new SearchRelations();
            this.Export = true;
            this.Verification = true;
            this.ValidationScript = string.Empty;
        }

        public Field(string name, string comment, bool export = true, bool verification = true)
            : base()
        {
            this.Name = name;
            this.Comment = comment;
            this.Export = export;
            this.Verification = verification;
        }

        public Field(string name, string fullname, string comment, bool export, bool verification, string validationScript,
            SearchControl control, SearchOptions options, SearchConstraints constraints, SearchRelations relations)
            : base()
        {
            this.Name = name;
            this.FullName = fullname;
            this.Comment = comment;
            this.Export = export;
            this.Verification = verification;
            this.SearchControl = control;
            this.SearchOptions = options;
            this.SearchConstraints = constraints;
            this.SearchRelations = relations;
            this.ValidationScript = validationScript;
        }

        public XmlElement Save(XmlDocument context)
        {
            XmlElement field = context.CreateElement(@"field");

            XmlElement name = context.CreateElement(@"name");
            XmlElement fullname = context.CreateElement(@"full-name");
            XmlElement comment = context.CreateElement(@"comment");
            XmlElement export = context.CreateElement(@"export");
            XmlElement verification = context.CreateElement(@"verification");
            XmlElement script = context.CreateElement(@"script");

            name.InnerText = this.Name;
            fullname.InnerText = this.FullName;
            comment.InnerText = this.Comment;
            export.InnerText = this.Export.ToString();
            verification.InnerText = this.Verification.ToString();
            script.InnerText = this.ValidationScript;


            field.AppendChild(name);
            field.AppendChild(fullname);
            field.AppendChild(comment);
            field.AppendChild(export);
            field.AppendChild(verification);
            field.AppendChild(script);

            field.AppendChild(this.SearchControl.Save(context));
            field.AppendChild(this.SearchConstraints.Save(context));
            field.AppendChild(this.SearchRelations.Save(context));
            field.AppendChild(this.SearchOptions.Save(context));
            return field;
        }

    }
}
