#include "gclist.h"
#include "catalog.h"
#include "persons.h"
#include "stage.h"
#include <list>
#include <string>

GCList::GCList(const CyclistCatalog &cc) {
  GCEntry gc;
  for (const auto &cyclist : cc.Data()) {
    gc.rider = &cyclist;
    gc.totalDelay = 0;
    gc_.push_back(gc);
  }
}

void GCList::ApplyStage(const Stage& st) {
  std::map<std::string, int> stageDelay = st.GetDelays();
  for (auto &aux : gc_) {
    Cyclist c = *(aux.rider);
    for (auto &a : stageDelay) {
      if (c.GetCyclistId() == a.first) {
        aux.totalDelay += a.second;
      }
    }
  }

  gc_.sort([](const GCEntry &a, const GCEntry &b) {
    return a.totalDelay < b.totalDelay;
  });
}

std::string GCList::GetClassification(int n) {
  int j = 0;
  std::string res = "";

  for (auto &a : gc_) {
    if (j < n) {
      j++;
      res += std::to_string(j);
      res += ". ";
      res += a.rider->GetName();
      res += ", ";
      res += a.rider->GetCyclistId();
      res += ", ";
      res += std::to_string(a.totalDelay);
      res += "\n";
    }
  }

  return res;
}
