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

namespace ImageBlockz.Core
{
    /// <summary>
    /// This class encapsulates methods for conversion operation parameters to XML elements and vice-versa.
    /// </summary>
    public class XmlOperation
    {
        /// <summary>
        /// Convert <see cref="Size"/> to the <see cref="XmlElement"/>
        /// </summary>
        /// <param name="size">Input <see cref="Size"/></param>
        /// <param name="xmlDoc">Input <see cref="XmlDocument"/></param>
        /// <returns>Output <see cref="XmlElement"/></returns>
        public static XmlElement SizeToXml(Size size, XmlDocument xmlDoc)
        {
            XmlElement xmlSize = xmlDoc.CreateElement("size");
            xmlSize.SetAttribute("width", size.Width.ToString());
            xmlSize.SetAttribute("height", size.Height.ToString()); 

            return xmlSize;
        }


        /// <summary>
        /// Convert <see cref="Rectangle"/> to the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="rect">Input <see cref="Rectangle"/>.</param>
        /// <param name="xmlDoc">Input <see cref="XmlDocument"/>.</param>
        /// <returns>Output <see cref="XmlElement"/></returns>
        public static XmlElement RectangleToXml(Rectangle rect, XmlDocument xmlDoc)
        {
            XmlElement xmlRectangle = xmlDoc.CreateElement("rectangle");
            xmlRectangle.SetAttribute("x", rect.X.ToString());
            xmlRectangle.SetAttribute("y", rect.Y.ToString());
            xmlRectangle.SetAttribute("width", rect.Width.ToString());
            xmlRectangle.SetAttribute("height", rect.Height.ToString());

            return xmlRectangle;
        }

        /// <summary>
        /// Convert <see cref="float"/> value to the <see cref="XmlElement"/>
        /// </summary>
        /// <param name="name">Name of value.</param>
        /// <param name="value">Input <see cref="float"/> value.</param>
        /// <param name="xmlDoc">Input <see cref="XmlDocument"/>.</param>
        /// <returns>Output <see cref="XmlElement"/></returns>
        public static XmlElement FloatToXml(string name, float value, XmlDocument xmlDoc)
        {
            XmlElement xmlFloat = xmlDoc.CreateElement(name);
            xmlFloat.SetAttribute("value", value.ToString());

            return xmlFloat;
        }


        /// <summary>
        /// Convert <see cref="string"/> to the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="name">Name of item.</param>
        /// <param name="value">Input <see cref="string"/> value.</param>
        /// <param name="xmlDoc">Input <see cref="XmlDocument"/>.</param>
        /// <returns>Output <see cref="XmlElement"/></returns>
        public static XmlElement StringToXml(string name, string value, XmlDocument xmlDoc)
        {
            XmlElement xmlString = xmlDoc.CreateElement(name);
            xmlString.SetAttribute("string", value);

            return xmlString;
        }

        /// <summary>
        /// Convert <see cref="bool"/> value to the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="name">Name of value.</param>
        /// <param name="value">Input <see cref="bool"/> value.</param>
        /// <param name="xmlDoc">Input <see cref="XmlDocument"/>.</param>
        /// <returns>Output <see cref="XmlElement"/>.</returns>
        public static XmlElement BoolToXml(string name, bool value, XmlDocument xmlDoc)
        {
            XmlElement xmlBool = xmlDoc.CreateElement(name);
            xmlBool.SetAttribute("bool", value.ToString());

            return xmlBool;
        }

        /// <summary>
        /// Convert <see cref="Size"/> from the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="node"><see cref="XmlNode"/></param>
        /// <returns><see cref="Size"/></returns>
        public static Size SizeFromXml(XmlNode node)
        {
            int width = Int32.Parse(node.Attributes["width"].Value);
            int height = Int32.Parse(node.Attributes["height"].Value);
            Size size = new Size(width, height);

            return size;
        }

        /// <summary>
        /// Convert <see cref="Rectangle"/> from the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="node"><see cref="XmlNode"/></param>
        /// <returns><see cref="Rectangle"/></returns>
        public static Rectangle RectangleFromXml(XmlNode node)
        {
            int x = Int32.Parse(node.Attributes["x"].Value);
            int y = Int32.Parse(node.Attributes["y"].Value);
            int width = Int32.Parse(node.Attributes["width"].Value);
            int height = Int32.Parse(node.Attributes["height"].Value);
            Rectangle rect = new Rectangle(x, y, width, height);

            return rect;
        }

        /// <summary>
        /// Convert <see cref="float"/> from the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="node"><see cref="XmlNode"/></param>
        /// <returns><see cref="float"/></returns>
        public static float FloatFromXml(XmlNode node)
        {
            float value = float.Parse(node.Attributes["value"].Value);

            return value;
        }

        /// <summary>
        /// Convert <see cref="string"/> from the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="node"><see cref="XmlNode"/></param>
        /// <returns><see cref="string"/></returns>
        public static string StringFromXml(XmlNode node)
        {
            string value = node.Attributes["string"].Value;

            return value;
        }

        /// <summary>
        /// Convert <see cref="bool"/> from the <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="node"><see cref="XmlNode"/></param>
        /// <returns><see cref="bool"/></returns>
        public static bool BoolFromXml(XmlNode node)
        {
            bool value = false;

            if (node.Attributes["bool"].Value == "True")
            {
                value = true;
            }
            
            return value;
        }
    }
} 
