#include "ProgressPrinter.h"

#include <cmath>
#include <iomanip>

#include "fsfw/serviceinterface/ServiceInterfaceStream.h"

ProgressPrinter::ProgressPrinter(std::string name, uint32_t numSteps, float percentageResolution)
    : name(name), numSteps(numSteps), percentageResolution(percentageResolution) {}

ProgressPrinter::~ProgressPrinter() {}

void ProgressPrinter::print(uint32_t currentStep) {
  float progressInPercent = static_cast<float>(currentStep) / static_cast<float>(numSteps) * 100;
  if (progressInPercent >= nextProgressPrint) {
    sif::info << name << " progress: " << std::setprecision(4) << progressInPercent << " %"
              << std::endl;
    nextProgressPrint += percentageResolution;
  }
  if (nextProgressPrint - progressInPercent < 0) {
    nextProgressPrint =
        (std::floor(progressInPercent / percentageResolution) * percentageResolution) +
        percentageResolution;
  }
}