#include "GroupedBar.hpp"

GroupedBar::GroupedBar(DataGroup* pDataGroup, float fLeft, float fWidth, double dHighestGridValue) {
  iType = GROUPED_BAR;
  this->pDataGroup = pDataGroup;
  this->fLeft = fLeft;

  std::vector<DataEntry*> vDataEntries = pDataGroup->GetDataEntries();
  
  float fSpace = fWidth / 2.0f;

  for (unsigned int i = 0; i < vDataEntries.size(); i++) {
    float fTop = vDataEntries.at(i)->GetValue() / dHighestGridValue * (UPPER_Y - ORIGIN_Y) + ORIGIN_Y;
    float fInnerLeft;

    if (i == 0) {
      fInnerLeft = fLeft;
    }
    else {
      fInnerLeft = fLeft + i * (fWidth + fSpace);
    }

    vBars.push_back(new Bar(vDataEntries.at(i), fInnerLeft, fTop, ORIGIN_Y, fWidth));
  }
}

GroupedBar::~GroupedBar() {
  while (vBars.size() > 0) {
    std::vector<Bar*>::iterator it = vBars.begin();

    if (it != vBars.end()) {
      delete *it;
      vBars.erase(it);
    }
  }
}

void GroupedBar::Draw(QPainter* pPainter) {
  for (unsigned int i = 0; i < vBars.size(); i++) {
    vBars.at(i)->Draw(pPainter);
  }
}

void GroupedBar::DrawLabels(QPainter* pPainter) {
  for (unsigned int i = 0; i < vBars.size(); i++) {
    vBars.at(i)->DrawLabels(pPainter);
  }
}

void GroupedBar::ChangePosition(float fDeltaLeft, float fDeltaTop, float fDeltaBottom) {
  UNREFERENCED(fDeltaLeft);
  UNREFERENCED(fDeltaTop);
  UNREFERENCED(fDeltaBottom);
}

void GroupedBar::Scale(float fDeltaWidth, float fDeltaHeight) {
  UNREFERENCED(fDeltaWidth);
  UNREFERENCED(fDeltaHeight);
}

void GroupedBar::ChangeAlpha(float fDeltaAlpha) {
  UNREFERENCED(fDeltaAlpha);
}

DataGroup* GroupedBar::GetDataGroup() {
  return pDataGroup;
}

std::vector<Bar*> GroupedBar::GetBars() {
  return vBars;
}