using System;
using System.Collections.Generic;
using System.Xml;
using ZionSchematics.Models.SearchConstraintTypes;

namespace ZionSchematics.Models
{
    [Serializable]
    public class SearchConstraints
    {
        public List<ISearchAreaConstraints> SearchAreas { get; set; }

        public List<string> ExcludedElements { get; set; }

        public SearchConstraints()
        {
            this.ExcludedElements = new List<string>();
            this.SearchAreas = new List<ISearchAreaConstraints>();
        }

        public SearchConstraints(List<ISearchAreaConstraints> searchareas, List<string> excludedelements)
        {
            this.ExcludedElements = excludedelements;
            this.SearchAreas = searchareas;
        }

        public XmlElement Save(XmlDocument context)
        {
            XmlElement tag = context.CreateElement(@"search-constraints");
            XmlElement areas = context.CreateElement(@"areas");
            XmlElement excluded = context.CreateElement(@"excluded-elements");

            foreach (ISearchAreaConstraints search in this.SearchAreas)
            {
                XmlElement area = context.CreateElement(@"area");

                XmlElement type = context.CreateElement(@"type");
                XmlElement ranges = context.CreateElement(@"ranges");
                XmlElement regions = context.CreateElement(@"regions");

                type.InnerText = search.Type;
                ranges.InnerText = search.Ranges;
                regions.InnerText = search.Regions;

                area.AppendChild(type);
                area.AppendChild(ranges);
                area.AppendChild(regions);

                areas.AppendChild(area);
            }

            foreach (string excludedElement in this.ExcludedElements)
            {
                XmlElement element = context.CreateElement(@"element");

                element.InnerText = excludedElement;

                excluded.AppendChild(element);
            }

            tag.AppendChild(areas);
            tag.AppendChild(excluded);

            return tag;
        }
    }
}
