//----------------------------------------------------------------------------
///
/// \brief  menu
///
///         menu entries for horo
//
/// \date   20190422
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


//---Own------------------------------

#include <biwak/menu.h>
#include <biwak/eadisplay.hpp>
#include <string.h>
//#include <arena/platform.h>


//---Defines------------------------------------------------------------------


//---Declaration--------------------------------------------------------------

/*
const char *menuEntries[]=
{
   "Dry & run",
   "Run",
   "Desolder",
   "Monitor",
   "Statistic",
   "Blink",
   "Sleep",
};

const int menuEntriesCount=sizeof(menuEntries)/sizeof(menuEntries[0]);
*/

CMenu::CMenu( CEaDisplay &display, const char *menuEntries[], int menuEntriesCount )
   :m_pos(0)
   ,m_scrollPos(0)
   ,m_display(display)
{
   m_active=true;
   m_menuEntries=menuEntries;
   m_menuEntriesCount=menuEntriesCount;
   updateDisplay();
}


void CMenu::updateDisplay()
{
   m_display.clear();
   for(int i1=0; i1<3; i1++)
   {
      m_display.gotoxy(
            ( 16 - strlen(menuEntries[m_scrollPos+i1] ) ) / 2, i1 );
      m_display.puts( menuEntries[m_scrollPos+i1] );
   }
   m_display.gotoxy(0, m_pos-m_scrollPos);
   m_display.putch( ARROW_RIGHT );
   m_display.gotoxy(15, m_pos-m_scrollPos);
   m_display.putch( ARROW_LEFT );

   m_display.gotoxy(( 16 + strlen(menuEntries[ m_pos ] ) ) / 2, m_pos-m_scrollPos);
   return;
}


void CMenu::down()
{
   if(m_pos+1<menuEntriesCount)
   {
      m_pos+=1;
      if(m_pos-m_scrollPos>=3)
         m_scrollPos++;
      updateDisplay();
   }

   return;
}


void CMenu::up()
{
   if(m_pos)
   {
      m_pos-=1;
      if(m_pos-m_scrollPos<0)
         m_scrollPos--;
      updateDisplay();
   }

   return;
}

int CMenu::select()
{
   m_active=false;
   return(m_pos);
}

void CMenu::activate()
{
   m_active=true;
   updateDisplay();

   return;
}


EMenu CMenu::getApp()
{
   if(m_active)
      return( static_cast<EMenu>(-1) );

   return( static_cast<EMenu>(m_pos) );
}



//---fin----------------------------------------------------------------------
