/*
 * menu_event.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/status.h>
#include "menu_event.h"
#include "menu_commands.h"
#include "epgsearchcfg.h"
#include "tools.h"

// --- cMenuEventSearch ------------------------------------------------------------

cMenuEventSearch::cMenuEventSearch(const cEvent *Event, bool CanSwitch, const cEvent* evPrev, const cEvent* evNext)
:cOsdMenu(tr("Event"))
{
  event = Event;
  if (event) 
  {
      cChannel *channel = Channels.GetByChannelID(event->ChannelID(), true);
      if (channel) 
      {
	  SetTitle(channel->Name());
	  szGreen = szYellow = NULL;
	  if (evPrev) szGreen = strdup(GETTIMESTRING(evPrev));
	  if (evNext) szYellow = strdup(GETTIMESTRING(evNext));
	  SetHelp(tr("Button$Record"), szGreen, szYellow, CanSwitch ? tr("Button$Switch") : NULL);
      }
  }
}

cMenuEventSearch::cMenuEventSearch(const cEvent *Event, bool CanSwitch, const cChannel* chPrev, const cChannel* chNext)
:cOsdMenu(tr("Event"))
{
  event = Event;
  if (event) 
  {
      cChannel *channel = Channels.GetByChannelID(event->ChannelID(), true);
      if (channel) 
      {
	  SetTitle(channel->Name());	  
	  szGreen = szYellow = NULL;
	  if (chPrev) szGreen = strdup(CHANNELNAME(chPrev));
	  if (chNext) szYellow = strdup(CHANNELNAME(chNext));
	  SetHelp(tr("Button$Record"), szGreen, szYellow, CanSwitch ? tr("Button$Switch") : NULL);
      }
  }
}

cMenuEventSearch::cMenuEventSearch(const cEvent *Event, bool CanSwitch)
:cOsdMenu(tr("Event"))
{
  event = Event;
  if (event) 
  {
      cChannel *channel = Channels.GetByChannelID(event->ChannelID(), true);
      if (channel) 
      {
	  SetTitle(channel->Name());
	  szGreen = szYellow = NULL;
	  SetHelp(tr("Button$Record"), "<<", ">>", CanSwitch ? tr("Button$Switch") : NULL);
      }
  }
}

cMenuEventSearch::~cMenuEventSearch()
{
    if (szGreen) free(szGreen);
    if (szYellow) free(szYellow);
}

void cMenuEventSearch::Display(void)
{
  cOsdMenu::Display();
  if (event)
  {
      DisplayMenu()->SetEvent(event);
      cStatus::MsgOsdTextItem(event->Description());
  }
}

eOSState cMenuEventSearch::Commands(eKeys Key)
{
    if (HasSubMenu())
	return osContinue;
    if (event) {
        cMenuSearchCommands *menu;
        eOSState state = AddSubMenu(menu = new cMenuSearchCommands(tr("EPG Commands"), event, true));
        if (Key != kNone)
	    state = menu->ProcessKey(Key);
        return state;
    }
    return osContinue;
}

eOSState cMenuEventSearch::ProcessKey(eKeys Key)
{
    if (!HasSubMenu())
    {
	switch (Key) {
	    case kUp|k_Repeat:
	    case kUp:
	    case kDown|k_Repeat:
	    case kDown:
	    case kLeft|k_Repeat:
	    case kLeft:
	    case kRight|k_Repeat:
	    case kRight:
		DisplayMenu()->Scroll(NORMALKEY(Key) == kUp || NORMALKEY(Key) == kLeft, NORMALKEY(Key) == kLeft || NORMALKEY(Key) == kRight);
		cStatus::MsgOsdTextItem(NULL, NORMALKEY(Key) == kUp);
		return osContinue;
	    case k1...k9: 
		if (!HasSubMenu())
		    return Commands(Key);
		else
		    return osContinue;
		break;
		
	    default: break;
	}
    }

  eOSState state = cOsdMenu::ProcessKey(Key);
  
  if (state == osUnknown) {
      switch (Key) {
	  case kOk:     return osBack;
	  default: break;
      }
  }
  return state;
}

