//-----------------------------------------------------------------------------
/*
 * Description: This file contains the class AxMenuConfigCommon
 *
 * See the file README in the main directory for a description of
 * this software, copyright information, and how to reach the author.
 *
 * Author: alex
 * Date:   17.11.2003
 *
 * Last modfied:
 *   $Author: alex $
 *   $Date: 2004/05/31 12:52:20 $
 *   $Revision: 1.11 $
 */
//-----------------------------------------------------------------------------

//=============================================================================
//     includes
//=============================================================================

//----- multiple defs of swap() workaround ------------------------------------
#include "AxPluginGlobals.h"

//----- qt --------------------------------------------------------------------
//----- CORBA -----------------------------------------------------------------
//----- ICE -------------------------------------------------------------------
//----- C++ -------------------------------------------------------------------
//----- C ---------------------------------------------------------------------
//----- vdr -------------------------------------------------------------------
#include <vdr/tools.h>
#include <vdr/i18n.h>
#include <vdr/menuitems.h>

//----- AxLib -----------------------------------------------------------------
#include <Ax/Tools/Trace.h>
// #include <Ax/Tools/TraceOn.h>

//----- local plugin ----------------------------------------------------------
#include "AxMenuConfigCommon.h"
#if ALEX_NOPORT
#include "AxOsdMailItem.h"
#endif
#include "AxVdrTools.h"

//----- locals ----------------------------------------------------------------

//=============================================================================
//     using namespaces
//=============================================================================
using namespace std;


//-----------------------------------------------------------------------------
//     AxMenuConfigCommon::AxMenuConfigCommon()
//-----------------------------------------------------------------------------
AxMenuConfigCommon::AxMenuConfigCommon(AxPluginSettings *theSettings)
                  : PARENT(tr("General settings"), 25)
                  , myOldSettings( theSettings)
                  , myNewSettings(*theSettings)
{
  wsdebug(("> AxMenuConfigCommon::AxMenuConfigCommon()"));
  cOsdItem *anItem = 0;
  char      szLine[256];

  //----- create edit elements -------------------------------------------
  sprintf(szLine, "----- %s ---------------------------------------------------", tr("General settings"));
  anItem = new cOsdItem(szLine);
  anItem->SetSelectable(false);
  Add(anItem);

#if !VDR_HAS_ASYNC_MSG
  if (myNewSettings.MailCmd[0] != '\0')
  {
#endif
    Add(new cMenuEditIntItem(tr("Check every (minutes)"), &myNewSettings.BGCheckDelay,  0, 60));
#if !VDR_HAS_ASYNC_MSG
  }
#endif

  if (SysLogLevel > 2)
  {
    Add(new cMenuEditBoolItem(tr("Log c-client-callbacks"), &myNewSettings.DebugCClient));
  } // if

#if ALEX_NOPORT
  sprintf(szLine, "----- %s ---------------------------------------------------", tr("MailBox-View"));
  anItem = new cOsdItem(szLine);
  anItem->SetSelectable(false);
  Add(anItem);

  Add(new cMenuEditStraItem(tr("Font"        ), &myNewSettings.MBV_Font, AxVdrTools::FontCount, AxVdrTools::FontNames));
#endif

  sprintf(szLine, "----- %s ---------------------------------------------------", tr("Mail-List-View"));
  anItem = new cOsdItem(szLine);
  anItem->SetSelectable(false);
  Add(anItem);

  Add(new cMenuEditStraItem(tr("Sort order"             ), &myNewSettings.MLV_SortOrder, AxVdrTools::SO_COUNT, AxVdrTools::SortOrderStrings));
  Add(new cMenuEditIntItem (tr("Maximum number of mails"), &myNewSettings.MLV_MaxMails , 0));
#if ALEX_NOPORT
  Add(new cMenuEditStraItem(tr("Font"           ), &myNewSettings.MLV_Font    , AxVdrTools::FontCount, AxVdrTools::FontNames));
#endif

#if ALEX_NOPORT
  sprintf(szLine, "----- %s ---------------------------------------------------", tr("Mail-View"));
  anItem = new cOsdItem(szLine);
  anItem->SetSelectable(false);
  Add(anItem);

  Add(new cMenuEditStraItem(tr("Header font"   ), &myNewSettings.MV_HeadFont, AxVdrTools::FontCount   , AxVdrTools::FontNames));
  Add(new cMenuEditStraItem(tr("Body font"     ), &myNewSettings.MV_BodyFont, AxVdrTools::FontCount   , AxVdrTools::FontNames));
  Add(new cMenuEditIntItem (tr("Scroll lines"  ), &myNewSettings.MV_JumpSize, 1                       , 5));
  Add(new cMenuEditIntItem (tr("Word-wrap mode"), &myNewSettings.MV_ViewMode, AxOsdMailItem::MVM_FIRST, AxOsdMailItem::MVM_LAST));
#endif

  setButtons();
  SetCurrent(Get(1));
  wsdebug(("< AxMenuConfigCommon::AxMenuConfigCommon()"));
} // AxMenuConfigCommon::AxMenuConfigCommon()

