#include "Morault_Utils.h"
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
bool Morault::Utils::Strings::IterativeComp(std::string originalString, int startPointer, std::string toCompare)
{
	bool retVal = true;
	for (int i = 0; i < toCompare.length(); i++) {
		retVal = originalString.at(startPointer + i) == toCompare.at(i);
		if (retVal == false) break;
	}
	return retVal;
}

std::string Morault::Utils::Strings::IterativeStringExcerpt(std::string originalString, int startPointer, int length)
{
	std::string retval = "";
	if (startPointer == -1) return retval;
	for (int i = 0; i < length; i++) {
		retval += originalString[startPointer + i];
	}

	return retval;
}

int32_t Morault::Utils::Strings::StringIndexToInteger_4b_le(std::string originalString, int startPointer)
{
	int32_t retval;

	retval = (unsigned char)originalString.at(startPointer + 3) << 24;
	retval += (unsigned char)originalString.at(startPointer + 2) << 16;
	retval += (unsigned char)originalString.at(startPointer + 1) << 8;
	retval += (unsigned char)originalString.at(startPointer);


	return retval;
}

int32_t Morault::Utils::Strings::StringIndexToInteger_2b_le(std::string originalString, int startPointer)
{
	int32_t retval;

	retval = (unsigned char)originalString.at(startPointer + 1) << 8;
	retval += (unsigned char)originalString.at(startPointer);


	return retval;
}

std::tuple<std::string, std::string> Morault::Utils::Strings::ParseCLineIntoKeyVal(std::string originalString)
{
	std::tuple<std::string, std::string> retval;

	int equPos = -1, valstartpos = -1, valendpos = -1;
	bool inQuotes = false;

	for (int i = 0; i < originalString.size(); i++) {
		if (originalString[i] == '=' and equPos == -1) {
			equPos = i;
			valstartpos = i + 1;
		}
		if (originalString[i] == '"') {
			if (inQuotes) {
				valendpos = i - 1;
				break;
			}
			if (valstartpos == -1) {
				valstartpos = i + 1;
				inQuotes = true;
			}
		}
		if (originalString[i] == ';' and not inQuotes) {
			valendpos = i - 1;
		}

	}
	
	try {
		retval = std::make_tuple<std::string, std::string>(IterativeStringExcerpt(originalString, 0, equPos), IterativeStringExcerpt(originalString, valstartpos, valendpos - valstartpos + 1));
	}
	catch(...){
		retval = std::make_tuple<std::string, std::string>("", "");
	}
	return retval;
}

std::string Morault::Utils::Strings::SingleWordTrimmer(std::string originalString)
{
	for (int i = 0; i < originalString.size(); i++) {
		if (originalString[i] == ' ' or originalString[i] == '/') return IterativeStringExcerpt(originalString, 0, i);
	}

	return originalString;
}

std::basic_string<Morault::Utils::Geometry::Triangle2D> Morault::Utils::Geometry::TesselateVertexString(VertexString vs)
{
	
	std::basic_string<Triangle2D> retval;

	/*GLUtesselator* tess = gluNewTess();


	gluTessBeginPolygon(tess, 0);
	
	for (int i = 0; i < vs.size(); i++) {
		GLdouble* coords;
		coords[0] = static_cast<double>(vs[i].x);
		coords[1] = 0;
		coords[2] = static_cast<double>(vs[i].y);
		gluTessVertex(tess, coords, coords);
	}
	gluTessEndPolygon(tess);

	

	gluDeleteTess(tess);

	*/
	throw;

	return retval;
}
