//CConsole.cpp


#include <winsock2.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <fstream>
#include <cstdlib>


#include "CConsole.h"
#include "CDInputManager.h"



		/************************/

void CConsole::Clear()
{
	x=0;
	y=0;
	yv=0;
}

bool CConsole::Init()
{
	

	Image=NULL;
	//Image = new CImage();	
	//Image->Load(CTextReader::getString("Console.Img"));	

	MaxY = .33 * 768; //g_DDManager.SCREEN_HEIGHT;//CTextReader::getFloat("Console.MaxY") * g_DDManager.SCREEN_HEIGHT;
	Speed = 2;//CTextReader::getInt("Console.Speed");
	DebugLevel = 5;//CTextReader::getInt("DebugLevel");
	ActivateCommand.Init(DIK_GRAVE);

	AddMessage(STRINGC("Console Initiated. Current Debug Level: ") + STRINGC(DebugLevel));
	//Activate();

	return true;
}

void CConsole::Draw()
{
	//Assert(Image , "CConsole Image Not Loaded")
	//g_DDManager.DrawCImage(Image,x,y-Image->Height);

	drawText();
	Update();


}


void CConsole::Activate()
{
	//change directions
	if(yv==0)
	{
		if(y>0)
		{
			yv=-Speed;
		}
		else
			yv=Speed;
	}
	else
	{
		yv*=-1;
	}


}

void CConsole::Update()
{
	if(yv!=0)
	{
		y+=yv;

		if(y<=0)
		{
			y=0;
			yv=0;
		}
		else if(y >= MaxY)
		{
			y=MaxY;
			yv=0;
		}
	}

	//key
	if(ActivateCommand.isActive())
	{
		Activate();
	}
	
		
}

void CConsole::AddMessage(STRINGC msg, int lvl)
{

	if(lvl<=DebugLevel)
	{
		AddMessage(msg);
	}

}

void CConsole::AddMessage(STRINGC msg)
{
	Strings.push_front(msg);
}

void CConsole::drawText()
{
	int y1=y-2;
	


	for(StringListIterator itor=Strings.begin(); itor!= Strings.end();++itor)
	{

		y1-=17; //g_DDManager.FontHeight;

		if(y1 < 0)
			break;
		
		g_GraphicSystem.PrintText(itor->GetString(),5,y1);	
		


	}

}
