#pragma once
#include "gosGml.hpp"
#include "gosGml API.h"

//===========================================================================//
// File:	 gosGml API.cpp													 //
// Project:  gosGml					                                         //
// Contents: 																 //
//---------------------------------------------------------------------------//
// Date      Who    Modification                                             //
// 4/10/00   JR     Created													 //
//---------------------------------------------------------------------------//
// Copyright (C) 2000 Microsoft Corp.						                 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================/

extern HGOSFONT3D gGmlFont;
extern HGOSFONT3D arial8;

extern HGOSSCRIPT gosGml_Script;
extern int gosGml_On;
extern unsigned char * gosGml_TextData;
extern GmlDisplaySystem* HDS;
extern GStack<char>* GS;
extern GStack<char>* GSF;



//Rei API



GML::GML(char* fontPath, char* imagePath, char* defaultPagePath, rect* DisplayTextArea, int tabwidth)
:ScrollX(0),ScrollY(0)
{
	_gos_SetDefaultPath(defaultPagePath);
	gGmlFont = gos_LoadFont("assets\\graphics\\arial8.tga");
	arial8=gos_LoadFont("assets\\fonts\\arial8.d3f");
	GDS=new GmlDisplaySystem(fontPath,imagePath,DisplayTextArea,tabwidth);
	Roll=new Rollover();
	Reg=new Region();
	TextArea=DisplayTextArea;
}

GML::~GML()
{
	delete TextArea;
	delete Reg;
	delete Roll;
	delete GDS;
}

int GML::DisplayGML(char* TextData)
{
	gos_TextSetRegion(TextArea->Left,TextArea->Top,TextArea->Right,TextArea->Bottom);
	gos_TextSetAttributes(gGmlFont, 0xffffffff,1,1,1,0,0,0);
	gos_TextSetPosition(TextArea->Left,TextArea->Top-ScrollY);
	return GDS->TextDrawGml(TextData);
}

void GML::SetParserCallback(ParserCallback* PCB)
{
	GDS->SetParserCallback(PCB);
}


ExportHyperlink* GML::ExportHyperlinks()
{return GDS->ExportHyperlinks();}

GMLHyperlink* GML::GetHyperlink(int Num)
{
	ExportHyperlink* EHL=GDS->ExportHyperlinks();
	for(int Count=0;Count<Num && EHL;Count++)
	{
		ExportHyperlink* Old=EHL;
		EHL=EHL->Next;
		delete Old->HLink;
		delete Old;
	}
	if(EHL && EHL->HLink)
	{
		GMLHyperlink* HL=EHL->HLink;
		delete EHL;
		return HL;
	}
	else
		return NULL;
}

GMLHyperlink* GML::GetHyperlink(char* Desc)
{
	if(!Desc)
		return NULL;
	ExportHyperlink* EHL=GDS->ExportHyperlinks();
	while(EHL && EHL->HLink && EHL->HLink->Name && strcmp(EHL->HLink->Name,Desc))
	{
		ExportHyperlink* Old=EHL;
		EHL=EHL->Next;
		delete Old->HLink;
		delete Old;
	}
	if(EHL && EHL->HLink)
	{
		GMLHyperlink* HL=EHL->HLink;
		delete EHL;
		return HL;
	}
	else
		return NULL;}

char* GML::Click(int x, int y)
{return GDS->TestForHit(x,y+ScrollY);}

void GML::RollOver(int x, int y)
{
	if(ImageData* ImD = Reg->TestForHit(x,y))
		Roll->CallOn(ImD);
}

ExportImageData* GML::ExportImageList()
{return GDS->ExportImageList();}

void GML::RegisterRollover(ImageData* Image, RolloverCallback* F)
{Roll->Add(Image,*F);}

char* GML::LoadPage(const char* FileName)
{
	char* T;
	BYTE** Dat = new BYTE*;
	DWORD* Size = new DWORD;
	_gos_GetPage(FileName, Dat, Size);
	delete Size;
	T=(char*)*Dat;
	delete Dat;
	return T;
}

char* GML::LocateFile(const char* FileName)
{return _gos_LocatePage(FileName);}

void GML::ManageVideo(HGOSVIDEO I)
{GDS->ManageVideo(I);}
void GML::ManageImage(DWORD I)
{
	GDS->ManageImage(I);
}

void GML::SetScrollOffset(int y)
{
	ScrollY=y;
}

HistoryS::HistoryS()
{HS=new History();}

HistoryS::~HistoryS()
{delete HS;}

char* HistoryS::HistoryBack()
{return HS->Back();}

char* HistoryS::HistoryForwards()
{return HS->Forward();}

bool HistoryS::BackIsEmpty()
{return HS->BackEmpty();}

bool HistoryS::ForwardsIsEmpty()
{return HS->ForwardEmpty();}

void HistoryS::HistoryAdd(char* Entry)
{HS->Add(Entry);}

void HistoryS::HistoryClear()
{HS->Clear();}

void HistoryS::GMLSetHistoryLen()
{return;}
