#include "Label.hpp"
#include "glew.h"

Label::Label(QString sText, int iStyle, int iLeft, int iTop, int iOrientation, int iAlignment) {
  iType = LABEL;

  this->sText = sText;
  this->iLeft = iLeft;
  this->iOriginalLeft = iLeft;
  this->iTop = iTop;
  this->iOriginalTop = iTop;
  this->iStyle = iStyle;
  this->iOrientation = iOrientation;
  this->iAlignment = iAlignment;
  oColor = Qt::black;

  switch (iStyle) {
    case FONTSTYLE_HEADLINE: {
      oFont = QFont("Calibri", 16, QFont::Bold);

      break;
    }
    case FONTSTYLE_ELEMENT: {
      oFont = QFont("Calibri", 13, QFont::Bold);

      break;
    }
    case FONTSTYLE_BAR: {
      oFont = QFont("Calibri", 13, QFont::Bold);

      break;
    }
    default: {
      oFont = QFont("Calibri", 13, QFont::Bold);
    }
  }
  
  QFontMetrics oMetrics = QFontMetrics(oFont);
  int iBorder = qMax(4, oMetrics.leading());
  
  QRect oRect;

  switch (this->iAlignment) {
    case Qt::AlignCenter: {
      oRect = oMetrics.boundingRect(0, 0, WIDTH - 2 * iBorder, int(HEIGHT * 0.125), Qt::AlignCenter | Qt::TextWordWrap, sText);
      iWidth = oRect.width();
      iHeight = oRect.height();

      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop + iWidth / 2;
      }
      else {
        this->iLeft = iLeft - iWidth / 2;
        this->iTop = iTop - iHeight / 2;
      }

      break;
    }
    case Qt::AlignLeft: {
      oRect = oMetrics.boundingRect(0, 0, WIDTH - 2 * iBorder, int(HEIGHT * 0.125), Qt::AlignLeft | Qt::TextWordWrap, sText);
      iWidth = oRect.width();
      iHeight = oRect.height();

      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop;
      }
      else {
        this->iLeft = iLeft;
        this->iTop = iTop - iHeight / 2;
      }

      break;
    }
    case Qt::AlignRight: {
      oRect = oMetrics.boundingRect(0, 0, WIDTH - 2 * iBorder, int(HEIGHT * 0.125), Qt::AlignRight | Qt::TextWordWrap, sText);
      iWidth = oRect.width();
      iHeight = oRect.height();

      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop + iWidth;
      }
      else {
        this->iLeft = iLeft - iWidth;
        this->iTop = iTop - iHeight / 2;
      }

      break;
    }
    default: {
      oRect = oMetrics.boundingRect(0, 0, WIDTH - 2 * iBorder, int(HEIGHT * 0.125), Qt::AlignCenter | Qt::TextWordWrap, sText);
      iWidth = oRect.width();
      iHeight = oRect.height();

      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop + iWidth / 2;
      }
      else {
        this->iLeft = iLeft - iWidth / 2;
        this->iTop = iTop - iHeight / 2;
      }
    }
  }
}

Label::~Label() {
  //
}

void Label::Draw(QPainter* pPainter) {
  pPainter->setFont(oFont);
  pPainter->setRenderHint(QPainter::TextAntialiasing);
  pPainter->setPen(oColor);

  if (iOrientation == ORIENTATION_VERTICAL) {
    pPainter->save();
    pPainter->translate(iLeft, iTop);
    pPainter->rotate(270.0f);
    pPainter->drawText(0, 0, sText);
    pPainter->restore();
  }
  else {
    pPainter->drawText(iLeft, iTop, iWidth, iHeight, Qt::AlignCenter | Qt::TextWordWrap, sText);
  }
}

void Label::DrawLabels(QPainter* pPainter) {
  UNREFERENCED(pPainter);
}

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

void Label::ChangePosition(int iLeft, int iTop) {
  switch (this->iAlignment) {
    case Qt::AlignCenter: {
      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop + iWidth / 2;
      }
      else {
        this->iLeft = iLeft - iWidth / 2;
        this->iTop = iTop - iHeight / 2;
      }

      break;
    }
    case Qt::AlignLeft: {
      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop;
      }
      else {
        this->iLeft = iLeft;
        this->iTop = iTop - iHeight / 2;
      }

      break;
    }
    case Qt::AlignRight: {
      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop + iWidth;
      }
      else {
        this->iLeft = iLeft - iWidth;
        this->iTop = iTop - iHeight / 2;
      }

      break;
    }
    default: {
      if (iOrientation == ORIENTATION_VERTICAL) {
        this->iLeft = iLeft + iHeight / 2 - VERTICAL_TEXT_OFFSET;
        this->iTop = iTop + iWidth / 2;
      }
      else {
        this->iLeft = iLeft - iWidth / 2;
        this->iTop = iTop - iHeight / 2;
      }
    }
  }
}

void Label::ChangeText(QString sText) {
  this->sText = sText;
}

void Label::ChangeAlpha(float fDeltaAlpha) {
  oColor.setAlphaF(MIN(MAX(oColor.alphaF() + fDeltaAlpha, 0.0f), 1.0f));
}

float Label::GetRed() {
  return oColor.redF();
}

float Label::GetGreen() {
  return oColor.greenF();
}

float Label::GetBlue() {
  return oColor.blueF();
}

float Label::GetAlpha() {
  return oColor.alphaF();
}

QString Label::GetText() {
  return sText;
}

int Label::GetLeft() {
  return iOriginalLeft;
}

int Label::GetTop() {
  return iOriginalTop;
}

int Label::GetOrientation() {
  return iOrientation;
}

int Label::GetAlignment() {
  return iAlignment;
}

int Label::GetStyle() {
  return iStyle;
}