#include "./home.h"
#include "./move.h"
#include "fonts/FreeSansBold18pt7b.trimmed.h"
#include "include/config.h"
#include "lib/settings.h"
#include "lib/motorstate.h"


#define HOME_FONT_BASELINE 25
#define HOME_FONT_HEIGHT 42

#define HOME_MOVING_REFRESHRATE 250

#define PRESET_MARGIN 7

// HOME_FONT_BASELINE is not a mistake, as there are no characters used which
// go below the baseline.
#define PRESET_LINEHEIGHT (HOME_FONT_BASELINE + (2 * PRESET_MARGIN))



void HomeScreen::onShow()
{
  this->showTime = State.CurrentTime;

  auto display = this->getDisplay();

  display->setFont(&FreeSansBold18pt7bTrimmed);
  display->fillScreen(Config::ColorMenuBackground);

  this->drawPreset1();
  this->drawMenu();
  this->drawPreset2();

  // TODO if the height does not match either preset, display the actual height
}


void HomeScreen::onButton(Button button)
{
  if (this->idle)
  {
    this->getScreenManager()->displayOn();
    this->idle = false;
    this->showTime = State.CurrentTime;

    // TODO should preset buttons activate immediately?
    return;
  }

  switch (button)
  {
    case Button::Up:
      motorStateMoveTo(Settings.Height.Preset[0]);
      this->getScreenManager()->show<MoveScreen>();
      break;

    case Button::Down:
      motorStateMoveTo(Settings.Height.Preset[1]);
      this->getScreenManager()->show<MoveScreen>();
      break;

    case Button::Menu:
      // TODO show menu
      break;
  }
}


void HomeScreen::onTick()
{
  if (!this->idle && State.CurrentTime - this->showTime >= Config::DisplayIdleTime)
  {
    this->getScreenManager()->displayOff();
    this->idle = true;
  }
}


void HomeScreen::drawPreset1()
{
  this->drawPreset(1, 0, Settings.Height.Preset[0]);
}


void HomeScreen::drawPreset2()
{
  this->drawPreset(2, Config::DisplayHeight - PRESET_LINEHEIGHT, Settings.Height.Preset[1]);
}


void HomeScreen::drawPreset(uint8_t number, uint8_t y, uint16_t value)
{
  auto display = this->getDisplay();
  uint16_t numberColor;
  uint16_t textColor;
  uint16_t backgroundColor;

  // An exact comparison is enough here, the movement code takes care of that if it's "close enough"
  if (value == State.CurrentHeight)
  {
    textColor = Config::ColorPresetSelectedText;
    numberColor = Config::ColorPresetSelectedNumber;
    backgroundColor = Config::ColorPresetSelectedBackground;
  }
  else
  {
    textColor = Config::ColorPresetText;
    numberColor = Config::ColorPresetNumber;
    backgroundColor = Config::ColorPresetBackground;
  }

  display->fillRect(0, y, Config::DisplayWidth, PRESET_LINEHEIGHT, backgroundColor);

  display->setCursor(0, y + HOME_FONT_BASELINE + PRESET_MARGIN);
  display->setTextColor(numberColor);
  display->print("P");
  display->print(number);


  char textValue[6];
  getDisplayHeight(&textValue[0], value);
  textValue[5] = 0;

  // Calculate the center position
  int16_t textX;
  int16_t textY;
  uint16_t textW;
  uint16_t textH;
  display->getTextBounds(&textValue[0], 0, 0, &textX, &textY, &textW, &textH);

  textX = (Config::DisplayWidth - textW) / 2;
  display->setCursor(textX, y + HOME_FONT_BASELINE + PRESET_MARGIN);
  display->setTextColor(textColor);
  display->print(textValue);
}


void HomeScreen::drawMenu()
{
}
