/* Drawer Capacity Calculator for RVController
 * 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"

#define BASE_SLOTS 32
#define STACK_MAX 99
#define STEEL_UPGRADE_MULT 1
#define GOLD_UPGRADE_MULT 2
#define OBSIDIAN_UPGRADE_MULT 3
#define DIAMOND_UPGRADE_MULT 7
#define MITHRIL_UPGRADE_MULT 12

bool checkUpgradeCount(int32_t count,int32_t maxcount) {
	return (count >= 0 && count <= maxcount);
}

void main(void) {
	while (true) {
		int32_t capacity = BASE_SLOTS * STACK_MAX;
		uint8_t upgrades_used = 0;
		printstr("\n\n\nSettings:\n");
		printstr("Base Stack Count: ");
		printint(BASE_SLOTS);
		printstr("\nMax Stack Size: ");
		printint(STACK_MAX);
		printchar('\n');

		int32_t upgcount = 0;
		while (true) {
			printstr("\nSteel Upgrades?\n[0-5] > ");
			upgcount = readint();
			printint(upgcount);
			printchar('\n');
			if (checkUpgradeCount(upgcount,5)) {
				break;
			} else {
				printstr("Invalid entry");
			}
		}
		upgrades_used += upgcount;
		capacity += BASE_SLOTS * STACK_MAX * STEEL_UPGRADE_MULT * upgcount;

		if (upgrades_used < 5) {
			while (true) {
				printstr("\nGold Upgrades?\n[0-");
				printint(5-upgrades_used);
				printstr("] > ");
				upgcount = readint();
				printint(upgcount);
				printchar('\n');
				if (checkUpgradeCount(upgcount,5-upgrades_used)) {
					break;
				} else {
					printstr("Invalid entry");
				}
			}
			upgrades_used += upgcount;
			capacity += BASE_SLOTS * STACK_MAX * GOLD_UPGRADE_MULT * upgcount;
		}

		if (upgrades_used < 5) {
			while (true) {
				printstr("\nObsidian Upgrades?\n[0-");
				printint(5-upgrades_used);
				printstr("] > ");
				upgcount = readint();
				printint(upgcount);
				printchar('\n');
				if (checkUpgradeCount(upgcount,5-upgrades_used)) {
					break;
				} else {
					printstr("Invalid entry");
				}
			}
			upgrades_used += upgcount;
			capacity += BASE_SLOTS * STACK_MAX * OBSIDIAN_UPGRADE_MULT * upgcount;
		}

		if (upgrades_used < 5) {
			while (true) {
				printstr("\nDiamond Upgrades?\n[0-");
				printint(5-upgrades_used);
				printstr("] > ");
				upgcount = readint();
				printint(upgcount);
				printchar('\n');
				if (checkUpgradeCount(upgcount,5-upgrades_used)) {
					break;
				} else {
					printstr("Invalid entry");
				}
			}
			upgrades_used += upgcount;
			capacity += BASE_SLOTS * STACK_MAX * DIAMOND_UPGRADE_MULT * upgcount;
		}

		if (upgrades_used < 5) {
			while (true) {
				printstr("\nMithril Upgrades?\n[0-");
				printint(5-upgrades_used);
				printstr("] > ");
				upgcount = readint();
				printint(upgcount);
				printchar('\n');
				if (checkUpgradeCount(upgcount,5-upgrades_used)) {
					break;
				} else {
					printstr("Invalid entry");
				}
			}
			upgrades_used += upgcount;
			capacity += BASE_SLOTS * STACK_MAX * MITHRIL_UPGRADE_MULT * upgcount;
		}

		printstr("\n1 slot: ");
		printint(capacity);
		printstr("\n2 slots: ");
		printint(capacity/2);
		printstr("\n4 slots: ");
		printint(capacity/4);
		printstr("\nPress 0 to run again\n");
		upgcount = readint();
	}
}
