#include "epgsearchsetup.h"
#include "epgsearchcfg.h"
#include "searchtimer_thread.h"
#include "tools.h"
#include "changrp.h"

const char *RedKeyMode[2];
const char *BlueKeyMode[2];
const char *ProgressbarMode[4];
const char *StartMenuMode[2];

cMenuEPGSearchSetup::cMenuEPGSearchSetup(void)
{
  RedKeyMode[0] = tr("Standard");
  RedKeyMode[1] = tr("Commands");

  BlueKeyMode[0] = tr("Standard");
  BlueKeyMode[1] = tr("Button$Search");

  ProgressbarMode[0] = tr("no");
  ProgressbarMode[1] = tr("pipes");
  ProgressbarMode[2] = tr("graphical");

  StartMenuMode[0] = tr("Schedule");
  StartMenuMode[1] = tr("Now");

  Setup();
}

void cMenuEPGSearchSetup::Setup(void)
{
  data = EPGSearchConfig;
  Set();
}

void cMenuEPGSearchSetup::Set()
{
  int current = Current();
  Clear();
  Add(new cMenuEditBoolItem(tr("Hide main menu entry"),         &data.hidemenu,       tr("no"),      tr("yes")));
  Add(new cMenuEditBoolItem(tr("Replace original schedule"),    &data.ReplaceOrgSchedule, tr("no"),      tr("yes")));
  Add(new cMenuEditStraItem(tr("Start menu"), &data.StartMenu, 2, StartMenuMode));
  Add(new cMenuEditStraItem(tr("Red key"), &data.redkeymode, 2, RedKeyMode));
  Add(new cMenuEditStraItem(tr("Blue key"), &data.bluekeymode, 2, BlueKeyMode));
  Add(new cMenuEditStraItem(tr("Show progress in 'Now'"),         &data.showProgress, 3, ProgressbarMode));
  Add(new cMenuEditStraItem(tr("Show progress in 'Next'"),         &data.showProgressNext, 3, ProgressbarMode));
  if (data.showProgress || data.showProgressNext)
      Add(new cMenuEditIntItem(tr("  Display width"), &data.ProgressWidth, 4, 12));
  Add(new cMenuEditBoolItem(  tr("Show channel numbers"),         &data.showChannelNr,       tr("no"),      tr("yes")));
  cOsdItem *item = new cOsdItem(tr("Channel groups"));
  Add(item);

#if VDRVERSNUM && VDRVERSNUM >= 10315
  Add(new cMenuEditBoolItem(  tr("Use short channel names"),         &data.useShortChNames,       tr("no"),      tr("yes")));
#endif
  
  Add(new cMenuEditIntItem(tr("Time interval for FRew/FFwd [min]"), &data.timeShiftValue, 1, 9999));
  Add(new cMenuEditBoolItem(  tr("Toggle Green/Yellow"),         &data.toggleGreenYellow,       tr("no"),      tr("yes")));
  Add(new cMenuEditBoolItem(  tr("Ignore PayTV channels"),         &data.ignorePayTV,       tr("no"),      tr("yes")));
  char* szUseUserTime = NULL;
  asprintf(&szUseUserTime, "%s %d", tr("Use user time"), 1);
  Add(new cMenuEditBoolItem(szUseUserTime, &data.ShowModes[showUserMode1].bUseIt,       tr("no"),      tr("yes")));
  if (data.ShowModes[showUserMode1].GetUsage())
  {
      Add(new cMenuEditStrItem(tr("  Description"), data.ShowModes[showUserMode1].szDescription, sizeof(data.ShowModes[showUserMode1].szDescription), tr(FileNameChars)));
      Add(new cMenuEditTimeItem(tr("  Time"), &data.ShowModes[showUserMode1].iTime));
  } 
  free(szUseUserTime);

  asprintf(&szUseUserTime, "%s %d", tr("Use user time"), 2);
  Add(new cMenuEditBoolItem( szUseUserTime, &data.ShowModes[showUserMode2].bUseIt,       tr("no"),      tr("yes")));
  if (data.ShowModes[showUserMode2].GetUsage())
  {
      Add(new cMenuEditStrItem(tr("  Description"), data.ShowModes[showUserMode2].szDescription, sizeof(data.ShowModes[showUserMode2].szDescription), tr(FileNameChars)));
      Add(new cMenuEditTimeItem(tr("  Time"), &data.ShowModes[showUserMode2].iTime));
  } 
  free(szUseUserTime);

  asprintf(&szUseUserTime, "%s %d", tr("Use user time"), 3);
  Add(new cMenuEditBoolItem(szUseUserTime, &data.ShowModes[showUserMode3].bUseIt,       tr("no"),      tr("yes")));
  if (data.ShowModes[showUserMode3].GetUsage())
  {
      Add(new cMenuEditStrItem(tr("  Description"), data.ShowModes[showUserMode3].szDescription, sizeof(data.ShowModes[showUserMode3].szDescription), tr(FileNameChars)));
      Add(new cMenuEditTimeItem(tr("  Time"), &data.ShowModes[showUserMode3].iTime));
  } 
  free(szUseUserTime);

  asprintf(&szUseUserTime, "%s %d", tr("Use user time"), 4);
  Add(new cMenuEditBoolItem(szUseUserTime, &data.ShowModes[showUserMode4].bUseIt,       tr("no"),      tr("yes")));
  if (data.ShowModes[showUserMode4].GetUsage())
  {
      Add(new cMenuEditStrItem(tr("  Description"), data.ShowModes[showUserMode4].szDescription, sizeof(data.ShowModes[showUserMode4].szDescription), tr(FileNameChars)));
      Add(new cMenuEditTimeItem(tr("  Time"), &data.ShowModes[showUserMode4].iTime));
  } 
  free(szUseUserTime);

  Add(new cMenuEditBoolItem(  tr("Use search timers"),         &data.useSearchTimers,       tr("no"),      tr("yes")));
  if (data.useSearchTimers)
  {
      Add(new cMenuEditIntItem(tr("  Update interval [min]"), &data.UpdateIntervall, 1, 9999));
      Add(new cMenuEditIntItem(tr("  SVDRP port"), &data.SVDRPPort, 1, 99999));
      Add(new cMenuEditBoolItem(IndentMenuItem(tr("Check timer conflicts")),         &data.checkTimerConflicts,       tr("no"),      tr("yes")));
      Add(new cMenuEditIntItem(IndentMenuItem(tr("Setup.Recording$Default priority")), &data.DefPriority, 0, MAXPRIORITY));
      Add(new cMenuEditIntItem(IndentMenuItem(tr("Setup.Recording$Default lifetime (d)")), &data.DefLifetime, 0, MAXLIFETIME));
      Add(new cMenuEditIntItem(IndentMenuItem(tr("Setup.Recording$Margin at start (min)")), &data.DefMarginStart));
      Add(new cMenuEditIntItem(IndentMenuItem(tr("Setup.Recording$Margin at stop (min)")), &data.DefMarginStop));
  }

  SetCurrent(Get(current));
  Display();
}

