#include "Stdafx.h"
#include "Cellule.h"

namespace CLITableur {
	Cellule::Cellule()
	{
		value = "";
		formula = "";

		// style par défaut
		alignment = DataGridViewContentAlignment::MiddleRight;
		foreColor = Color::Black;
		backgroundColor = Color::White;
		isBold = isItalic = isUnderlined = false;
		fontName = "Microsoft Sans Serif";
		fontSize= 10;

		// format
		format = FormatType::AUTOMATIQUE;
		formatString = "";
	}


	bool Cellule::IsNumber()
	{
		try 
		{
			Double::Parse(value);
			return true;
		}
		catch(...)
		{
			return false;
		}
	}


	XmlNode^ Cellule::ToXml(XmlDocument^ doc, int col, int row)
	{
		// créé un noeud sur l'arbre
		XmlNode^ n = doc->CreateElement("cell");

		// définit les attributs
		XmlAttribute^ av = doc->CreateAttribute("value");
		av->Value = Value;

		XmlAttribute^ af = doc->CreateAttribute("formula");
		af->Value = Formula;

		XmlAttribute^ acol = doc->CreateAttribute("col");
		acol->Value = "" + col;

		XmlAttribute^ arow = doc->CreateAttribute("row");
		arow->Value = "" + row;

		XmlAttribute^ aalign = doc->CreateAttribute("alignment");
		aalign->Value = alignment.ToString();

		XmlAttribute^ abold = doc->CreateAttribute("isBold");
		abold->Value = "" + isBold;

		XmlAttribute^ aitalic = doc->CreateAttribute("isItalic");
		aitalic->Value = "" + isItalic;

		XmlAttribute^ aunderlined = doc->CreateAttribute("isUnderlined");
		aunderlined->Value = "" + isUnderlined;

		XmlAttribute^ aforeColor = doc->CreateAttribute("foreColor");
		aforeColor->Value = ColorTranslator::ToHtml(foreColor);

		XmlAttribute^ abackgroundColor = doc->CreateAttribute("backgroundColor");
		abackgroundColor->Value = ColorTranslator::ToHtml(backgroundColor);

		XmlAttribute^ afontName = doc->CreateAttribute("fontName");
		afontName->Value = fontName;

		XmlAttribute^ afontSize = doc->CreateAttribute("fontSize");
		afontSize->Value = "" + fontSize;

		XmlAttribute^ aformatString = doc->CreateAttribute("formatString");
		aformatString->Value = formatString;

		XmlAttribute^ aformatType = doc->CreateAttribute("formatType");
		aformatType->Value = "" + String::Format("{0}",(int)format);

		n->Attributes->Append(av);
		n->Attributes->Append(af);
		n->Attributes->Append(aalign);
		n->Attributes->Append(abold);
		n->Attributes->Append(aitalic);
		n->Attributes->Append(aunderlined);
		n->Attributes->Append(aforeColor);
		n->Attributes->Append(abackgroundColor);
		n->Attributes->Append(afontName);
		n->Attributes->Append(afontSize);
		n->Attributes->Append(aformatString);
		n->Attributes->Append(aformatType);

		n->Attributes->Append(acol);
		n->Attributes->Append(arow);

		return n;
	}

	void Cellule::FromXml(XmlNode^ n)
	{
		for each (XmlAttribute^ at in n->Attributes)
		{
			if(at->Name=="value")                
				Value = at->Value;				

			if(at->Name=="formula")
				formula = at->Value;         

			if(at->Name=="alignment")
			{
				String^ val = at->Value;
				if(val == "MiddleLeft")
					alignment = DataGridViewContentAlignment::MiddleLeft;

				if(val == "MiddleCenter")
					alignment = DataGridViewContentAlignment::MiddleCenter;

				if(val == "MiddleRight")
					alignment = DataGridViewContentAlignment::MiddleRight;

			}

			if(at->Name=="isBold")
				isBold = Boolean::Parse(at->Value);

			if(at->Name=="isItalic")
				isItalic = Boolean::Parse(at->Value);

			if(at->Name=="isUnderlined")
				isUnderlined = Boolean::Parse(at->Value);

			if(at->Name=="foreColor")
				foreColor = ColorTranslator::FromHtml(at->Value);

			if(at->Name=="backgroundColor")
				backgroundColor = ColorTranslator::FromHtml(at->Value);

			if(at->Name=="fontName")
				fontName = at->Value;

			if(at->Name=="fontSize")
				fontSize = Single::Parse(at->Value);

			if(at->Name=="formatString")
				formatString = at->Value;

			if(at->Name=="formatType")
			{
				switch(Int32::Parse(at->Value))
				{
				case 0:
					format = FormatType::AUTOMATIQUE;
					break;

				case 1:
					format = FormatType::NOMBRE;
					break;

				case 2:
					format = FormatType::POURCENTAGE;
					break;

				case 3:
					format = FormatType::DATE;
					break;

				case 4:
					format = FormatType::TEXT;
					break;

				case 5:
					format = FormatType::PERSONNALISE;
					break;
				}
			}
		}
	}
}