/* Menu Thing for RVController - Screensaver
 * A product of Advanced Mesecons Devices, a Cheapie Systems company
 * This is free and unencumbered software released into the public domain.
 * See http://unlicense.org/ for more information */

#include <stdbool.h>
#include <stdint.h>
#include "rvcontroller-ecalls.h"

char screensaverbuf[] = "\n                    \n                    \n                    \n                    \n                    \n                    ";

int strlen(char *string) {
	int i;
	for (i = 0;string[i] != 0;i++) {}
	return i;
}

void screensaverWriteText(char *text, int column, int row, bool erase) {
	int textlen = strlen(text);
	if (column < 0) {
		// Cut the left side off of the text and change the start position to the left side of the screen
		text = text + (0 - column);
		textlen = textlen + column;
		column = 0;
	} else if (column + textlen > 19) {
		// Cut the right side off of the text
		textlen = 20 - column;
	}
	for (int i=0;i < textlen;i++) {
		if (erase) {
			screensaverbuf[(row*21)+column+i+1] = 0x20;
		} else {
			screensaverbuf[(row*21)+column+i+1] = text[i];
		}
	}
}

void doMarquee(void) {
	char text[] = "RVController ";
	int col = 19;
	int row = randomint(0,5);
	console_clearbuffer();
	while (console_readchar() == 0) {
		col--;
		if (col < (0 - strlen(text))) {
			col = 20;
			row = randomint(0,5);
		}
		screensaverWriteText(text,col,row,false);
		printstr(screensaverbuf);
	}
	screensaverWriteText(text,col,row,true);
}

void doBouncingText(void) {
	char text[] = "RVController";
	int col = 0;
	int row = 0;
	int xdir = 1;
	int ydir = 1;
	console_clearbuffer();
	while (console_readchar() == 0) {
		screensaverWriteText(text,col,row,true);
		col = col + xdir;
		row = row + ydir;
		if (col <= 0) { xdir = 1; }
		if (col >= (19 - strlen(text))) { xdir = -1; }
		if (row <= 0) { ydir = 1; }
		if (row >= 5) { ydir = -1; }
		screensaverWriteText(text,col,row,false);
		printstr(screensaverbuf);
	}
	screensaverWriteText(text,col,row,true);
}

void doMatrix(void) {
	int row[] = {-8,-4,-1,-5,-2}; // "Random" positions off the top of the screen to give "random" start delays
	int col[] = {1,18,2,12,9};
	int len[] = {3,4,5,6,7};
	char textscratch[] = " ";
	console_clearbuffer();
	while (console_readchar() == 0) {
		for (int i=0;i<=4;i++) {
			row[i]++;
			textscratch[0] = randomint(0x21,0x7a);
			if (row[i] <= 5 && row[i] >= 0) {
				screensaverWriteText(textscratch,col[i],row[i],false);
			}
			if ((row[i] - len[i]) <= 5 && (row[i] - len[i]) >= 0) {
				screensaverWriteText(textscratch,col[i],(row[i] - len[i]),true);
			} else if ((row[i] - len[i]) > 5 && randomint(0,1) == 1) {
				row[i] = 0 - randomint(1,3);
				col[i] = randomint(0,19);
				len[i] = randomint(2,8);
			}
		}
		printstr(screensaverbuf);
	}
	for (int i=0;i <= 5;i++) {
		screensaverWriteText("                    ",0,i,true);
	}
}

void runScreensaver(void) {
	char inputbuf[9];
	bool quit = false;
	while (!quit) {
		printstr("\n\n\n\n\n\n\n");
		printstr("SCREENSAVER\n");
		printstr("1: Marquee\n");
		printstr("2: Bouncing Text\n");
		printstr("3: Matrix\n");
		printstr("\n");
		printstr("0: Exit");
		readstr(inputbuf,2);
		switch (inputbuf[0]) {
			case '0':
				quit = true;
				break;
			case '1':
				doMarquee();
				quit = true;
				break;
			case '2':
				doBouncingText();
				quit = true;
				break;
			case '3':
				doMatrix();
				quit = true;
				break;
			default:
				printstr("\nInvalid option\n\nPress any key\n\n\n");
				readstr(inputbuf,1);
		}
	}
}