//-----------------------------------------------------------------------------
//     AxMenuConfigCommon::~AxMenuConfigCommon()
//-----------------------------------------------------------------------------
AxMenuConfigCommon::~AxMenuConfigCommon()
{
  wsdebug(("> AxMenuConfigCommon::~AxMenuConfigCommon()"));
  wsdebug(("< AxMenuConfigCommon::~AxMenuConfigCommon()"));
} // AxMenuConfigCommon::~AxMenuConfigCommon()

#define DEBUG_PROCESS_KEY 0

//-------------------------------------------------------------------------
//     AxMenuConfigCommon::ProcessKey()
//-------------------------------------------------------------------------
eOSState AxMenuConfigCommon::ProcessKey(eKeys theKey)
{
#if DEBUG_PROCESS_KEY
  if (theKey != kNone)
  {
    wsdebug(("> AxMenuConfigCommon::ProcessKey() Key: %-10s  Cur: %3d  HasSub: %d  parent processing..."
           , cKey::ToString(theKey)
           , Current()
           , HasSubMenu()
           ));
  } // if
#endif

  bool fHasSubMenu = HasSubMenu();
  eOSState aState = PARENT::ProcessKey(theKey);

#if DEBUG_PROCESS_KEY
  if (theKey != kNone)
  {
    wsdebug(("  AxMenuConfigCommon::ProcessKey() Key: %-10s  Cur: %3d  HasSub: %d  State: %-10s  "
           , cKey::ToString(theKey)
           , Current()
           , HasSubMenu()
           , AxVdrTools::getOSStateStr(aState)
           ));
  } // if
#endif

  if (aState != osUnknown && fHasSubMenu)
  {
#if DEBUG_PROCESS_KEY
    if (theKey != kNone)
    {
      wsdebug(("< AxMenuConfigCommon::ProcessKey() Key: %-10s  Cur: %3d  HasSub: %d  State: %-10s  already handled"
            , cKey::ToString(theKey)
            , Current()
            , HasSubMenu()
            , AxVdrTools::getOSStateStr(aState)
            ));
    } // if
#endif
    return aState;
  } // if

  //----- select action... ----------------------------------------------------
  switch (theKey)
  {
    case kOk    :
    {
      if (aState == osUnknown)
      {
        Store();
        aState = osBack;
      } // if
      break;
    }
    case kUp    :
    case kDown  :
    case kLeft  :
    case kRight :
    {
      setButtons();
      break;
    }

    //----- Edit -----
    case kRed   :
    {
      break;
    }

    //----- New ------
    case kGreen :
    {
      break;
    }

    //----- Delete -----
    case kYellow:
    {
      break;
    }

    //----- ??? ------
    case kBlue:
    {
      break;
    }

    //----- default -----
    default     :
    {
//      aState = osUnknown;
      break;
    }
  } // switch

#if DEBUG_PROCESS_KEY
  if (theKey != kNone)
  {
    wsdebug(("< AxMenuConfigCommon::ProcessKey() Key: %-10s  Cur: %3d  HasSub: %d  State: %-10s  "
          , cKey::ToString(theKey)
          , Current()
          , HasSubMenu()
          , AxVdrTools::getOSStateStr(aState)
          ));
  } // if
#endif
  return aState;
} // AxMenuConfigCommon::ProcessKey()

//-------------------------------------------------------------------------
//     AxMenuConfigCommon::setButtons()
//-------------------------------------------------------------------------
void AxMenuConfigCommon::setButtons(int theButtonPageNum)
{
#if 0
  wsdebug(("> AxMenuConfigCommon::setButtons() ButtonPage: %d", theButtonPageNum));
  wsdebug(("< AxMenuConfigCommon::setButtons() '%10s' '%10s' '%10s' '%10s'"
         , aButton1Text, aButton2Text, aButton3Text, aButton4Text
         ));
#endif
} // AxMenuConfigCommon::setButtons()

//-------------------------------------------------------------------------
//     AxMenuConfigCommon::Store()
//-------------------------------------------------------------------------
void AxMenuConfigCommon::Store(void)
{
  wsdebug(("> AxMenuConfigCommon::Store()"));
  *myOldSettings = myNewSettings;
  wsdebug(("< AxMenuConfigCommon::Store()"));
} // AxMenuConfigCommon::Store()
