#include "BlendTransition.hpp"

BlendTransition::BlendTransition(AlterableObject* pAlterableObject,
  float fAlpha, int iTotalSteps) {
  iType = BLEND_COLOR_TRANSITION;
  bStarted = false;
  this->pAlterableObject = pAlterableObject;
  this->iTotalSteps = iTotalSteps;

  //linear blending
  if (pAlterableObject->GetType() == GRID_LINE) {
    GridLine* pGridLine = (GridLine*) pAlterableObject;

    fAlphaStep = (fAlpha - pGridLine->GetAlpha()) / iTotalSteps;
  }
  else if ((pAlterableObject->GetType() == BAR) || (pAlterableObject->GetType() == GROUPED_BAR) ||
    (pAlterableObject->GetType() == STACKED_BAR) || (pAlterableObject->GetType() == PIE_SECTOR) ||
    (pAlterableObject->GetType() == PIE_SLICE) || (pAlterableObject->GetType() == MORPH_ELEMENT)) {
    DataElement* pDataElement = (DataElement*) pAlterableObject;

    fAlphaStep = (fAlpha - pDataElement->GetAlpha()) / iTotalSteps;
  }
  else if (pAlterableObject->GetType() == LABEL) {
    Label* pLabel = (Label*) pAlterableObject;

    fAlphaStep = (fAlpha - pLabel->GetAlpha()) / iTotalSteps;
  }
  else if (pAlterableObject->GetType() == AXIS) {
    fAlphaStep = fAlpha / iTotalSteps;
  }
  else {
    fAlphaStep = 0.0f;
  }

  iStepsDone = 0;
}

BlendTransition::~BlendTransition() {
  while (vChildren.size() > 0) {
    std::vector<Transition*>::iterator it = vChildren.begin();

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

  while (vToBeDeleted.size() > 0) {
    std::vector<AlterableObject*>::iterator it = vToBeDeleted.begin();

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

void BlendTransition::Update() {
  if (!bStarted) {
    Start();
  }

  pAlterableObject->ChangeAlpha(fAlphaStep);
  iStepsDone++;
}

bool BlendTransition::Complete() {
  if ((bStarted) && (iStepsDone >= iTotalSteps)) {
    return true;
  }
  else {
    return false;
  }
}