using System.Drawing;
using System.Drawing.Imaging;
using System.Xml;

namespace ZionGraphics.Filtering
{
    public class IFilter
    {
        public int Red { get; set; }
        public int Green { get; set; }
        public int Blue { get; set; }
        public int Intensity { get; set; }

        public Bitmap SetPixelFormat(Bitmap img, PixelFormat format)
        {
            return img.Clone(new Rectangle(0, 0, img.Width, img.Height), format);
        }

        public virtual Bitmap Apply(Bitmap bitmap)
        {
            return bitmap;
        }

        public static IFilter build(string option, int intensity = 0, int red = 0, int green = 0, int blue = 0)
        {
            IFilter filter = null;
            switch (option)
            {
                case "AdaptativeThreshold":
                    filter = new AdaptativeThreshold();
                    break;
                case "BlackAndWhite":
                    filter = new BlackAndWhite();
                    break;
                case "Brightness":
                    filter = new Brightness();
                    break;
                case "ColorFiltering":
                    filter = new ColorFiltering();
                    break;
                case "Compression":
                    filter = new Compression();
                    break;
                case "Contrast":
                    filter = new Contrast();
                    break;
                case "Deskewing":
                    filter = new Deskewing();
                    break;
                case "Grayscale":
                    filter = new Grayscale();
                    break;
                case "Hue":
                    filter = new Hue();
                    break;
                case "InterativeThreshold":
                    filter = new InterativeThreshold();
                    break;
                case "InvertColors":
                    filter = new InvertColors();
                    break;
                case "Light":
                    filter = new Light();
                    break;
                case "PercentageThreshold":
                    filter = new PercentageThreshold();
                    break;
                case "Quality":
                    filter = new Quality();
                    break;
                case "Resolution":
                    filter = new Resolution();
                    break;
                case "Rotate":
                    filter = new Rotate();
                    break;
                case "Saturation":
                    filter = new Saturation();
                    break;
                case "Sepia":
                    filter = new Sepia();
                    break;
                case "Warmth":
                    filter = new Warmth();
                    break;
                case "Dilatation":
                    filter = new Dilatation();
                    break;
            }

            filter.Intensity = intensity;
            filter.Red = red;
            filter.Green = green;
            filter.Blue = blue;

            return filter;
        }

        public static IFilter build(XmlNode node)
        {
            return build(
                node.Attributes["type"].Value,
                int.Parse(node.Attributes["intensity"].Value),
                int.Parse(node.Attributes["red"].Value),
                int.Parse(node.Attributes["green"].Value),
                int.Parse(node.Attributes["blue"].Value)
                );
        }

        public void AppendTo(XmlDocument doc, XmlNode parent)
        {
            XmlElement node = doc.CreateElement("Filter");

            node.SetAttribute("type", this.GetType().Name);
            node.SetAttribute("intensity", this.Intensity.ToString());
            node.SetAttribute("red", this.Red.ToString());
            node.SetAttribute("green", this.Green.ToString());
            node.SetAttribute("blue", this.Blue.ToString());

            parent.AppendChild(node);
        }

        public IFilter() { }
    }
}