#include<iostream>
#include "merc.h"

struct colourConType
{	char *name;
	char colour;
};

struct colourConType colourConvert[] =
{	{	"hl-default",	'w'	},
	{	"hl-code",	'w'	},
	{	"hl-brackets",	'c'	},
	{	"hl-comment",	'w'	},
	{	"hl-quotes",	'w'	},
	{	"hl-string",	'r'	},
	{	"hl-identifier",'D'	},
	{	"hl-builtin",	'y'	},
	{	"hl-reserved",	'W'	},
	{	"hl-inlinedoc",	'b'	},
	{	"hl-var",	'b'	},
	{	"hl-url",	'M'	},
	{	"hl-special",	'w'	},
	{	"hl-number",	'B'	},
	{	"hl-inlinetags",'C'	},
	{	"hl-main",	'D'	},
	{	"hl-gutter",	'w'	},
	{	"hl-table",	'g'	},
	{	"hl-prepro",	'r'	},
	{	NULL,		'R'	},
};

// &Entity; <Entity lalala>

char *readTillTag(char *tag, char *blob)
{	char wholeTag[MSL];
	while(*blob != '\0')
	{	if(*blob == '<' || *blob == '&' )
		{	blob = grabNextEntity(blob, wholeTag, *blob);
			if(!strcasecmp(tag, wholeTag) )
				return blob;
		}
		++blob;
	}
	return blob;
}

char *grabNextEntity(char *blob, char *tag, char prelim)
{	char delim;
	tag[0] = '\0';
	if( prelim == '<' )
		delim = '>';
	else if( prelim == '&' )
		delim = ';';
	else
	{	logfp(LOG_BUG, "grabNextEntity: Bad prelimiter - %c", prelim);
		return blob;
	}
	*blob++;//Get rid of the prelimiter
	
	while(*blob != '\0' )
	{	if(*blob == delim)
			break;
		*tag = *blob;
		++tag;
		++blob;
	}
	*tag = '\0';
	++blob; //Hack off the delimiter
	return blob;
}
	
char *parseHtmlString(char *blob)
{	char wholeTag[MSL], *ptr;
	char choppedTag[MSL];
	char *newBlob, *start;
	newBlob = (char *)calloc(strlen(blob), sizeof(*newBlob));
	start = &newBlob[0];
	blob = readTillTag("/pre", blob);
	while( *blob != '\0' )
	{	if(*blob == '<' || *blob == '&' )
		{	blob = grabNextEntity(blob, wholeTag, *blob);
			//wholeTag is just that, but without the prelimiter or delimiter
			//So <span class="hl-prepro"> becomes 'span class="h1-prepro"'
			ptr = wholeTag;
			ptr = one_argument(ptr, choppedTag);
			if(choppedTag[0] == '\0' )
			{	logfp(LOG_BUG, "parseHtmlString: choppedTag is empty.");
				continue;
			}
			if(!strcasecmp(choppedTag, "span") )
			{	char colourTag[MSL];
				bool found = false;
				//ptr should be class="lalala" soo.
				one_argument(ptr+6, colourTag);
				for(int i = 0; colourConvert[i].name != NULL ; ++i)
				{	if(!strcasecmp(colourTag, colourConvert[i].name) )
					{	*newBlob = '{';
						++newBlob;
						*newBlob = colourConvert[i].colour;
						++newBlob;
						found = true;
						break;
					}
				}
				if(!found)
					logfp(LOG_BUG, "parseHtmlString: Bad colourTag! %s", colourTag);
				continue;
			}
			if(!strcasecmp(choppedTag, "/span" ) )
			{	*newBlob = '{';
				++newBlob;
				*newBlob = 'x';
				++newBlob;
				continue;
			}
			if(!strcasecmp(choppedTag, "quot") )
			{	*newBlob = '"';
				++newBlob;
				continue;
			}
			if(!strcasecmp(choppedTag, "lt") )
			{	*newBlob = '<';
				++newBlob;
				continue;
			}
			if(!strcasecmp(choppedTag, "gt" ) )
			{	*newBlob = '>';
				++newBlob;
				continue;
			}
			if(!strcasecmp(choppedTag, "amp" ) )
			{	*newBlob = '&';
				++newBlob;
				continue;
			}
			continue;	
		}
		*newBlob = *blob;
		if(*newBlob == '{')
		{	++newBlob;
			*newBlob = 'c';
			++newBlob;
			*newBlob = '{';
			++newBlob;
			*newBlob = '{';
		}
		++blob; ++newBlob;
		
	}
	*newBlob = '\0';
	return start;
}