void cMenuEPGSearchSetup::Store(void)
{
    bool bRestart = false;
    if (EPGSearchConfig.useSearchTimers != data.useSearchTimers)
    {
	bRestart = true;
	cSearchTimerThread::Exit();
    }

    EPGSearchConfig = data;

    SetupStore("HideMenu",  EPGSearchConfig.hidemenu);
    SetupStore("ReplaceOrgSchedule",  EPGSearchConfig.ReplaceOrgSchedule);
    SetupStore("StartMenu",  EPGSearchConfig.StartMenu);
    SetupStore("RedKeyMode",  EPGSearchConfig.redkeymode);
    SetupStore("BlueKeyMode",  EPGSearchConfig.bluekeymode);
    SetupStore("ShowProgress",  EPGSearchConfig.showProgress);
    SetupStore("ShowProgressNext",  EPGSearchConfig.showProgressNext);
    SetupStore("ProgressWidth",  EPGSearchConfig.ProgressWidth);
    SetupStore("ShowChannelNr",  EPGSearchConfig.showChannelNr);
    
    SetupStore("UserMode1UseIt",  EPGSearchConfig.ShowModes[showUserMode1].GetUsage());
    SetupStore("UserMode1Description",  EPGSearchConfig.ShowModes[showUserMode1].GetDescription());
    SetupStore("UserMode1Time",  EPGSearchConfig.ShowModes[showUserMode1].GetTime());
    
    SetupStore("UserMode2UseIt",  EPGSearchConfig.ShowModes[showUserMode2].GetUsage());
    SetupStore("UserMode2Description",  EPGSearchConfig.ShowModes[showUserMode2].GetDescription());
    SetupStore("UserMode2Time",  EPGSearchConfig.ShowModes[showUserMode2].GetTime());
  
    SetupStore("UserMode3UseIt",  EPGSearchConfig.ShowModes[showUserMode3].GetUsage());
    SetupStore("UserMode3Description",  EPGSearchConfig.ShowModes[showUserMode3].GetDescription());
    SetupStore("UserMode3Time",  EPGSearchConfig.ShowModes[showUserMode3].GetTime());
    
    SetupStore("UserMode4UseIt",  EPGSearchConfig.ShowModes[showUserMode4].GetUsage());
    SetupStore("UserMode4Description",  EPGSearchConfig.ShowModes[showUserMode4].GetDescription());
    SetupStore("UserMode4Time",  EPGSearchConfig.ShowModes[showUserMode4].GetTime());
    
    SetupStore("UseSearchTimers",  EPGSearchConfig.useSearchTimers);
    SetupStore("UpdateIntervall",  EPGSearchConfig.UpdateIntervall);
    SetupStore("SVDRPPort",  EPGSearchConfig.SVDRPPort);
    SetupStore("CheckTimerConflicts",  EPGSearchConfig.checkTimerConflicts);

    SetupStore("TimeIntervallFRFF",  EPGSearchConfig.timeShiftValue);
    SetupStore("ToggleGreenYellow",  EPGSearchConfig.toggleGreenYellow);

    SetupStore("ShortChannelNames",  EPGSearchConfig.useShortChNames);

    SetupStore("DefPriority",  EPGSearchConfig.DefPriority);
    SetupStore("DefLifetime",  EPGSearchConfig.DefLifetime);
    SetupStore("DefMarginStart",  EPGSearchConfig.DefMarginStart);
    SetupStore("DefMarginStop",  EPGSearchConfig.DefMarginStop);

    SetupStore("IgnorePayTV",  EPGSearchConfig.ignorePayTV);


    if (bRestart)
	cSearchTimerThread::Init();
}

