/* Menu Thing for RVController - Digilines
 * 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 <stdint.h>
#include <stdbool.h>
#include "rvcontroller-ecalls.h"

char dispbuf[] = "\nLast channel:\n(none)              \nLast message:\n(none)              \ns: Send\nq: Quit";

void dispWrite(char *text,int pos) {
	int outpos;
	for (int i=0;;i++) {
		if (text[i] == 0) { break; }
		outpos = pos + i;
		dispbuf[outpos] = text[i];
	}
}

void sendUI(void) {
	char channeloutbuf[19];
	char msgoutbuf[19];
	printstr("\n\n\n\n\nEnter channel:\n> ");
	readstr(channeloutbuf,19);
	printstr(channeloutbuf);
	printstr("\nEnter message:\n> ");
	readstr(msgoutbuf,19);
	printstr(msgoutbuf);
	digiline_send(channeloutbuf,msgoutbuf);
	printstr("\nSent!\nPress any key");
	readstr(channeloutbuf,1);
}

void runDigilines(void) {
	char channelinbuf[21];
	char msginbuf[21];
	char kbinput;
	bool quit = false;

	console_clearbuffer();
	digiline_clearbuffer();
	dispWrite("(none)              ",15);
	dispWrite("(none)              ",50);

	while (!quit) {
		if (digiline_bufferlevel() > 0) {
			digiline_receive(channelinbuf,21,msginbuf,21);
			dispWrite("                    ",15);
			dispWrite("                    ",50);
			dispWrite(channelinbuf,15);
			dispWrite(msginbuf,50);
		}
		printstr(dispbuf);

		kbinput = console_readchar();
		switch (kbinput) {
			case 's':
				sendUI();
				break;
			case 'q':
				quit = true;
				break;
		}
	}
}
