#include "./screen.h"
#include "include/config.h"
#include "include/metrics.h"


uint16_t BaseScreen::printCentered(const char* text, int16_t y)
{
  int16_t textX;
  int16_t textY;
  uint16_t textW;
  uint16_t textH;
  this->display->getTextBounds(text, 0, 0, &textX, &textY, &textW, &textH);

  textX = (Config::DisplayWidth - textW) / 2;
  this->display->setCursor(textX, y);
  this->display->print(text);

  return textW;
}


void BaseScreen::drawLargeTextLineCentered(const char* text, int16_t y, uint16_t textColor, uint16_t backgroundColor)
{
  this->display->fillRect(0, y, Config::DisplayWidth, Metrics::LargeTextLineHeight, backgroundColor);
  this->display->setTextColor(textColor);
  this->printCentered(text, y + Metrics::LargeTextLineYOffset);
}


void BaseScreen::drawArrowLeft(int16_t x, int16_t y, uint16_t color)
{
  this->display->fillTriangle(
    x + Metrics::HArrowWidth, y,                          // Top right
    x, y + (Metrics::HArrowHeight / 2),                   // Middle left
    x + Metrics::HArrowWidth, y + Metrics::HArrowHeight,  // Bottom right
    color);
}


void BaseScreen::drawArrowRight(int16_t x, int16_t y, uint16_t color)
{
  this->display->fillTriangle(
    x, y,                                                         // Top left
    x +  + Metrics::HArrowWidth, y + (Metrics::HArrowHeight / 2), // Middle right
    x, y + Metrics::HArrowHeight,                                 // Bottom left
    color);
}


void BaseScreen::drawArrowUp(int16_t x, int16_t y, uint16_t color)
{
  this->display->fillTriangle(
    x + (Metrics::VArrowWidth / 2), y,                    // Top middle
    x, y + Metrics::VArrowHeight,                         // Bottom left
    x + Metrics::VArrowWidth, y + Metrics::VArrowHeight,  // Bottom right
    color);
}


void BaseScreen::drawArrowDown(int16_t x, int16_t y, uint16_t color)
{
  this->display->fillTriangle(
    x, y,                                                       // Top left
    x + Metrics::VArrowWidth, y,                                // Top right
    x + (Metrics::VArrowWidth / 2), y + Metrics::VArrowHeight,  // Bottom middle
    color);
}


void ScreenManager::init()
{
  pinMode(Config::DisplayPinBL, OUTPUT);
  digitalWrite(Config::DisplayPinBL, HIGH);
}


void ScreenManager::displayOff()
{
  digitalWrite(Config::DisplayPinBL, LOW);
  this->display->sendCommand(ST77XX_SLPIN);
}


void ScreenManager::displayOn()
{
  this->display->sendCommand(ST77XX_SLPOUT);
  digitalWrite(Config::DisplayPinBL, HIGH);
}
