#include "./menu.h"
#include "./home.h"
#include "./calibrate.h"
#include "./manual.h"
#include "./presets.h"
#include "include/config.h"
#include "include/metrics.h"


void MenuScreen::onShow()
{
  this->display->setFont(Metrics::LargeFont);
  this->display->setTextSize(Metrics::LargeFontTextSize);

  this->display->fillScreen(Config::ColorHomeBackground);
  this->drawLargeTextLineCentered("Menu", 0, Config::ColorMenuHeaderText, Config::ColorMenuHeaderBackground);

  for (uint8_t i = 0; i < (uint8_t)MenuItem::__Count; i++)
    this->drawMenuItem(i);
}


void MenuScreen::onButton(Button button)
{
  uint8_t previousIndex = this->itemIndex;

  switch (button)
  {
    case Button::Middle:
      this->activateMenuItem((MenuItem)this->itemIndex);
      return;

    case Button::Top:
      if (this->itemIndex == 0)
        this->itemIndex = (uint8_t)MenuItem::__Count;

      this->itemIndex--;
      break;

    case Button::Bottom:
      this->itemIndex++;
      if (this->itemIndex == (uint8_t)MenuItem::__Count)
        this->itemIndex = 0;

      break;
  }

  this->drawMenuItem(previousIndex);
  this->drawMenuItem(this->itemIndex);
}


void MenuScreen::onTick()
{
}


void MenuScreen::drawMenuItem(uint8_t index)
{
  uint16_t y = (index + 2) * Metrics::LargeTextLineHeight;
  bool isSelected = index == this->itemIndex;

  this->drawLargeTextLineCentered(
    this->getMenuItemTitle((MenuItem)index), y,
    isSelected ? Config::ColorMenuSelectedText : Config::ColorMenuText,
    isSelected ? Config::ColorMenuSelectedBackground : Config::ColorMenuBackground);
}


const char* MenuScreen::getMenuItemTitle(MenuItem item)
{
  switch (item)
  {
    case MenuItem::Manual:
      return "Manual";

    case MenuItem::Presets:
      return "Presets";

    case MenuItem::Calibrate:
      return "Calibrate";

    case MenuItem::Exit:
      return "Exit";

    default:
      return nullptr;
  }
}


void MenuScreen::activateMenuItem(MenuItem item)
{
  switch (item)
  {
    case MenuItem::Manual:
      this->screenManager->show<ManualScreen>();
      break;

    case MenuItem::Presets:
      this->screenManager->show<PresetsScreen>();
      break;

    case MenuItem::Calibrate:
      this->screenManager->show<CalibrateScreen>();
      break;

    case MenuItem::Exit:
      this->screenManager->show<HomeScreen>();
      break;

    default:
      break;
  }
}