using System.Collections.Generic;
using System.Xml;

namespace ZionGraphics.Filtering
{
    public class Filters
    {
        List<IFilter> filters;

        public List<IFilter> List
        {
            get
            {
                if (filters == null)
                    filters = new List<IFilter>();

                return filters;
            }
        }

        public XmlElement ToNode(XmlDocument document)
        {
            XmlElement nodes = document.CreateElement("Filters");

            filters.ForEach(filter =>
            {
                XmlElement node = document.CreateElement("Filter");
                node.SetAttribute("type", filter.GetType().Name);
                node.SetAttribute("intensity", filter.Intensity.ToString());
                node.SetAttribute("red", filter.Red.ToString());
                node.SetAttribute("green", filter.Green.ToString());
                node.SetAttribute("blue", filter.Blue.ToString());
                nodes.AppendChild(node);
            });

            return nodes;
        }

        public Filters() { }

        public Filters(XmlNode node)
        {
            foreach (XmlNode n in node.ChildNodes)
            {
                List.Add(IFilter.build(
                    n.Attributes["type"].Value,
                    int.Parse(n.Attributes["intensity"].Value),
                    int.Parse(n.Attributes["red"].Value),
                    int.Parse(n.Attributes["green"].Value),
                    int.Parse(n.Attributes["blue"].Value)
                    ));
            }
        }

    }
}
