/*
 * $Id: eventEditMenu.c,v 1.1.1.1 2005/02/17 05:45:52 sundin Exp $
 *
 * Tag: $Name:  $
 *
 * See the file COPYRIGHT for copyright information
 *
 * $Log: eventEditMenu.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.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 "eventEditMenu.h"
#include "calManager.h"
#include "calEvent.h"
#include "i18n.h"

#include <vdr/menuitems.h>

cEventEditMenu::cEventEditMenu(cCalEvent* ev, bool insert) :
 cOsdMenu (tr("Edit Event"), 24), _newEvent(insert), _ev(ev)
{

  if (_ev)
    {

      // deep copy
      
      _data = *_ev;

      if (_newEvent)
      {
	SetTitle(tr("New Event"));
      }

      // dirty
      _type = (int)_ev->_type;

      _calTypeTexts[0] = tr("Fix Day");
      _calTypeTexts[1] = tr("Weekly");
      _calTypeTexts[2] = tr("Yearly");

      Add(new cMenuEditStraItem(tr("Event type"), &_type, 3, _calTypeTexts));

      Add(new cMenuEditIntItem(tr("Day"), &_data._day, 1, 31));

      Add(new cMenuEditIntItem(tr("Month"), &_data._month, 1, 12));

      Add(new cMenuEditIntItem(tr("Year"), &_data._year, 1970, 2100));

      Add(new cMenuEditIntItem(tr("Day Of Week (Mo=1)"),
			       &_data._dayOfWeek, 1, 7));

      Add(new cMenuEditStrItem(tr("Description"),
			       _data._text, MAX_EVENT_NAME, tr(FileNameChars)));

      Display();

   }
}


cEventEditMenu::~cEventEditMenu()
{
  if (_ev && _newEvent)
  {
     delete _ev;     // NEW was pressed, but not confirmed
  }
}


void cEventEditMenu::updateEvent()
{
  if (_ev)
  {
    if ( memcmp(_ev, &_data, sizeof(_data)) != 0 || _type != _ev->_type)
    {
      *_ev = _data;
      _ev->_type = (eCalEventType)_type;

      cCalManager* mgr = cCalManager::getInstance();

      if (_newEvent)
        {
          mgr->add(_ev);
	}
      mgr->save();

      _newEvent = false;
    }
  }
}


eOSState cEventEditMenu::ProcessKey(eKeys key)
{

  eOSState state = cOsdMenu::ProcessKey(key);

  if (state == osUnknown)
  {
     switch (key)
     {
       case kOk:
            updateEvent();
            return osBack;

       case kRed:
       case kGreen:
       case kYellow:
       case kBlue:   return osContinue;

       default: break;
     }
  }


  return state;

}

