using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml;
using ImageBlockz.Operations.InOut;
using ImageBlockz.Api;
using ImageBlockz.Core;

namespace ImageBlockz.Operations.Edit
{
    public class Crop : AbstractOperation
    {
        public static readonly string OperationName = typeof(Crop).FullName;

        public Crop() : base(OperationType.Filter)
        {
        
        }

        public Crop(Rectangle rec) : this()
        {
            Rec = rec;
        }

        public Rectangle Rec
        {
            get;
            set;
        }

        public override Bitmap Do(Bitmap img)
        {
            try
            {
                Bitmap cropedBmp = img.Clone(Rec, img.PixelFormat);
                return cropedBmp;
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Illegal values for cropping. Correct your entry. Operartion will be skiped for now.");    
            }

            return img;
        }

        public override XmlNode ToXml(XmlDocument xmlDoc)
        {
            XmlElement crop = xmlDoc.CreateElement(OperationName);
            XmlElement rectangle = XmlOperation.RectangleToXml(Rec, xmlDoc);

            crop.AppendChild(rectangle);

            return crop;
        }

        public override void FromXml(XmlNode node)
        {
            Rec = XmlOperation.RectangleFromXml(node["rectangle"]);
            Console.WriteLine(Rec.X.ToString() + "x" + Rec.Y.ToString() + "x" + Rec.Width.ToString() + "x" + Rec.Height.ToString());
        }

        public override string GetFormattedName()
        {
            return "Crop";
        }


        public override string GetInfo()
        {
            return "X: " + Rec.X.ToString() + " Y: " + Rec.Y.ToString() + " Width: " + Rec.Width.ToString() + " Height: " + Rec.Height.ToString();
        }
    }
}
