#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include "conmat.h"

//Global vars
char filename[255];
LINE lines[NUM_LINES];
FILE *in;

int main(int argc, char *argv[]){


	if(argc < 2){
		showUsage();
		return 0;
	}//if

	in = NULL;
	strcpy(filename, argv[1]);		//Copy in the filename

	memset(&lines, 0, sizeof(LINE)*NUM_LINES);

	if(!reopenFile(filename)){
		printf("Error opening input file...aborting!\n");
		return 0;
	}//if

//	textmode(MONO);				//Just seeing how this would effect it...
	directvideo = 1;			//Write directly to video memory
	textmode(C80);				//Color 80 column mode
	clrscr();
	window(1,1,80,25);			//Create window
	_setcursortype(_NOCURSOR);

	char c = '!';
	int iDelay = 0;
	while((c != 'Q') && (c != 'q')){
		while(!kbhit()){			//Loop forever
	
			moveLines();
			delay(iDelay);
	
		}//while
		c = getch();
		if(c=='-'){
			iDelay++;
		}
		else if(c=='+'){
			iDelay--;
		}//else if
		else if(c==' '){
			while(!kbhit()){
				delay(200);
			}//while
			getch();
		}//else if

		if(iDelay < 0)
			iDelay = 0;
		if(iDelay > 1000)
			iDelay = 1000;
	}//while

	textcolor(WHITE);
	clrscr();
	fclose(in);

	return 1;
}//main

//This is the function responsible for moving the lines
void moveLines(){

	int i = random(NUM_LINES);			//Pick a line to move...

	if(lines[i].len == 0){
		getLine(i);
	}//if

	lines[i].currx--;
	if(lines[i].currx < 1)
		gotoxy(1, i+1);
	else
		gotoxy(lines[i].currx, i+1);

	if( lines[i].len + lines[i].currx < 0){
		memset(&lines[i], 0, sizeof(LINE));
	}//if

	char buffer[81];
	memset(buffer, 0, 81);
	
	if(lines[i].currx < 0){
		int ind = 0 - lines[i].currx;
		strncpy(buffer, &lines[i].text[ind], 80);
	}//if
	else{
		strncpy(buffer, lines[i].text, 80-lines[i].currx);
	}//else
	
	if((strlen(buffer)>3) && (lines[i].currx >= 1)){
		textcolor(LIGHTGREEN);
		char buff[6];
		buff[5] = '\0';
		strncpy(buff, buffer, 5);
		cprintf("%s", buff);
		textcolor(GREEN);	
		cprintf("%s", &buffer[5]);
	}//if
	else{
		textcolor(GREEN);
		cprintf("%s", buffer);
	}//else

}//moveLines

int getLine(int i){
	char line[500];
	char buffer[500];
	char *pTok = NULL;
	memset(line, 0, 500);

	strcpy(buffer, " ");

	while((pTok == NULL) && (strlen(buffer) != 0)){
		if(feof(in))
			if(!reopenFile(filename))
				return 0;

		fgets(line, 499, in);				//Read in a line
		strcpy(buffer, line);
		pTok = strtok(buffer, "\r\n\t ");
	}//while

	if(strlen(buffer) == 0)
		return getLine(i);

	memset(line, 0, 500);
	while(pTok != NULL){
		strcat(line, pTok);
		strcat(line, " ");
		pTok = strtok(NULL, "\r\n\t ");
	}//while

	memset(lines[i].text, 0, 500);
	strncpy(lines[i].text, line, 498);
	strcat(lines[i].text, " ");
	
	lines[i].len = strlen(lines[i].text);
	lines[i].currx = 80;

	return 1;

}//getLine

int reopenFile(char *pFile){

	if(in != NULL)
		fclose(in);

	in = fopen(pFile, "rt");		//Open input file
	if(in == NULL)
		return 0;
	else
		return 1;

}//reopenFile

void showUsage(){
	printf("Usage: conmat.exe <inputfile.txt>\n");
}//showUsage