/*
 * $Id: eventListMenu.c,v 1.1.1.1 2005/02/17 05:45:52 sundin Exp $
 *
 * Tag: $Name:  $
 *
 * See the file COPYRIGHT for copyright information
 *
 * $Log: eventListMenu.c,v $
 * Revision 1.1.1.1  2005/02/17 05:45:52  sundin
 * initial import ver0.0.5
 *
 * Revision 1.1.1.1  2005/01/17 18:06:31  ksltuf
 * PIM-Plugin 0.0.4
 *
 * Revision 1.4  2004/11/24 21:34:35  ksltuf
 * Added new translation for "Delete Event?"
 *
 * Revision 1.3  2004/11/24 21:29:51  ksltuf
 * Changed order of include-Statements - VDR-includes come last
 * (problems with STL under gcc 2.95)
 *
 * Revision 1.2  2004/07/05 19:00:26  ksltuf
 * Added COPYRIGHT and cvs information
 *
 *
 */


#include "eventListMenu.h"
#include "eventEditMenu.h"
#include "calEvent.h"
#include "calEventMenuItem.h"
#include "calManager.h"
#include "i18n.h"

#include <vdr/interface.h>
#include <vdr/menuitems.h>
#include <vdr/osdbase.h>


cEventListMenu::cEventListMenu(int day, int month, int year, int dayOfWeek)
  : cOsdMenu(tr("Events"), 13, 12)
{
  char* dbuf;

  asprintf (&dbuf, "%s %d-%02d-%02d", tr("Events for"), year, month, day);
  SetTitle(dbuf);
  free(dbuf);

  cCalManager* mgr = cCalManager::getInstance();

  tEventIds ids = mgr->getEventsForDay(day, month, year, dayOfWeek);

  tEventIds::const_iterator it = ids.begin();

  char buf[256];
  while (it != ids.end())
    {
      cCalEvent *ev = mgr->getEventById(*it);
      strcpy(buf, ev->getLongText());
      cOsdMenu::Add (new cCalEventMenuItem(buf, *it));
      ++it;
    }

  SetHelp(tr("Edit"),tr("New"),tr("Delete"), NULL);
  Display();
}


cEventListMenu::cEventListMenu() : cOsdMenu(tr("Events"), 13, 12)
{
  cCalManager* mgr = cCalManager::getInstance();

  tEventIds ids = mgr->getAllEvents();

  tEventIds::const_iterator it = ids.begin();

  char buf[256];
  while (it != ids.end())
    {
      cCalEvent *ev = mgr->getEventById(*it);
      strcpy(buf, ev->getLongText());
      cOsdMenu::Add (new cCalEventMenuItem(buf, *it));
      ++it;
    }

  SetHelp(tr("Edit"),tr("New"),tr("Delete"), NULL);
  Display();
}


cEventListMenu::~cEventListMenu()
{
}


cCalEvent* cEventListMenu::getCurrentEvent()
{
  cCalEventMenuItem* item = (cCalEventMenuItem*)Get(Current());

  return item ? cCalManager::getInstance()->getEventById(item->getId()) : NULL;
}


eOSState cEventListMenu::Edit(void)
{
  cCalEvent* ev = getCurrentEvent();

  if (HasSubMenu() || !ev)
    return osContinue;

  return AddSubMenu(new cEventEditMenu(ev));
}


eOSState cEventListMenu::New(void)
{
  if (HasSubMenu())
    return osContinue;

  return AddSubMenu(new cEventEditMenu(new cCalEvent, true));
}


eOSState cEventListMenu::Delete(void)
{
  if (HasSubMenu())
    return osContinue;

  cCalEvent* ev = getCurrentEvent();
  if (ev)
  {
    if (Interface->Confirm(tr("Delete Event?")))
    {
       cCalManager::getInstance()->del(ev);
       cCalManager::getInstance()->save();
       cOsdMenu::Del(Current());
       Display();
    }
  }

  return osContinue;
}


eOSState cEventListMenu::ProcessKey(eKeys key)
{
  eOSState state = cOsdMenu::ProcessKey(key);

  if (state == osUnknown)
    switch (key)
    {
       case kOk:     state = Edit();
       case kRed:    state = Edit();
		     break;

       case kGreen:  state = New();
		     break;

       case kYellow: state = Delete();
		     break;

       default:      break;
    }

  // check if a new event has to be displayed
  
  if (!HasSubMenu())
    {
      cCalManager* mgr = cCalManager::getInstance();
      int lastId = mgr->getLastNewId();

      if (lastId >= 0)
        {
          char buf[2*MAX_EVENT_NAME];

          mgr->resetLastNewId();
          cCalEvent *ev = mgr->getEventById(lastId);
          strcpy(buf, ev->getLongText());

          cOsdMenu::Add (new cCalEventMenuItem(buf, lastId));
          Display();
        }
    }


  return state;
}

