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

//***************************************************************************
// Includes
//***************************************************************************

#include <vdr/interface.h>

#include "menu.h"
#include "pin.h"

//***************************************************************************
// Broadcast Menu
//***************************************************************************
//***************************************************************************
// Object
//***************************************************************************

cBroadcastMenu::cBroadcastMenu(const char* title, cLockedBroadcasts* theBroadcasts)
   : cOsdMenu(title)
{
   cLockedBroadcast* broadcast;

   lockedBroadcasts = theBroadcasts;

   for (broadcast = lockedBroadcasts->First(); broadcast; broadcast = lockedBroadcasts->Next(broadcast))
      cOsdMenu::Add(new cBroadcastMenuItem(broadcast));

   SetHelp(tr("Edit"), tr("Add"), tr("Delete"), "");
	Display();
}

cBroadcastMenu::~cBroadcastMenu()
{
}

//***************************************************************************
// Process Key
//***************************************************************************

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

   if (state == osUnknown)
   {
      switch (key)
      { 
         case kOk:
         case k1:
         case kRed:
         {
            state = AddSubMenu(new cBroadcastEditMenu(((cBroadcastMenuItem*)Get(Current()))->GetBroadcast()));
            break;
         }

         case k2:
         case kGreen:  
         {
            cLockedBroadcast* broadcast;

            lockedBroadcasts->Add(broadcast = new cLockedBroadcast("- new -"));
            cOsdMenu::Add(new cBroadcastMenuItem(broadcast));

            Display();
            break;
         }

         case k3:
         case kYellow: 
         {
            if (Interface->Confirm(tr("Remove broadcast?")))
            {
               // remove from list
                
               lockedBroadcasts->Del(((cBroadcastMenuItem*)Get(Current()))->GetBroadcast());
               cOsdMenu::Del(Current());
            }

            Display();             
            break;
         }

         default:  break;
      }
   }

   return state;
}

//***************************************************************************
// Broadcast Menu Item
//***************************************************************************
//***************************************************************************
// Object
//***************************************************************************

cBroadcastMenuItem::cBroadcastMenuItem(cLockedBroadcast* aBroadcast) 
   : cOsdItem()
{
   broadcast = aBroadcast;
   SetText(broadcast->GetName(), /*copy*/ true);
}

cBroadcastMenuItem::~cBroadcastMenuItem()
{
}

//***************************************************************************
// Broadcast Edit Menu
//***************************************************************************
//***************************************************************************
// Object
//***************************************************************************

cBroadcastEditMenu::cBroadcastEditMenu(cLockedBroadcast* aBroadcast)
{
   static const char* trSearchModes[cLockedBroadcast::smCount] = { 0 };
   const char* characters = " abcdefghijklmnopqrstuvwABCDEFGHIJKLMNOPQRSTUVW+-/()[]{}ßöäüÖÄÜ.,;#<>@0123456789";

   broadcast = aBroadcast;
   strncpy(pattern, broadcast->GetPattern(), sizePatternMax);
   pattern[sizePatternMax] = 0;
   active = broadcast->GetActive();
   searchMode = broadcast->SearchMode();
   strncpy(title, broadcast->GetName(), sizeTitleMax);
   title[sizeTitleMax] = 0;
   
   SetTitle(title);
   
   // translate search modes

   if (!trSearchModes[0])
      for (int i = 0; i < cLockedBroadcast::smCount; i++)
         trSearchModes[i] = tr(cLockedBroadcast::searchModes[i]);

   // add menu items

   Add(new cMenuEditStrItem(tr("Title"), title, sizeTitleMax, characters));
   Add(new cMenuEditStrItem(tr("Keyword"), pattern, sizePatternMax, characters));
   Add(new cMenuEditBoolItem(tr("Lock active"), &active));
	Add(new cMenuEditStraItem(tr("Search mode"), &searchMode, cLockedBroadcast::smCount, trSearchModes));
}

cBroadcastEditMenu::~cBroadcastEditMenu()
{
   // nothing yet
}

//***************************************************************************
// Store
//***************************************************************************

void cBroadcastEditMenu::Store(void)
{
   broadcast->SetPattern(pattern);
   broadcast->SetActive(active);
   broadcast->SetSearchMode(searchMode);
   broadcast->SetName(title);
}
