#include "HistoryModule.h"
#include "printError.h"
#include <stdio.h>

#define MAX_HIST_LENGTH 1000

// using namespace std;

void HistoryModule::initHistoryModule() {
  if (MAX_HIST_LENGTH <= vHistory.max_size()) {
    historyLength = MAX_HIST_LENGTH;
  } else {
    historyLength = vHistory.max_size();
  }

  delimitationMode = DELIMITATE_MODE_SRWF;
}

HistoryModule::HistoryModule() {
  set_name(NO_NAME);
  initHistoryModule();
}

HistoryModule::HistoryModule(const char *name) {
  initHistoryModule();
  set_name(name);
}

bool HistoryModule::set_maxHistoryLength(unsigned int length) {
  if (length <= MAX_HIST_LENGTH && length <= vHistory.max_size()) {
    // this->historyLength = historyLength;
    historyLength = length;
    return true;
  }
  return false;
}

unsigned int HistoryModule::get_maxHistoryLength() { return historyLength; }

bool HistoryModule::set_delimitationMode(int delimitationMode) {
  if (delimitationMode > DELIMITATE_MODE_LBOUND &&
      delimitationMode < DELIMITATE_MODE_UBOUND) {
    this->delimitationMode = delimitationMode;
    return true;
  }
  return false;
}

int HistoryModule::get_delimitationMode() { return delimitationMode; }

bool HistoryModule::add_entry(float entryValue) {
  if (vHistory.size() <= historyLength) {
    HistoryEntry *historyEntry = new HistoryEntry();
    if (historyEntry != NULL) {
      historyEntry->set_entryValue(entryValue);
      try {
        vHistory.push_back(historyEntry);
        return true;
      } catch (bad_alloc &error) {
        printError("bad_alloc caught: ", error.what());
      }
    } else {
      printError("Couldn't create HistoryEntry!");
    }
  } else {
    switch (delimitationMode) {
    case DELIMITATE_MODE_SRWF:
      printError("History is already full!");
      break;
    default:
      printError("History is already full! DelimitationMode isn't set!");
      break;
    }
  }
  return false;
}

unsigned int HistoryModule::get_numberOfEntries() { return vHistory.size(); }

