#include "Bar.hpp"
#include "glew.h"

Bar::Bar(DataEntry* pDataEntry, float fLeft, float fTop, float fBottom, float fWidth, Label* pLabel) {
  iType = BAR;
  this->pDataEntry = pDataEntry;
  this->fLeft = fLeft;
  this->fTop = fTop;
  this->fBottom = fBottom;
  this->fWidth = fWidth;
  fHeight = fTop - fBottom;

  vVertices = new glm::vec4[4];
  vVertices[0] = glm::vec4(fLeft, fBottom, 0.0f, 1.0f);
  vVertices[1] = glm::vec4(fLeft + fWidth, fBottom, 0.0f, 1.0f);
  vVertices[2] = glm::vec4(fLeft + fWidth, fTop, 0.0f, 1.0f);
  vVertices[3] = glm::vec4(fLeft, fTop, 0.0f, 1.0f);

  vColors = new glm::vec4[4];
  vColors[0] = pDataEntry->GetColor();
  vColors[1] = pDataEntry->GetColor();
  vColors[2] = pDataEntry->GetColor();
  vColors[3] = pDataEntry->GetColor();

  vIndices = new int[6];
  vIndices[0] = 0;
  vIndices[1] = 1;
  vIndices[2] = 2;
  vIndices[3] = 2;
  vIndices[4] = 3;
  vIndices[5] = 0;

  pShader = new Shader(sSolidColorVertSource, sSolidColorFragSource);

  if (pLabel != NULL) {
    this->pLabel = new Label(pLabel->GetText(), pLabel->GetStyle(), pLabel->GetLeft(),
      pLabel->GetTop(), pLabel->GetOrientation(), pLabel->GetAlignment());
  }
  else {
    this->pLabel = new Label(QObject::tr(pDataEntry->GetName().c_str()), FONTSTYLE_BAR,
      (fLeft + fWidth / 2.0f + 1.0f) / 2.0f * WIDTH,
      (1.0f - (ORIGIN_Y + 1.0f) / 2.0f) * HEIGHT + BAR_TEXT_OFFSET, ORIENTATION_VERTICAL, Qt::AlignRight);
  }
}

Bar::~Bar() {
  delete vVertices;
  delete vColors;
  delete vIndices;
  delete pLabel;
  delete pShader;
}

void Bar::Draw(QPainter* pPainter) {
  pPainter->setRenderHint(QPainter::Antialiasing);
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  pShader->Bind();

  glEnableVertexAttribArray(pShader->GetVertexLocation());
  glVertexAttribPointer(pShader->GetVertexLocation(), 4, GL_FLOAT, GL_FALSE, 0, vVertices);

  glEnableVertexAttribArray(pShader->GetColorLocation());
  glVertexAttribPointer(pShader->GetColorLocation(), 4, GL_FLOAT, GL_FALSE, 0, vColors);

  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, vIndices);

  pShader->Unbind();
}

void Bar::DrawLabels(QPainter* pPainter) {
  pLabel->Draw(pPainter);
}

void Bar::ChangePosition(float fDeltaLeft, float fDeltaTop, float fDeltaBottom) {
  fLeft += fDeltaLeft;
  fTop += fDeltaTop;
  fBottom += fDeltaBottom;

  if (fTop < ORIGIN_Y) {
    fTop = 0;
  }

  vVertices[0] = glm::vec4(fLeft, fBottom, 0.0f, 1.0f);
  vVertices[1] = glm::vec4(fLeft + fWidth, fBottom, 0.0f, 1.0f);
  vVertices[2] = glm::vec4(fLeft + fWidth, fTop, 0.0f, 1.0f);
  vVertices[3] = glm::vec4(fLeft, fTop, 0.0f, 1.0f);

  pLabel->ChangePosition((fLeft + fWidth / 2.0f + 1.0f) / 2.0f * WIDTH,
    (1.0f - (ORIGIN_Y + 1.0f) / 2.0f) * HEIGHT + BAR_TEXT_OFFSET);
}

void Bar::Scale(float fDeltaWidth, float fDeltaHeight) {
  fWidth += fDeltaWidth;
  fHeight += fDeltaHeight;
  fTop = fBottom + fHeight;

  vVertices[0] = glm::vec4(fLeft, fBottom, 0.0f, 1.0f);
  vVertices[1] = glm::vec4(fLeft + fWidth, fBottom, 0.0f, 1.0f);
  vVertices[2] = glm::vec4(fLeft + fWidth, fTop, 0.0f, 1.0f);
  vVertices[3] = glm::vec4(fLeft, fTop, 0.0f, 1.0f);
}

void Bar::ChangeAlpha(float fDeltaAlpha) {
  for (int i = 0; i < 4; i++) {
    vColors[i] = vColors[i] + glm::vec4(0.0f, 0.0f, 0.0f, fDeltaAlpha);
  }

  pLabel->ChangeAlpha(fDeltaAlpha);
}