#include "GUIXMLTest.hpp"

void GUIXMLTest()
{
	printf("\nTesting GUI XML Test -----\n\n");

//  	GUIXMLDefaultTest();
//  
//  	GUIXMLPositionTest();
//  
//  	GUIXMLAlignTest();
// 
// 	GUIHelpers::eAlign topBottom[] = { GUIHelpers::eAlign::TOP, GUIHelpers::eAlign::CENTER, GUIHelpers::eAlign::BOTTOM };
// 	GUIHelpers::eAlign leftRight[] = { GUIHelpers::eAlign::LEFT, GUIHelpers::eAlign::CENTER, GUIHelpers::eAlign::RIGHT };
// 
// 	GUIHelpers::eOrientation ori[] = { GUIHelpers::eOrientation::HORIZONTAL, GUIHelpers::eOrientation::VERTICAL };
// 
// 	GUIHelpers::eAlign* align = topBottom;
// 	for (int four = 0, plus = 0; four < 2; ++four) {
// 		for (int one = 0; one < 3; ++one) {
// 			for (int two = 0; two < 3; ++two) {
// 				for (int three = 0; three < 3; ++three) {
// 					GUIXMLAlignTest(align[one], align[two], align[three], ori[four]);
// 				}
// 			}
// 		}
// 		align = leftRight;
// 	}

	GUIXMLBoxSizerTest();
}

void GUIXMLDefaultTest()
{
	UTest u("GUI XML Default");

	LoadFile("GUIXMLDefaultTest.xml");

	std::vector<GUIEngine::cWindow*> objects = GUIEngine::cGUI::Inst().GetObjects();

	GUIEngine::cPanel* panel = (GUIEngine::cPanel*)objects[0];

	Settings sets;
	sets.pos = { 0, 0 };
	sets.size = { 200, 200 };
	sets.pad = { 0, 0, 0, 0 };
	sets.center = { 100, 100 };

	cWindowTest("Panel", u, panel, sets);
	
	GUIEngine::cLayout* layout = (GUIEngine::cLayout*)panel->getChildren()[0];

	sets.pos = { 5, 5 };
	sets.size = { 190, 190 };
	sets.pad = { 5, 5, 5, 5 };


	cWindowTest("Layout", u, layout, sets);
	
	GUIEngine::cLabel* label = (GUIEngine::cLabel*)layout->getChildren()[0];


	sets.pos = { 56, 96 };
	sets.size = { 88, 8 };

	cWindowTest("Label", u, label, sets);

	u.report();

	GUIEngine::cGUI::Inst().Delete();
}

void GUIXMLPositionTest()
{
	UTest u("GUI XML Position");

	LoadFile("GUIXMLPositionTest.xml");

	std::vector<GUIEngine::cWindow*> objects = GUIEngine::cGUI::Inst().GetObjects();
	

	GUIEngine::cPanel* panel = (GUIEngine::cPanel*)objects[0];

	Settings sets;
	sets.pos = { 20, 20 };
	sets.size = { 200, 200 };
	sets.pad = { 0, 0, 0, 0 };
	sets.center = { 120, 120 };

	cWindowTest("Panel", u, panel, sets);


	GUIEngine::cLayout* layout = (GUIEngine::cLayout*)panel->getChildren()[0];

	sets.pos = { 25, 25 };
	sets.size = { 190, 190 };
	sets.pad = { 5, 5, 5, 5 };

	cWindowTest("Layout", u, layout, sets);

	
	GUIEngine::cLabel* label = (GUIEngine::cLabel*)layout->getChildren()[0];

	sets.pos = { 76, 116 };
	sets.size = { 88, 8 };

	cWindowTest("Label", u, label, sets);

	u.report();

	GUIEngine::cGUI::Inst().Delete();
}

