#include "gosGml.hpp"
#include "Index.h"

IndexItem::IndexItem()
:Name(0),Value(0)
{}

IndexItem::~IndexItem()
{
	if(Name)
		delete[] Name;
	if(Value)
		delete[] Value;
}

/*
IndexItem& IndexItem::operator= (const IndexItem& rhs)
{
	if(this == &rhs)
		return *this;
	if(this->Name)
		delete[] this->Name;
	if(this->Value)
		delete[] this->Value;

	Name=rhs.Name;
	Value=rhs.Value;

	return *this;
}*/

Index::Index(char* Input, char* fontPath, char* imagePath, char* defaultPagePath, rect* DisplayTextArea, int tabwidth)
:Dim(DisplayTextArea),NumItems(0),CurrentItem(0),ItemHighlighted(false)
{
	if(!Input)
		return;
	Interpret(Input);
	Text=MakeGML();
	DS=new GML(fontPath, imagePath, defaultPagePath, DisplayTextArea, tabwidth);
	//SB=new Scrollbar();
}

Index::~Index()
{
	if(Text)
		delete[] Text;
	for(CurrentItem=0; CurrentItem<NumItems; CurrentItem++)
		delete *(List+CurrentItem);
	delete[] List;
	delete DS;
	//if(SB)
	//	delete SB;
}

void Index::HighLight(char* Text)
{
	int Res;
	if(Res=Search(Text))
	{
	}
}

char* Index::Click(int x, int y)
{
	char* CR=DS->Click(x,y);
	return CR;
}

int Index::Draw()
{
	return DS->DisplayGML(Text);
}

void Index::SetScrollOffset(int x)
{
	DS->SetScrollOffset(x);
}

#pragma warning(disable:4706)
void Index::Interpret(char* Data)
{
	GQueue<IndexItem>* TList=new GQueue<IndexItem>();
	char* DataO;
	IndexItem* II;
	while(*Data!='*')
	{
		II=new IndexItem;
		DataO=Data;
		while(*Data!='|')
			Data++;
		II->Name=MemToStr(DataO,Data-DataO);
		Data++;
		DataO=Data;
		while(*Data!=13)
			Data++;
		II->Value=MemToStr(DataO,Data-DataO);
		Data+=2;	//13 and 10
		TList->Push(II);
		NumItems++;
	}
	List=new IndexItem*[NumItems];
	while(II=TList->Pop())
	{
		*(List+CurrentItem)=II;
		CurrentItem++;
	}
	delete TList;
	CurrentItem=0;
}
#pragma warning(default:4706)


char* Index::MakeGML()
{
	char* Buffer=new char[131072];	//128 K... I really hope we don't ever need more!!
	*(Buffer+131071)=3;
	char* SBuffer=Buffer;
	SMakeGML(SBuffer);
	if(*(Buffer+131071)!=3)
		STOP(("Index::MakeGML() Sadly, you have a Index which is huge. The 128k buffer overflowed. Do you really need so many items?"));
	char* Buffer2=DupStr(Buffer);
	delete[] Buffer;			//don't cache 128 if you don't really need it ne
	return Buffer2;
}

void Index::SMakeGML(char*& Data)
{
	int Temp1;
	CurrentItem=0;
	while(CurrentItem<NumItems)
	{
		memcpy(Data,"<p>",3);
		Data+=3;
		memcpy(Data,"<a name=\"plaintext\" href=\"",26);
		Data+=26;
		Temp1=strlen((*(List+CurrentItem))->Value);
		memcpy(Data,(*(List+CurrentItem))->Value,Temp1);
		Data+=Temp1;
		memcpy(Data,"\">",2);
		Data+=2;
		Temp1=strlen((*(List+CurrentItem))->Name);
		memcpy(Data,(*(List+CurrentItem))->Name,Temp1);
		Data+=Temp1;
		memcpy(Data,"</a></p>",8);
		Data+=8;
		CurrentItem++;
	}
	*Data=0;
	CurrentItem=0;
	return;
}

int Index::Search(char* Text)
{
	int Low=0, High=NumItems-1, Mid=0;
	int SL=strlen(Text);
	while(Low<High)
	{
		Mid=(Low+High)>>1;
		switch(memicmp((*(List+Mid))->Name,Text,SL))
		{
			case 0:
				return Mid;
			case 1:
				Low=Mid+1;
				break;
			case -1:
				High=Mid-1;
				break;
		}
	}
	if(Low==High && !memicmp((*(List+Low))->Name,Text,SL))
		return Low;
	return NULL;
}