//-----------------------------------------------------------------------------
/*
 * Description: This file contains the class AxMenuConfigMailBox
 *
 * 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/04/04 15:53:38 $
 *   $Revision: 1.5 $
 */
//-----------------------------------------------------------------------------

//=============================================================================
//     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 "AxMenuConfigMailBox.h"
#include "AxVdrTools.h"
#include "AxPluginSetup.h"

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

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

const char *AxMenuConfigMailBox::MailBoxNameChars = " abcdefghijklmnopqrstuvwxyz0123456789-.#@~{}[]()_/";
const char *AxMenuConfigMailBox::UserNameChars    = "abcdefghijklmnopqrstuvwxyz0123456789_";
const char *AxMenuConfigMailBox::PaswordChars     = "abcdefghijklmnopqrstuvwxyz0123456789_";
const char *AxMenuConfigMailBox::MailBoxChars     = " abcdefghijklmnopqrstuvwxyz0123456789-.#@~{}[]_/";
const char *AxMenuConfigMailBox::AccessCodeChars  = "0123456789";


//-----------------------------------------------------------------------------
//     AxMenuConfigMailBox::AxMenuConfigMailBox()
//-----------------------------------------------------------------------------
AxMenuConfigMailBox::AxMenuConfigMailBox(AxPluginSettings *thePluginSetup, Ax::Mail::MailBoxSetup *theSetup)
                  : PARENT(tr("Configure account settings"), 20)
                  , myPluginSetup(thePluginSetup)
                  , myOldSetup   (theSetup )
                  , myNewSetup   (*theSetup)
{
  wsdebug(("> AxMenuConfigMailBox::AxMenuConfigMailBox()"));

  //----- create edit elements -------------------------------------------
  Add(new cMenuEditStrItem (tr("Account name"       ),  myNewSetup.AccountName    , sizeof(myNewSetup.AccountName) - 1, MailBoxNameChars));
  Add(new cMenuEditStrItem (tr("Login name"         ),  myNewSetup.LoginName      , sizeof(myNewSetup.LoginName  ) - 1, UserNameChars   ));
  Add(new cMenuEditStrItem (tr("Login password"     ),  myNewSetup.LoginPass      , sizeof(myNewSetup.LoginPass  ) - 1, PaswordChars    ));
  Add(new cMenuEditStrItem (tr("MailBox"            ),  myNewSetup.MailBox        , sizeof(myNewSetup.MailBox    ) - 1, MailBoxChars    ));
  Add(new cMenuEditStraItem(tr("VDR restrict access"), &myNewSetup.AccessMode     , AxVdrTools::AccessModeCount, AxVdrTools::AccessModeNames));
  Add(new cMenuEditStrItem (tr("VDR access code"    ),  myNewSetup.AccessCode     , sizeof(myNewSetup.AccessCode ) - 1, AccessCodeChars ));
  Add(new cMenuEditBoolItem(tr("Auto-mark-seen"     ), &myNewSetup.fAutoMarkSeen  ));
  Add(new cMenuEditBoolItem(tr("Cleanup-on-close"   ), &myNewSetup.fExpungeOnClose));
  if (thePluginSetup->BGCheckDelay > 0)
  {
    Add(new cMenuEditStraItem(tr("Background check"), &myNewSetup.BGCheckMode  , AxVdrTools::BGCheckModeCount, AxVdrTools::BGCheckModeNames));
  } // if

  setButtons();
  wsdebug(("< AxMenuConfigMailBox::AxMenuConfigMailBox()"));
} // AxMenuConfigMailBox::AxMenuConfigMailBox()


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

#define DEBUG_PROCESS_KEY 0

//-------------------------------------------------------------------------
//     AxMenuConfigMailBox::ProcessKey()
//-------------------------------------------------------------------------
eOSState AxMenuConfigMailBox::ProcessKey(eKeys theKey)
{
#if DEBUG_PROCESS_KEY
  if (theKey != kNone)
  {
    wsdebug(("> AxMenuConfigMailBox::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(("  AxMenuConfigMailBox::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(("< AxMenuConfigMailBox::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(("< AxMenuConfigMailBox::ProcessKey() Key: %-10s  Cur: %3d  HasSub: %d  State: %-10s  "
          , cKey::ToString(theKey)
          , Current()
          , HasSubMenu()
          , AxVdrTools::getOSStateStr(aState)
          ));
  } // if
#endif
  return aState;
} // AxMenuConfigMailBox::ProcessKey()

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

//-------------------------------------------------------------------------
//     AxMenuConfigMailBox::Store()
//-------------------------------------------------------------------------
void AxMenuConfigMailBox::Store(void)
{
  wsdebug(("> AxMenuConfigMailBox::Store()"));
  *myOldSetup = myNewSetup;
  wsdebug(("< AxMenuConfigMailBox::Store()"));
} // AxMenuConfigMailBox::Store()
