#include "./home.h"
#include "./move.h"
#include "./move-sensorerror.h"
#include "include/config.h"
#include "include/metrics.h"
#include "lib/settings.h"
#include "lib/control.h"
#include "lib/state.h"
#include "./menu.h"



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

  this->display->setFont(Metrics::LargeFont);
  this->display->setTextSize(Metrics::LargeFontTextSize);

  this->display->fillScreen(Config::ColorHomeBackground);

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

  this->drawNonPresetHeight();
}


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

    // Preset buttons activate immediately
    if (button == Button::Middle)
      return;
  }

  uint16_t targetHeight;

  switch (button)
  {
    case Button::Top:
      targetHeight = Settings.Height.Preset[0];
      break;

    case Button::Bottom:
      targetHeight = Settings.Height.Preset[1];
      break;

    case Button::Middle:
      this->screenManager->show<MenuScreen>();
      return;
  }


  Control.moveStart(targetHeight);
  this->screenManager->show<MoveScreen>();
}


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


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


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


void HomeScreen::drawNonPresetHeight()
{
  auto y = Metrics::LargeTextLineHeight;

  if (Control.getCurrentHeight() != Settings.Height.Preset[0] &&
      Control.getCurrentHeight() != Settings.Height.Preset[1])
  {
    this->display->setTextColor(Config::ColorNonPresetText);
    this->drawHeight(y, Control.getCurrentHeight());
  }
}


void HomeScreen::drawPreset(int16_t y, uint16_t value)
{
  uint16_t textColor;
  uint16_t backgroundColor;
  uint16_t arrowColor;

  // An exact comparison is enough here, the movement code takes care of that if it's "close enough"
  if (value == Control.getCurrentHeight())
  {
    textColor = Config::ColorPresetSelectedText;
    backgroundColor = Config::ColorPresetSelectedBackground;
    arrowColor = 0;
  }
  else
  {
    textColor = Config::ColorPresetText;
    backgroundColor = Config::ColorPresetBackground;
    arrowColor = Config::ColorPresetArrow;
  }

  this->display->fillRect(0, y, Config::DisplayWidth, Metrics::LargeTextLineHeight, backgroundColor);

  if (arrowColor)
    this->drawArrowLeft(Metrics::ArrowMargin, y + Metrics::LargeTextLineHArrowYOffset, arrowColor);

  this->display->setTextColor(textColor);
  this->drawHeight(y, value);
}


void HomeScreen::drawHeight(int16_t y, uint16_t value)
{
  char textValue[6];
  Control.getDisplayHeight(&textValue[0], value);

  this->printCentered(&textValue[0], y + Metrics::LargeTextLineYOffset);
}


void HomeScreen::drawMenu()
{
  this->drawArrowLeft(Metrics::ArrowMargin, Metrics::MiddleLargeTextLineY, Config::ColorHomeMenuArrow);

  this->display->setTextColor(Config::ColorHomeMenuText);
  this->printCentered("Menu", Metrics::MiddleLargeTextLineY + Metrics::LargeTextLineYOffset);
}
