#include "procOperator.h"

#define PROC_COLOR 0xD
#define S_LEN 2


procOperator::procOperator(RTKMailbox *mb_led, RTKMailbox *mb_log, RTKSemaphore *stop, Log *log) {
	this->sem = stop;
	this->mb_led = mb_led;
	this->mb_log = mb_log;
	this->log = log;
	LogData ld("Process Operator startet");
	this->log->skriv(ld);
}

procOperator::~procOperator() {}

void procOperator::run() {
	char *com = new char[S_LEN];
	char *logstr = new char[256];
	strcpy(logstr,"Kommando indtastet: ");
	WWindow *win = WNewWindow(5, 2, 36, 9, 500, PROC_COLOR, " Operator: ");
	WSetUserInput(KBGetCh); // input fra keyboard
	while (true) {
		WPutC( win, 0x0C );
		Wprintf(win, "h: HELP\nl: LED\nv: VIS\nn: NULSTIL\nx: EXIT\nIndtast kommando: ");
		WGetS(win, com, S_LEN);

		com[1] = 0;
		logstr[ strlen("Kommando indtastet: ") ] = 0;
		LogData ld( strcat(logstr, com) );

		switch (com[0]) {
			case 'x':
				this->log->skriv(ld);
				RTKSignal( *(this->sem) );
				break;
			case 'l':
				this->log->skriv(ld);
				do {
					WPutC( win, 0x0C );
					Wprintf(win, "LED:\nn: toggle nr n [0-7]\na toggle all\nx exit\nIndtast kommando:");
					WGetS(win, com, S_LEN);
				} while ( ! ( (com[0]-'0' >= 0 && com[0]-'7' <= 7) || com[0] == 'a' || com[0] == 'x') );
				if (com[0] != 'x')
					RTKPut( *(this->mb_led), &com[0] );
				break;

			case 'v':
				this->log->skriv(ld);
				RTKPut( *(this->mb_log), &com[0] );
				break;

			case 'n':
				this->log->skriv(ld);
				RTKPut( *(this->mb_log), &com[0] );
				break;

			case 'h':
				this->log->skriv(ld);
				WPutC( win, 0x0C );
				Wprintf(win, "Indtast dato for i fredags");
				WGetS(win, com, S_LEN-1);
				break;

			default:
				Wprintf(win, "Ugyldig kommando!");
		}
	}
	delete [] logstr;
	delete [] com;
}