/*
 * statusandquestion: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: menusetup.c 0.1 2004/07/11 14:12:20 hflor Exp $
 */

#include "statusandquestion.h"
#include "menumessages.h"
#include "menusetup.h"
#include "vdrtools.h"
#include "i18n.h"

// --- cMenuSetupStatusAndQuestion -------------------------------------------------------

cMenuSetupStatusAndQuestion::cMenuSetupStatusAndQuestion(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::cMenuSetupStatusAndQuestion", plugin_name);
#endif
  store = false;

  #define StoreValue(T) T.o = T.u
  #define StoreValueChar(T) strcpy(T.o, T.u)

  // var pos7
  StoreValue     (commandline_preference);
  StoreValue     (delete_b_e);
  StoreValueChar (mainmenu_name);
  StoreValue     (mainmenu_visible);
  StoreValue     (message_in_queue);
  StoreValue     (num_entrys);
  StoreValue     (response_in_queue);
  StoreValue     (sortdirection);
  StoreValue     (verbose);

  #undef StoreValue
  #undef StoreValueChar

  Set();
}

cMenuSetupStatusAndQuestion::~cMenuSetupStatusAndQuestion(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::~cMenuSetupStatusAndQuestion store=%s", plugin_name, store ? "true" : "false");
#endif
  if (!store) {

    #define RestoreValue(T) T.u = T.o
    #define RestoreValueChar(T) strcpy(T.u, T.o)

    // var pos8
    RestoreValue     (commandline_preference);
    RestoreValue     (delete_b_e);
    RestoreValueChar (mainmenu_name);
    RestoreValue     (mainmenu_visible);
    RestoreValue     (message_in_queue);
    RestoreValue     (num_entrys);
    RestoreValue     (response_in_queue);
    RestoreValue     (sortdirection);
    RestoreValue     (verbose);

    #undef RestoreValue
    #undef RestoreValueChar
  }
}

void cMenuSetupStatusAndQuestion::Store(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::Store", plugin_name);
#endif

  #define SaveValue(T, N) if (T.u != T.o) SetupStore(N, T.u);
  #define SaveValueChar(T, N) if (strcmp(T.u, T.o)) SetupStore(N, T.u);

  // var pos9
  SaveValue     (commandline_preference, "commandline");
  SaveValue     (delete_b_e,             "deletelines");
  SaveValueChar (mainmenu_name,          "name");
  SaveValue     (mainmenu_visible,       "visible");
  SaveValue     (message_in_queue,       "message");
  SaveValue     (num_entrys,             "entrys");
  SaveValue     (response_in_queue,      "response");
  SaveValue     (sortdirection,          "sortdirection");
  SaveValue     (verbose,                "verbose");

  #undef SaveValue
  #undef SaveValueChar

  store = true;
}

void cMenuSetupStatusAndQuestion::Set(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::Set Current=%d", plugin_name, Current());
#endif
  int current = Current();
  Clear();

  #define DisplayLine(T) !T.h && (!commandline_preference.u || !T.c)

  Add(new cOsdItem         (tr("Setup$Display messages now"),                            osUser5));
  if (DisplayLine(commandline_preference))
    Add(new cMenuEditBoolItem(tr("Setup$Preferr Command Line Parameter"),                &commandline_preference.u));
  if (DisplayLine(mainmenu_visible))
    Add(new cMenuEditBoolItem(tr("Setup$Visible in Mainmenu"),                           &mainmenu_visible.u));
  if (DisplayLine(mainmenu_name) && mainmenu_visible.u)
    Add(new cMenuEditStrItem (tr("Setup$  Name for Mainmenu"),                           mainmenu_name.u, sizeof(mainmenu_name.u), tr(FileNameChars)));
  if (DisplayLine(sortdirection))
    Add(new cMenuEditBoolItem(tr("Setup$Sortdirection messagelist"),                     &sortdirection.u, tr("Choise$descending"), tr("Choise$ascending")));
  if (DisplayLine(delete_b_e))
    Add(new cMenuEditStraItem(tr("Setup$Deleteline position"),                           &delete_b_e.u, 4, DeleteLine));
  if (DisplayLine(num_entrys) && delete_b_e.u == 0x3)
    Add(new cMenuEditIntItem (tr("Setup$  Display bottom update ex lines"),              &num_entrys.u, 1, 200));
  if (DisplayLine(response_in_queue))
    Add(new cMenuEditIntItem(tr("Setup$Holdtime for response in queue (min)"),           &response_in_queue.u, 2, 199));
  if (DisplayLine(message_in_queue))
    Add(new cMenuEditIntItem(tr("Setup$Holdtime for message in history (min)"),          &message_in_queue.u, 5, 999));
  if (DisplayLine(verbose))
    Add(new cMenuEditBoolItem(tr("Setup$Verbose Log Mode"),                              &verbose.u));

  #undef DisplayLine

#ifdef SAQ_Debug
  dsyslog("%s: Count=%d", plugin_name, Count());
#endif
  SetCurrent(Get(current));
}

eOSState cMenuSetupStatusAndQuestion::ProcessKey(eKeys Key)
{
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::ProcessKey Key=%s", plugin_name, KeyName(Key));
#endif
  int m_commandline_preference = commandline_preference.u;
  int m_mainmenu_visible = mainmenu_visible.u;
  int m_delete_b_e = delete_b_e.u;

  eOSState state = cMenuSetupPage::ProcessKey(Key);
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::ProcessKey OSState=%s", plugin_name, OSStateName(state));
#endif

  if (m_commandline_preference != commandline_preference.u || m_mainmenu_visible != mainmenu_visible.u || m_delete_b_e != delete_b_e.u) {
    Set();
    Display();
  }
  switch (state) {
    case osUser5:   state = AddSubMenu(new cMenuMessages);
                    break;
    default:        break;
  }
#ifdef SAQ_Debug
  dsyslog("%s: cMenuSetupStatusAndQuestion::ProcessKey returned OSState=%s", plugin_name, OSStateName(state));
#endif
  return state;
}
