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

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

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

        public Rotate(float angle)
            : this()
        {
            Angle = angle;
        }

        public float Angle
        {
            set;
            get;
        }

        public override Bitmap Do(Bitmap bmp)
        {
            PixelFormat format = PixelFormat.Format24bppRgb;

            Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height, format);
            Graphics graphic = Graphics.FromImage(rotatedImage);

            graphic.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
            graphic.RotateTransform(Angle);
            graphic.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);

            graphic.DrawImage(bmp, new Point(0, 0));

            return rotatedImage;
        }

        public override XmlNode ToXml(XmlDocument xmlDoc)
        {
            XmlElement rotate = xmlDoc.CreateElement(OperationName);
            XmlElement angle = XmlOperation.FloatToXml("angle", Angle, xmlDoc);
            
            rotate.AppendChild(angle);

            return rotate;
        }

        public override void FromXml(XmlNode node)
        {
            Angle = XmlOperation.FloatFromXml(node["angle"]);
        }

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


        public override string GetInfo()
        {
            return "Angle: " + Angle.ToString() + " degress";
        }
    }
}
