using System;
using System.Collections.Generic;
using System.Xml;
using ZionSchematics.Models.SearchNearestTypes;
using ZionSchematics.Models.SearchRelationTypes;

namespace ZionSchematics.Models
{
    [Serializable]
    public class SearchRelations
    {
        public List<IRelation> Relations { get; set; }

        public INearest Nearest { get; set; }

        public SearchRelations()
        {
            this.Relations = new List<IRelation>();
            this.Nearest = null;
        }

        public SearchRelations(List<IRelation> relations, INearest nearest)
        {
            this.Relations = relations;
            this.Nearest = nearest;
        }

        public XmlElement Save(XmlDocument context)
        {
            XmlElement tag = context.CreateElement(@"search-relations");

            tag.AppendChild(CreateRelations(context));
            tag.AppendChild(CreateNearest(context));

            return tag;
        }

        private XmlElement CreateRelations(XmlDocument context)
        {
            XmlElement relations = context.CreateElement(@"relations");

            foreach (IRelation r in this.Relations)
            {
                XmlElement relation = context.CreateElement(@"relation");
                XmlElement reference = context.CreateElement(@"reference");
                XmlElement type = context.CreateElement(@"type");
                XmlElement boundry = context.CreateElement(@"boundary");
                XmlElement offset = context.CreateElement(@"offset");

                reference.InnerText = r.Reference;
                type.InnerText = r.Type;
                offset.InnerText = r.Offset.ToString();
                boundry.InnerText = r.Boundary.Type;

                relation.AppendChild(reference);
                relation.AppendChild(type);
                relation.AppendChild(boundry);
                relation.AppendChild(offset);

                relations.AppendChild(relation);
            }

            return relations;
        }

        private XmlElement CreateNearest(XmlDocument context)
        {
            XmlElement neighborhood = context.CreateElement(@"neighborhood");
            if (this.Nearest != null)
            {
                XmlElement nearest = context.CreateElement(@"nearest");
                XmlElement reference = context.CreateElement(@"reference");
                XmlElement type = context.CreateElement(@"type");
                XmlElement x = context.CreateElement(@"x");
                XmlElement y = context.CreateElement(@"y");

                reference.InnerText = this.Nearest.Reference;
                type.InnerText = this.Nearest.Type;
                x.InnerText = this.Nearest.X.ToString();
                y.InnerText = this.Nearest.Y.ToString();

                nearest.AppendChild(reference);
                nearest.AppendChild(type);
                nearest.AppendChild(x);
                nearest.AppendChild(y);

                neighborhood.AppendChild(nearest);
            }
            return neighborhood;
        }


    }
}
