#include "gen_bot_header.h"

#ifndef __linux__
#include <io.h>
#endif
#include <fcntl.h>
#ifndef __linux__
#include <sys\stat.h>
#else
#include <sys/stat.h>
#endif

CBaseOutput cout;
CLeftOutput lcout;
CRightOutput rcout;
CMiddleOutput mcout;
//CLog clog;

void CBaseOutput::Display()
{
	if (FStrEq(TextToDisplay, "") == false)
	{
		char chunk[128] = "";

		if ( strlen( TextToDisplay) < 127)
		{
			strcpy(chunk, TextToDisplay);
		}
		else
		{
			strncpy( chunk, TextToDisplay, 127);
			chunk[127] = 0;		// strncpy doesn't always append the null terminator
		}

		// Throw out the message		
		UTIL_Printf(chunk);

		// Reset the message
		strcpy(TextToDisplay, "");
	}
}

CLog::CLog(void) : CursedVar(false), Started(false), fLogFile(NULL)
{
};

CLog::~CLog(void)
{
	if (Started && fLogFile)
		fclose(fLogFile);
};

void CLog::AddToTextString(const char *NewStr)
{
	// 'Display' the text. Just write it to a file :)
//	strcpy(TextToDisplay, NewStr);
//	Display();
}

void CLog::Display()
{
/*	FILE *fp = fopen("bot_log.txt", "a");
	fprintf(fp, "log write start\n");
	if (FStrEq(TextToDisplay, "") == false)
	{
		if (!Started)
		{
			Started = true;
			char LogFileName[256] = "";
			BuildBotFileName(LogFileName, "bot_log.txt", NULL);
			fLogFile = fopen(LogFileName, "a");
			fprintf(fLogFile, "\n----------------------------------------------------------\n\n");
		}

		// Throw out the message
		fprintf(fLogFile, TextToDisplay);

		// Reset the message
		strcpy(TextToDisplay, "");
		fprintf(fp, "log write end\n");
		fclose(fp);
	}*/
}

void CScreenOutput::AddToTextString(const char *NewStr)
{
	short CurrentLength = strlen(TextToDisplay);
	short NewLength = strlen(NewStr);
	char temp[512];

	strcpy(temp, "");
	// don't allow the string to have more then 512 chars otherwise the message might overflow
	if ((CurrentLength + NewLength) > 512)
	{
		strncpy(temp, NewStr, 511);
		temp[511] = 0;
	}
	else
		strcpy(temp, NewStr);

	CheckForThrowOut(temp);

	strcat(TextToDisplay, temp);
};

void CScreenOutput::Display()
{
	if (IS_DEDICATED_SERVER())
	{
		TextToDisplay[0] = 0;
		return;
	}

	if (TextToDisplay[0] != 0)
	{
		if (BotManager.GetHost() == NULL)
			ALERT(at_console, TextToDisplay);
		else
		{
			// Throw out the message
			MESSAGE_BEGIN (MSG_ONE_UNRELIABLE, 23, NULL, BotManager.GetHost()->pev);
				WRITE_BYTE (TE_TEXTMESSAGE);
				WRITE_BYTE (TextChannel); // channel
				WRITE_SHORT (xpos); // x coordinates * 8192
				WRITE_SHORT (ypos); // y coordinates * 8192
				WRITE_BYTE (0); // effect (fade in/out)
				WRITE_BYTE (R); // initial RED
				WRITE_BYTE (G); // initial GREEN
				WRITE_BYTE (B); // initial BLUE
				WRITE_BYTE (1); // initial ALPHA
				WRITE_BYTE (0); // effect RED
				WRITE_BYTE (0); // effect GREEN
				WRITE_BYTE (255); // effect BLUE
				WRITE_BYTE (1); // effect ALPHA
				WRITE_SHORT (1); // fade-in time in seconds * 256
				WRITE_SHORT (1); // fade-out time in seconds * 256
				WRITE_SHORT (1024); // hold time in seconds * 256
				WRITE_STRING (TextToDisplay); // send message
			MESSAGE_END (); // end
		}

		// Reset the message
		TextToDisplay[0] = 0;
	}
}

void BuildBotFileName(char *filename, char *arg1, char *arg2)
{
#ifndef __linux__

#ifdef _DEBUG
	strcpy(filename, "esf_inteam\\esf_bot\\");
#else
    strcpy(filename, "esf\\esf_bot\\");
#endif

#else
    strcpy(filename, "esf/esf_bot/");
#endif

    if ((arg1) && (*arg1) && (arg2) && (*arg2))
    {
		strcat(filename, arg1);

#ifndef __linux__
		strcat(filename, "\\");
#else
		strcat(filename, "/");
#endif

		strcat(filename, arg2);
    }
    else if ((arg1) && (*arg1))
    {
		strcat(filename, arg1);
    }
}

// returns true if the given file is valid
bool IsValidFile(const char *szFileName)
{
	FILE *fp = fopen(szFileName, "r");

	if (fp)
	{
		fclose(fp);
		return true;
	}

	return false;
}

bool FileIsOlder(const char *szFileName1, const char *szFileName2)
{
	// For now I just only check for file dates in windows, since it seems that
	// some linux versions don't support file create times.
#ifdef __linux__
	return true;
#else
	int file1, file2;
    struct stat stat1, stat2;

    file1 = open(szFileName1, O_RDONLY);
	file2 = open(szFileName2, O_RDONLY);
	
	fstat(file1, &stat1);
	fstat(file2, &stat2);
	
	close(file1);
	close(file2);

    return(stat1.st_mtime < stat2.st_mtime);
#endif
}