eOSState cMenuEPGSearchSetup::ProcessKey(eKeys Key)
{
    int iTemp_useUserTime1 = data.ShowModes[showUserMode1].GetUsage();
    int iTemp_useUserTime2 = data.ShowModes[showUserMode2].GetUsage();
    int iTemp_useUserTime3 = data.ShowModes[showUserMode3].GetUsage();
    int iTemp_useUserTime4 = data.ShowModes[showUserMode4].GetUsage();
    int iTemp_showProgress = data.showProgress;
    int iTemp_useSearchTimers = data.useSearchTimers;

    if (!HasSubMenu())
	SetHelp(NULL, NULL, NULL, NULL);

    eOSState state = cMenuSetupPage::ProcessKey(Key);

    const char* ItemText = Get(Current())->Text();
    int iOnChannelGroups = 0;
    if (!HasSubMenu())
    {
	if (strstr(ItemText, tr("Channel groups")) == ItemText)
	{
	    SetHelp(NULL, NULL, NULL, tr("Setup"));
	    iOnChannelGroups = 1;
	}
    }

    if (iTemp_useUserTime1 != data.ShowModes[showUserMode1].GetUsage() ||
	iTemp_useUserTime2 != data.ShowModes[showUserMode2].GetUsage() ||
	iTemp_useUserTime3 != data.ShowModes[showUserMode3].GetUsage() ||
	iTemp_useUserTime4 != data.ShowModes[showUserMode4].GetUsage() ||
	iTemp_showProgress != data.showProgress ||
	iTemp_useSearchTimers != data.useSearchTimers)
    {
	Set();
	Display();
    }

    if (state == osUnknown) {
	switch (Key) {
	  case kBlue:
	      if (!HasSubMenu() && iOnChannelGroups == 1)
		  state = AddSubMenu(new cMenuChannelGroups);
	      break;
	  default: break;
      }
  }
    return state;
}
