#include "C:\L\clientviews\ChatWin\Chat.h"
ChatWindow::ChatWindow()
{
	Reset();
}
ChatWindow::ChatWindow(int HowWide, int Height)
{
	Reset();
	ScrnWidth = HowWide;
	NumDisLines	= Height;
}
ChatWindow::ChatWindow(int X, int Y, int HowWide, int Height)
{
	Reset();
	ScrnWidth = HowWide;
	NumDisLines	= Height;
	SetPos(X, Y);
}
ChatWindow::~ChatWindow()
{
}
void ChatWindow::Reset()
{
	for(NumLinesUsed = 0; NumLinesUsed < MaxLinesSaved; NumLinesUsed++)
	{
		Lines[NumLinesUsed].Red		= 0;	Lines[NumLinesUsed].Grn		= 0;
		Lines[NumLinesUsed].Blu		= 0;	Lines[NumLinesUsed].Port	= -1;
		Lines[NumLinesUsed].Sex		= 0;	Lines[NumLinesUsed].sMsg	= "";
		Lines[NumLinesUsed].Who		= -1;
	}
	NumLinesUsed	= 0;
	NumDisLines		= 0;
	LineIndex		= 0;
	ScrnWidth		= 0;
	TestPortsX		= 0;
	TestPortsY		= 0;
	TestPortR		= 0;
	TestPortB		= 0;
}
void ChatWindow::SetX(int Data)
{
	PosX		= Data;
	TestPortsX	= PosX + 1;
	TestPortR	= TestPortsX + 15;
}
int ChatWindow::GetX() const
{
	return PosX;
}
void ChatWindow::SetY(int Data)
{
	PosY		= Data;
	TestPortsY	= PosX + 3;
	TestPortB	= TestPortsY + (NumDisLines * 16);//Line height == 16
}
int ChatWindow::GetY() const
{
	return PosY;
}
void ChatWindow::SetPos(int DataX, int DataY)
{
	PosX = DataX; TestPortsX = PosX + 1; TestPortR = TestPortsX + 15;
	PosY = DataY; TestPortsY = PosX + 3; TestPortB = TestPortsY + (NumDisLines * 16);
}
int ChatWindow::GetTestX() const
{
	return TestPortsX;
}
int ChatWindow::GetTestY() const
{
	return TestPortsY;
}		
int ChatWindow::GetTestR() const
{
	return TestPortR;
}
int ChatWindow::GetTestB() const
{
	return TestPortB;
}
void ChatWindow::SetScrnWidth(int Width)
{
	ScrnWidth	= Width;
}
int ChatWindow::GetScrnWidth()
{
	return ScrnWidth;
}
void ChatWindow::SetNumDisLines(int L)
{
	NumDisLines	= L;
}
int ChatWindow::GetNumDisLines()
{
	return NumDisLines;
}
void ChatWindow::IncIndex()
{
	if(LineIndex < MaxLinesSaved - 1 && LineIndex < NumLinesUsed - NumDisLines) ++LineIndex;
}
void ChatWindow::DecIndex()
{
	if(LineIndex > 0) --LineIndex;
}
void ChatWindow::SetIndex(int Percentage)
{
	double Percent = 0.0;
	Percent		= double(Percentage)/100;
	LineIndex	= int(Percent * (NumLinesUsed - NumDisLines));
}
void ChatWindow::AddMsg(int ServIn, int dataRed, int dataGrn, int dataBlu, int Port, int dataSex, string dataString)
{
	int i = 0, iWork = 0; string strWork = "", DataSucker = ""; myMsg tempMsg;
	bool isInitial;
	strWork	= ""; DataSucker = "";
	tempMsg.Who = ServIn; tempMsg.Port = Port; tempMsg.Sex = dataSex;
	tempMsg.Red	= dataRed; tempMsg.Grn = dataGrn; tempMsg.Blu = dataBlu;

	isInitial = true;

	istringstream DATA(dataString.c_str());
	if(DATA.fail()) return;

	while(!DATA.eof() || strWork.length() > 0)
	{
		while(!DATA.eof())
		{
			DATA >> DataSucker;
			//+1 here to account for a space that is about to get added.
			if(int(strWork.length()) + int(DataSucker.length()) + 1 <= ScrnWidth)
			{
				strWork	+= DataSucker; DataSucker = "";
				if(!DATA.eof()) strWork += " ";
			}
			else break;
		}
		//Save the message line we just generated
		tempMsg.sMsg = strWork; strWork = "";
		//Save excess DataSucker data
		if(DataSucker.length() > 0)
		{
			strWork = DataSucker; DataSucker = "";
			if(!DATA.eof()) strWork += " ";
		}
		if(NumLinesUsed == MaxLinesSaved)
		{
			//Shift Lines up one...
			for(i = 0; i < (MaxLinesSaved - 1); i++)
			{
				Lines[i].Who	= Lines[i + 1].Who;
				Lines[i].Red 	= Lines[i + 1].Red;
				Lines[i].Grn	= Lines[i + 1].Grn;
				Lines[i].Blu	= Lines[i + 1].Blu;
				Lines[i].Port	= Lines[i + 1].Port;
				Lines[i].Sex	= Lines[i + 1].Sex;
				Lines[i].sMsg	= Lines[i + 1].sMsg;
				Lines[i].Class	= Lines[i + 1].Class;
			}
			//Assign to the last line in array--------------------
			if(isInitial == true)
			{
				Lines[MaxLinesSaved - 1].Who = tempMsg.Who;
				isInitial = false;
			}
			else Lines[NumLinesUsed].Who = -1;
			Lines[MaxLinesSaved - 1].Red	= tempMsg.Red;
			Lines[MaxLinesSaved - 1].Grn	= tempMsg.Grn;
			Lines[MaxLinesSaved - 1].Blu	= tempMsg.Blu;
			Lines[MaxLinesSaved - 1].Port	= tempMsg.Port;
			Lines[MaxLinesSaved - 1].Sex	= tempMsg.Sex;
			Lines[MaxLinesSaved - 1].sMsg	= tempMsg.sMsg;
			Lines[MaxLinesSaved - 1].Class	= -1;
		}
		else
		{
			//Assign data to the current slot waiting
			if(isInitial == true)
			{
				Lines[NumLinesUsed].Who		= tempMsg.Who;
				isInitial = false;
			}
			else Lines[NumLinesUsed].Who = -1;
			Lines[NumLinesUsed].Red		= tempMsg.Red;
			Lines[NumLinesUsed].Grn		= tempMsg.Grn;
			Lines[NumLinesUsed].Blu		= tempMsg.Blu;
			Lines[NumLinesUsed].Port	= tempMsg.Port;
			Lines[NumLinesUsed].Sex		= tempMsg.Sex;
			Lines[NumLinesUsed].sMsg	= tempMsg.sMsg;
			Lines[NumLinesUsed].Class	= -1;
			//Going to increment until the above if-test becomes True
			++NumLinesUsed;
		}
		if(NumLinesUsed > NumDisLines) LineIndex = NumLinesUsed - NumDisLines;
		else LineIndex = 0;
		tempMsg.Who = - 1; tempMsg.Port = -1; tempMsg.Sex = 0; tempMsg.Who = -1;
	}
}
void ChatWindow::AddMsg(int ServIn, int dataRed, int dataGrn, int dataBlu, int Port, int dataSex, int dataClass, string dataString)
{
	int i = 0, iWork = 0; string strWork = "", DataSucker = ""; myMsg tempMsg; strWork = ""; DataSucker = "";
	bool isInitial;
	tempMsg.Who = ServIn; tempMsg.Port = Port; tempMsg.Sex = dataSex; tempMsg.Red = dataRed;
	tempMsg.Grn = dataGrn; tempMsg.Blu = dataBlu; tempMsg.Class = dataClass;

	isInitial = true;

	istringstream DATA(dataString.c_str());
	if(DATA.fail()) return;

	while(!DATA.eof() || strWork.length() > 0)
	{
		while(!DATA.eof())
		{
			DATA >> DataSucker;
			//+1 here to account for a space that is about to get added.
			if(int(strWork.length()) + int(DataSucker.length()) + 1 <= ScrnWidth)
			{
				strWork	+= DataSucker; DataSucker = "";
				if(!DATA.eof()) strWork += " ";
			}
			else break;
		}
		//Save the message line we just generated
		tempMsg.sMsg = strWork; strWork = "";
		//Save excess DataSucker data
		if(DataSucker.length() > 0)
		{
			strWork = DataSucker; DataSucker = "";
			if(!DATA.eof()) strWork += " ";
		}
		if(NumLinesUsed == MaxLinesSaved)
		{
			//Shift Lines up one...
			for(i = 0; i < (MaxLinesSaved - 1); i++)
			{
				Lines[i].Who	= Lines[i + 1].Who;
				Lines[i].Red 	= Lines[i + 1].Red;
				Lines[i].Grn	= Lines[i + 1].Grn;
				Lines[i].Blu	= Lines[i + 1].Blu;
				Lines[i].Port	= Lines[i + 1].Port;
				Lines[i].Sex	= Lines[i + 1].Sex;
				Lines[i].sMsg	= Lines[i + 1].sMsg;
				Lines[i].Class	= Lines[i + 1].Class;
			}
			//Assign to the last line in array
			Lines[MaxLinesSaved - 1].Who	= tempMsg.Who;
			Lines[MaxLinesSaved - 1].Red	= tempMsg.Red;
			Lines[MaxLinesSaved - 1].Grn	= tempMsg.Grn;
			Lines[MaxLinesSaved - 1].Blu	= tempMsg.Blu;
			if(isInitial == true)
			{
				Lines[MaxLinesSaved - 1].Port	= tempMsg.Port;
				isInitial = false;
			}
			else Lines[NumLinesUsed].Port	= -1;
			Lines[MaxLinesSaved - 1].Sex	= tempMsg.Sex;
			Lines[MaxLinesSaved - 1].sMsg	= tempMsg.sMsg;
			Lines[MaxLinesSaved - 1].Class	= tempMsg.Class;
		}
		else
		{
			//Assign data to the current slot waiting
			Lines[MaxLinesSaved].Who		= tempMsg.Who;
			Lines[NumLinesUsed].Red			= tempMsg.Red;
			Lines[NumLinesUsed].Grn			= tempMsg.Grn;
			Lines[NumLinesUsed].Blu			= tempMsg.Blu;
			if(isInitial == true)
			{
				Lines[NumLinesUsed].Port	= tempMsg.Port;
				isInitial = false;
			}
			else Lines[NumLinesUsed].Port	= -1;
			Lines[NumLinesUsed].Sex		= tempMsg.Sex;
			Lines[NumLinesUsed].sMsg	= tempMsg.sMsg;
			Lines[NumLinesUsed].Class	= tempMsg.Class;
			//Going to increment until the above if-test becomes True
			++NumLinesUsed;
		}
		if(NumLinesUsed > NumDisLines) LineIndex = NumLinesUsed - NumDisLines;
		else LineIndex = 0;
	}
}
void ChatWindow::AddMsg(char *charMsg)
{
	int				iWork		= 0;
	string			strWork		= "";
	string			DataSucker	= "";
	istringstream	DATA(charMsg);
	if(DATA.fail()) return;
	strWork		= "";
	DataSucker	= "";

	/*
	incoming Msg should be compiled with an Avatar's Portrait, Sex, and 
	then Message (which has ServIndex and three color numbers at it's 
	beginning - ServIndex will get eliminated and then rest of message 
	broken down to build the lines of text as in the other like-function.
	*/
}
int ChatWindow::GetLineIndex()
{
	return LineIndex;
}
int ChatWindow::GetNumLines()
{
	return NumLinesUsed;
}
int ChatWindow::GetSex(int index)
{
	return Lines[index].Sex;
}
int ChatWindow::GetPort(int index)
{
	return Lines[index].Port;
}
int ChatWindow::GetRed(int index)
{
	return Lines[index].Red;
}
int ChatWindow::GetGrn(int index)
{
	return Lines[index].Grn;
}
int ChatWindow::GetBlu(int index)
{
	return Lines[index].Blu;
}
string ChatWindow::GetChatLine(int index)
{
	return Lines[index].sMsg;
}
void ChatWindow::ClearScrn()
{
	for(LineIndex = 0; LineIndex < MaxLinesSaved; LineIndex++)
	{
		Lines[LineIndex].Red	= 0;	Lines[LineIndex].Grn	= 0;
		Lines[LineIndex].Blu	= 0;	Lines[LineIndex].Port	= -1;
		Lines[LineIndex].Sex	= 0;	Lines[LineIndex].sMsg	= "";
	}
	LineIndex		= 0;
	NumLinesUsed	= 0;
}
int ChatWindow::GetWhoIsIt(int i)
{
	if(i >= 0 && i < MaxLinesSaved)	return Lines[i].Who;
	else							return -1;
}
int ChatWindow::GetClass(int i)
{
	if(i >= 0 && i < MaxLinesSaved) return Lines[i].Class;
	else							return -1;
}