void GUIXMLAlignTest( const GUIHelpers::eAlign one /*= GUIHelpers::eAlign::DEFAULT_ALIGN*/, const GUIHelpers::eAlign two /*= GUIHelpers::eAlign::DEFAULT_ALIGN*/, 
	const GUIHelpers::eAlign three /*= GUIHelpers::eAlign::DEFAULT_ALIGN*/, const GUIHelpers::eOrientation orientation /*= GUIHelpers::eOrientation::DEFAULT_ORIENTATION*/ )
{
	UTest u("GUI XML Align");

	LoadFile("GUIXMLAlignTest.xml");

	std::vector<GUIEngine::cWindow*> objects = GUIEngine::cGUI::Inst().GetObjects();

	GUIEngine::cPanel* panel = (GUIEngine::cPanel*)objects[0];

	Settings sets;
	sets.pos = { 0, 0 };
	sets.size = { 400, 400 };
	sets.pad = { 0, 0, 0, 0 };
	sets.center = { 200, 200 };

	cWindowTest("Panel", u, panel, sets);

	GUIEngine::cLayout* layout = (GUIEngine::cLayout*)panel->getChildren()[0];

	if (orientation != GUIHelpers::eOrientation::DEFAULT_ORIENTATION)
		layout->setOrientation(orientation);

	sets.pos = { 5, 5 };
	sets.size = { 390, 390 };
	sets.pad = { 5, 5, 5, 5 };
	
	cWindowTest("Layout", u, layout, sets);

	GUIEngine::cLabel* label = (GUIEngine::cLabel*)layout->getChildren()[0];
	
	cString msg = "";

	if (one != GUIHelpers::eAlign::DEFAULT_ALIGN)
		label->setAlign(one);

	switch (label->getAlign()) {
	case GUIHelpers::eAlign::TOP:
		msg = "Top";
		sets.pos = { 10, 10 };
		sets.center = { 74, 14};
		break;
	case GUIHelpers::eAlign::BOTTOM:
		msg = "Bottom";
		sets.pos = { 10, 382 };
		sets.center = { 74, 386 };
		break;
	case GUIHelpers::eAlign::LEFT:
		msg = "Left";
		sets.pos = { 10, 178 };
		sets.center = { 74, 182 };
		break;
	case GUIHelpers::eAlign::CENTER:
		msg = "Center";
		sets.pos = { 136, 178 };
		sets.center = { 200, 182 };
		if (layout->getOrientation() == GUIHelpers::eOrientation::HORIZONTAL) {
			sets.pos = { 10, 196 };
			sets.center = { 74, 200 };
		}
		break;
	case GUIHelpers::eAlign::RIGHT:
		msg = "Right";
		sets.pos = { 262, 178 };
		sets.center = { 326, 182 };
		break;
	}	

	sets.size = { 128, 8 };	

	cWindowTest("Label 'Hello world One' " + msg, u, label, sets);

	label = (GUIEngine::cLabel*)layout->getChildren()[1];

	if (two != GUIHelpers::eAlign::DEFAULT_ALIGN)
		label->setAlign(two);

	switch (label->getAlign()) {
	case GUIHelpers::eAlign::TOP:
		msg = "Top";
		sets.pos = { 128, 10 };
		sets.center = { 200, 14 };
		break;
	case GUIHelpers::eAlign::BOTTOM:
		msg = "Bottom";
		sets.pos = { 128, 382 };
		sets.center = { 200, 386 };
		break;
	case GUIHelpers::eAlign::LEFT:
		msg = "Left";
		sets.pos = { 10, 196 };
		sets.center = { 82, 200 };
		break;
	case GUIHelpers::eAlign::CENTER:
		msg = "Center";
		sets.pos = { 128, 196 };
		sets.center = { 200, 200 };
		if (layout->getOrientation() == GUIHelpers::eOrientation::HORIZONTAL) {
			sets.pos = { 128, 196 };
			sets.center = { 200, 200 };
		}
		break;
	case GUIHelpers::eAlign::RIGHT:
		msg = "Right";
		sets.pos = { 246, 196 };
		sets.center = { 318, 200 };
		break;
	}

	sets.size = { 144, 8 };
	
	cWindowTest("Label 'Hello world Two' " + msg, u, label, sets);

	label = (GUIEngine::cLabel*)layout->getChildren()[2];

	if (three != GUIHelpers::eAlign::DEFAULT_ALIGN)
		label->setAlign(three);

	switch (label->getAlign()) {
	case GUIHelpers::eAlign::TOP:
		msg = "Top";
		sets.pos = { 254, 10 };
		sets.center = { 322, 14 };
		break;
	case GUIHelpers::eAlign::BOTTOM:
		msg = "Bottom";
		sets.pos = { 254, 382 };
		sets.center = { 322, 386 };
		break;
	case GUIHelpers::eAlign::LEFT:
		msg = "Left";
		sets.pos = { 10, 214 };
		sets.center = { 78, 218 };
		break;
	case GUIHelpers::eAlign::CENTER:
		msg = "Center";
		sets.pos = { 132, 214 };
		sets.center = { 200, 218 };
		if (layout->getOrientation() == GUIHelpers::eOrientation::HORIZONTAL) {
			sets.pos = { 254, 196 };
			sets.center = { 322, 200 };
		}
		break;
	case GUIHelpers::eAlign::RIGHT:
		msg = "Right";
		sets.pos = { 254, 214 };
		sets.center = { 322, 218 };
		break;
	}
	sets.size = { 136, 8 };

	cWindowTest("Label 'Hello world Three' " + msg, u, label, sets);

	u.report();

	GUIEngine::cGUI::Inst().Delete();
}

