#include "MPFW_Utils.h"
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
bool MPFW::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 MPFW::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;
}

std::string MPFW::Utils::Strings::IterativeStringExcerpt_delim(std::string originalString, int startPointer, char delimiter)
{
	std::string retval = "";
	if (startPointer == -1) return retval;
	for (int i = startPointer; i < originalString.size(); i++) {
		if (originalString[i] == delimiter) break;
		retval += originalString[i];
	}

	return retval;
}

int32_t MPFW::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 MPFW::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> MPFW::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 MPFW::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;
}

float MPFW::Utils::Strings::StringIndexToFloat_4b_le(std::string originalString, int startPointer)
{
	

	unsigned char buf[4];

	buf[0] = originalString[startPointer];
	buf[1] = originalString[startPointer + 1];
	buf[2] = originalString[startPointer + 2];
	buf[3] = originalString[startPointer + 3];


	float retval = (*(float*)buf);

	return retval;

}

std::basic_string<MPFW::Utils::Geometry::Triangle2D> MPFW::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;
}
