/*****************************************************************************
 * @fn        printHeader
 *
 * @brief     Prints the header of the menu system on the top page/line on the
 *            LCD. This includes title and navigation item numbers in right 
 *            corner. Also prints an underline.
 *
 * input parameters
 *
 * @param     pMenu   - The menu struct which is going to be displayed
 */
static void printHeader(const menu_t *pMenu)
{
  /* displaying navigation data in the top
   * each of them returns the amount of pixels they occupie
   */
   
  uint8 occupied = printNavNumbers(pMenu);   /* numbers in top right corner */

  /* determining prefered title depending on where in the menu the user is */
  char *prefTitle;
  if(pMenu->pTextHeader)
  {
    prefTitle = pMenu->pTextHeader;
  }
  else if(pMenu->pParentMenu)
  {
    menu_t *pTopMenu = menuTop(pMenu);
    int8 nCurrentItem = pTopMenu->nCurrentItem;
    prefTitle=pTopMenu->pItems[nCurrentItem].pTextDescription;
  }
  else
  {
    prefTitle = "Main Menu";
  }

  /* calculating maximal width for menu title and the threshold to when the
   * title is too big to be centered and must be skeewed. Both are specified
   * in number of characters, not pixels.
   */
  uint8 maxWidth = (LCD_COLS-occupied)/LCD_CHAR_WIDTH-1;
  uint8 skewThreshold = (LCD_COLS-2*occupied)/LCD_CHAR_WIDTH-2;
  
  /* truncates title as necessary */
  char title[22];
  uint8 prefTitleLen = lcdGetStringLength(prefTitle);
  uint8 width = ( prefTitleLen<maxWidth ? prefTitleLen : maxWidth );
  for(uint8 i=0;i<width;i++)
  {
    title[i]=prefTitle[i];
  }
  title[width] = '\0';

  /* writes title */
  if(width<=skewThreshold)
  { 
    /* title's not too big to be centered */
    lcdBufferPrintStringAligned(0, title,eLcdAlignCenter,eLcdPage0);
    //lcdBufferPrintStringCentered(0,title,0);
  }
  else
  {
    /* title's too big to be centered. Skew title */
    int8 pos = LCD_COLS-occupied-LCD_CHAR_WIDTH-width*LCD_CHAR_WIDTH;
    lcdBufferPrintString(0,title,pos,eLcdPage0);
  }

  /* header underline */
  lcdBufferSetHLine(0,0,LCD_COLS-1,7);

}
void MainMenu::draw() const
{
    Gui::clearTextBufs();
    C2D_SceneBegin(g_renderTargetTop);
    menuTop();
    C2D_SceneBegin(g_renderTargetBottom);
    Gui::backgroundBottom(false);
    for (MainMenuButton* button : buttons)
    {
        button->draw();
    }
    static const std::string version = StringUtils::format("v%d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO, GIT_REV);
    Gui::staticText(version, 316, 223, FONT_SIZE_11, FONT_SIZE_11, COLOR_LIGHTBLUE, TextPosX::RIGHT, TextPosY::TOP);
}