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

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

        public OpenFile() : base(OperationType.Input)
        {
        
        }

        public OpenFile(string filename)
            : this()
        {
            Filename = filename;
        }

        public string Filename
        {
            get;
            set;
        }

        public override Bitmap Do(Bitmap img)
        {
            Bitmap openedBitmap = new Bitmap(Filename);
            Rectangle rect = new Rectangle(0, 0, openedBitmap.Width, openedBitmap.Height);
            openedBitmap = openedBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
            return openedBitmap;
        }

        public override XmlNode ToXml(XmlDocument xmlDoc)
        {
            XmlElement openfile = xmlDoc.CreateElement(OperationName);
            XmlElement filename = xmlDoc.CreateElement("filename");

            filename.AppendChild(xmlDoc.CreateTextNode(Filename));
            openfile.AppendChild(filename);

            return openfile;
        }

        public override void FromXml(XmlNode node)
        {
            Filename = node["filename"].FirstChild.Value;

        }

        public override string GetFormattedName()
        {
            return "Open File";
        }


        public override string GetInfo()
        {
            return "Filename: " + Filename;
        }
    }
}