void GUIXMLBoxSizerTest()
{
	UTest u("GUI XML BoxSizer");

	LoadFile("GUIXMLBoxSizerTest.xml", true);

	std::vector<GUIEngine::cWindow*> objects = GUIEngine::cGUI::Inst().GetObjects();

	GUIEngine::cPanel* panel = (GUIEngine::cPanel*)objects[0];

	Settings sets;
	sets.pos = { 0, 0 };
	sets.size = { 200, 200 };
	sets.pad = { 0, 0, 0, 0 };
	sets.center = { 100, 100 };

	cWindowTest("Panel", u, panel, sets);

	GUIEngine::cLayout* layout = (GUIEngine::cLayout*)panel->getChildren()[0];

	sets.pos = { 5, 5 };
	sets.size = { 190, 190 };
	sets.pad = { 5, 5, 5, 5 };


	cWindowTest("Layout", u, layout, sets);

	GUIEngine::cBoxSizer* boxsizer = (GUIEngine::cBoxSizer*)layout->getChildren()[0];

	sets.pos = { 51, 15 };
	sets.size = { 172, 5 };
	sets.center = { 100, 91 };

	GUIEngine::cLabel* label = (GUIEngine::cLabel*)layout->getChildren()[1];

	//label->setFontSize(8);

	sets.pos = { 64, 182 };
	sets.size = { 72, 8 };
	sets.center = { 100, 186 };

	cWindowTest("Label", u, label, sets);

	u.report();

	GUIEngine::cGUI::Inst().Delete();


}

void LoadFile( const cString& filename, const bool show /*= false*/ )
{
	GUIEngine::cGUI::Inst().Delete();
	GUIEngine::cGUI::Inst().Initialize(filename, "xml/");
	printf(cString("\nUsing " + filename + " file.\n").c_str());

	if (show == true)
		GUIEngine::cGUI::Inst().Display();
}

void cWindowTest(const cString& msg, UTest& u, GUIEngine::cWindow* win, const Settings sets)
{
	cWindowTest(msg, u, win, sets.pos, sets.size, sets.pad, sets.center);
}

void cWindowTest( const cString& msg, UTest& u, GUIEngine::cWindow* win, const GUIHelpers::Position& pos, 
	const GUIHelpers::Size& size, const GUIHelpers::Padding& pad, const GUIHelpers::Position& center)
{
	u.test(msg + " Position", win->getPosition() == pos);

	u.test(msg + " Size", win->getSize(false) == size);

	u.test(msg + " Padding", win->getPadding() == pad);

	u.test(msg + " Center", win->getCenter() == center);
}