/* Menu Thing for RVController - Calculator
 * 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"

void runCalculator(void) {
	char input[2] = {0};
	int32_t num1 = 0;
	int32_t num2 = 0;
	int32_t result = 0;

	printstr("\n\n\n\n\n\n");
	printstr("Enter number 1:\n> ");
	num1 = readint();
	printint(num1);
	printstr("\nEnter number 2:\n> ");
	num2 = readint();
	printint(num2);

	bool opok = false;
	while (!opok) {
		printstr("\nSelect operation:\n[+-*/%&|^<<>>] > ");
		readstr(input,2);
		printstr(input);

		switch (input[0]) {
			case '<':
			case '>':
				// Duplicate the character for these two to give the illusion that I bothered to store both
				// Then fall through
				printstr(input);
			case '+':
			case '-':
			case '*':
			case '/':
			case '%':
			case '&':
			case '|':
			case '^':
				printchar('\n');
				opok = true;
				break;
			default:
				printstr("\nInvalid operation\n");
		}
	}

	switch (input[0]) {
		case '+':
			printint(num1+num2);
			break;
		case '-':
			printint(num1-num2);
			break;
		case '*':
			printint(num1*num2);
			break;
		case '/':
			printint(num1/num2);
			break;
		case '%':
			printint(num1%num2);
			break;
		case '&':
			printint(num1&num2);
			break;
		case '|':
			printint(num1|num2);
			break;
		case '^':
			printint(num1^num2);
			break;
		case '<':
			if (num2 < 0 || num2 > 31) {
				printstr("Nice try.");
			} else {
				printint(num1<<num2);
			}
			break;
		case '>' :
			if (num2 < 0 || num2 > 31) {
				printstr("Nice try.");
			} else {
				printint(num1>>num2);
			}
			break;
	}

	printstr("\nPress any key");
	readstr(input,1);
}
