#include "command_generator.h"

#include <cstdio>

command_generator::command_generator() {
	m_operand1 = 0;
	m_operand2 = 0;
	m_operations[0] = '+';
	m_operations[1] = '-';
	m_operations[2] = 'x';
	m_operations[3] = '/';
	m_operations[4] = 'z';
	m_operation_index = 4;
	m_command_index = 0;
	sprintf(m_command, "%d%c%d\n", m_operand1, m_operations[m_operation_index], m_operand2);
}

void command_generator::set_callbacks(callback callbacks) {
	m_callbacks = callbacks;
}

char command_generator::get_character() {
	return get_next_character(get_command());
}

const char* command_generator::get_command() {
	if (m_command_index == COMMAND_LENGTH) {
		prepare_next_command();
		m_command_index = 0;
		sprintf(m_command, "%d%c%d\n", m_operand1, m_operations[m_operation_index], m_operand2);
	}

	return m_command;
}

char command_generator::get_next_character(const char* command) {
	return command[m_command_index++];
}

void command_generator::prepare_next_command() {
	m_operand2++;

	if (m_operand2 == 10) {
		m_operand1++;
		m_operand2 = 0;
	}

	if (m_operand1 == 10) {
		m_operand1 = 0;
		m_operation_index++;
	}

	if (m_operation_index == 5) {
		m_operation_index = 0;
		m_callbacks.stop(m_callbacks.opaque);
	}
}
