#include "DataGroup.hpp"

DataGroup::DataGroup(std::string sName) {
  pReferenceDataGroup = NULL;
  this->sName = sName;
  bCopy = false;
}

DataGroup::DataGroup(DataGroup* pReferenceDataGroup) {
  this->pReferenceDataGroup = pReferenceDataGroup;
  this->sName = pReferenceDataGroup->GetName();
  bCopy = true;
}

DataGroup::~DataGroup() {
  //don't delete the original data entries if the data group is a copy
  if (!bCopy) {
    while (vDataEntries.size() > 0) {
      std::vector<DataEntry*>::iterator it = vDataEntries.begin();

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

void DataGroup::AddDataEntry(DataEntry* pDataEntry) {
  vDataEntries.push_back(pDataEntry);
}

std::vector<DataEntry*> DataGroup::GetDataEntries() {
  return vDataEntries;
}

int DataGroup::GetSize() {
  return vDataEntries.size();
}

std::string DataGroup::GetName() {
  return sName;
}

bool DataGroup::IsCopy() {
  return bCopy;
}

DataGroup* DataGroup::GetReferenceDataGroup() {
  return pReferenceDataGroup;
}

double DataGroup::GetHighestValue() {
  double dHighestValue = 0.0;

  for (unsigned int i = 0; i < vDataEntries.size(); i++) {
    double dEntryValue = vDataEntries.at(i)->GetValue();

    if (dEntryValue > dHighestValue) {
      dHighestValue = dEntryValue;
    }
  }

  return dHighestValue;
}

double DataGroup::SumValues() {
  double dSummedValue = 0.0;

  for (unsigned int i = 0; i < vDataEntries.size(); i++) {
    dSummedValue += vDataEntries.at(i)->GetValue();
  }

  return dSummedValue;
}