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

#include "menu_commands.h"
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>
#include "menu_searchactions.h"
#include "menu_searchresults.h"
#include "menu_recsdone.h"
#include "menu_switchtimers.h"
#include "menu_blacklists.h"
#include "tools.h"

extern int updateForced;
// --- cMenuSearchActions ---------------------------------------------------------

cMenuSearchActions::cMenuSearchActions(cSearchExt* Search, bool DirectCall)
:cOsdMenu(tr("Search actions"))
{
    directCall = DirectCall;
    SetHasHotkeys();
    
    search = Search;

    Add(new cOsdItem(hk(tr("Execute search"))));
    Add(new cOsdItem(hk(tr("Use as search timer on/off"))));
    Add(new cOsdItem(hk(tr("Trigger search timer update"))));
    Add(new cOsdItem(hk(tr("Show recordings done"))));
    Add(new cOsdItem(hk(tr("Use as template"))));
    Add(new cOsdItem(hk(tr("Show switch list"))));
    Add(new cOsdItem(hk(tr("Show blacklists"))));
    Add(new cOsdItem(hk(tr("Delete created timers?"))));
}

cMenuSearchActions::~cMenuSearchActions()
{
}


eOSState cMenuSearchActions::Search(void)
{
    return AddSubMenu(new cMenuSearchResults(search));
}

eOSState cMenuSearchActions::OnOffSearchtimer(void)
{
  if (search) 
  {
     search->useAsSearchTimer = search->useAsSearchTimer?0:1;
     SearchExts.Save();
     if (!search->useAsSearchTimer && Interface->Confirm(tr("Disable associated timers too?")))
	 search->OnOffTimers(search->useAsSearchTimer == 1);
     if (search->useAsSearchTimer && Interface->Confirm(tr("Activate associated timers too?")))
	 search->OnOffTimers(search->useAsSearchTimer == 1);
  }
  return osBack;
}


eOSState cMenuSearchActions::Execute(void)
{
  int current = Current();
  if (current <= 7)
  {
     if (current == 0)
     	return Search();
     if (current == 1)
	return OnOffSearchtimer(); 
     if (current == 2)
     {
	 if (Interface->Confirm(tr("Run search timer update?")))
	     updateForced = 2; // with message about completion
	 return osBack;
     }	
     if (current == 3)
	 return AddSubMenu(new cMenuRecsDone(search));
     if (current == 4)
     {
	 if (!Interface->Confirm(tr("Copy this entry to templates?")))
	     return osBack;
	 // unlink the search entry to allow an isolated copy to templates
	 cListObject* prev = search->Prev();
	 search->Unlink();
	 cSearchExt* templateObj = new cSearchExt;
	 *templateObj = *search;
	 templateObj->ID = SearchTemplates.GetNewID();
	 SearchTemplates.Add(templateObj);
	 // add search back to searches
	 SearchTemplates.Save();
	 SearchExts.Add(search, prev);
	 SearchExts.Save();
	 return osBack;
     }
     if (current == 5)
	 return AddSubMenu(new cMenuSwitchTimers());
     if (current == 6)
	 return AddSubMenu(new cMenuBlacklists());
     if (current == 7)
     {
	 if (!Interface->Confirm(tr("Delete all timers created from this search?")))
	     return osBack;
	 search->DeleteAllTimers();
	 return osBack;
     }
  }
  return osContinue;
}

eOSState cMenuSearchActions::ProcessKey(eKeys Key)
{
    bool hadSubmenu = HasSubMenu();
    eOSState state = cOsdMenu::ProcessKey(Key);

    // jump back to calling menu, if a command was called directly with key '1' .. '9'
    if (directCall && hadSubmenu && !HasSubMenu())
	return osBack;

    if (state == osUnknown) {
	switch (Key) {

	    case kGreen:
	    case kYellow:
	    case kBlue:
		return osContinue;
	    case kOk:  
		return Execute();
	    default:   break;
	}
    }
    return state;
}
