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

#include "controls.h"
#include "statusandquestion.h"
#include "messages.h"
#include "vdrtools.h"
#include <vdr/remote.h>

cStatusControl::cStatusControl(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cStatusControl::cStatusControl", plugin_name);
#endif
  lastStatusMessage = NULL;
  lastStatusMessageTime = 0;
}

cStatusControl::~cStatusControl(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cStatusControl::~cStatusControl", plugin_name);
#endif
  if (lastStatusMessage)
    free(lastStatusMessage);
}

void cStatusControl::OsdStatusMessage(const char *Message)
{
#ifdef SAQ_Debug
  dsyslog("%s: cStatusControl::OsdStatusMessage %d %s", plugin_name, controlstate, Message);
#endif
  if (Message && strlen(Message) > 2 && controlstate == ControlState_Normal && History.SetLock(true, true))
  {
    if (!lastStatusMessage || strcmp(lastStatusMessage, Message) || time(NULL) - lastStatusMessageTime > 60)
    {
      if (lastStatusMessage)
        free(lastStatusMessage);
      lastStatusMessage = strdup(Message);
      lastStatusMessageTime = time(NULL);
      cMessageData *messagedata = new cMessageData(MessageTyp_VDRStatus);
      messagedata->SetText(Message);
      History.Add(new cMessage(messagedata));
    }
    History.SetLock(false);
  }
}

void cStatusControl::OsdCurrentItem(const char *Text)
{
#ifdef SAQ_Debug
  dsyslog("%s: cStatusControl::OsdCurrentItem %d %s", plugin_name, controlstate, Text);
#endif
  if (controlstate == ControlState_MenuSelect)
    if (strstr(Text, "Select this line"))
      cRemote::Put(kOk, true);
    else
      cRemote::Put(kDown, true);
}

void cWaitControl::Show(void)
{
#ifdef SAQ_Debug
  dsyslog("%s: cWaitControl::Show %d", plugin_name, controlstate);
#endif
  if (controlstate == ControlState_MenuSelect)
  {
    controlstate = ControlState_WaitShow;
    cRemote::Put(kNone, true);
  }  
}

eOSState cWaitControl::ProcessKey(eKeys Key)
{
#ifdef SAQ_Debug
  dsyslog("%s: cWaitControl::ProcessKey %d Key=%s", plugin_name, controlstate, KeyName(Key));
#endif
  if (controlstate == ControlState_Normal)
    return osEnd;
  controlstate = ControlState_Wait1;
  for (int to = 0; to < 15 && (controlstate == ControlState_Wait1 || controlstate == ControlState_Wait2); to++)
    sleep(1);
  if (controlstate != ControlState_Wait2)
    return osEnd;
  controlstate = ControlState_Loop;
  cRemote::Put(kNone, true);  
  cRemote::Put(kNone, true);
  return osContinue;
}
