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

#include "i18n.h"
#include "arghdirector.h"
#include "directorosd.h"

#include "menudirector.h"
#include "osdproxy.h"

// --- cMenuDirectorData -----------------------------------------------------

cMenuDirectorData::cMenuDirectorData()
{
  mHidePluginEntryInMainMenu = 0;
  mPortalmode = 1;
  mSwapKeys = 0;
  mSwapKeysInMenu = 0;
  mDisplayChannelInfo = 1;
  mSwitchChannelOnItemSelect = 1;
  mHidePluginAfterChannelSwitch = 0;
  mDisplayShortcutNumbers = 1;
  mDisplayEpgInfo = 1;
  mShowNext = 3;
  mHideButton = kOk;
  mHideButtonIndex = 0;
  mShowOldDirectorOsd = 0;
  mAutoStart = 1;
};

// --- cMenuEditStraItemTr -----------------------------------------------------

cMenuEditStraItemTr::cMenuEditStraItemTr(const char *Name, int *Value, int NumStrings, const char * const *Strings)
   : cMenuEditIntItem(Name, Value, 0, NumStrings - 1)
{
  strings = Strings;
  Set();
}

void cMenuEditStraItemTr::Set(void)
{
  SetValue(tr(strings[*value]));
}

#if VDRVERSNUM >= 10332
cDirectorStatus::cDirectorStatus(cPlugin* plugin)
{
	parent = plugin;
}
	
void cDirectorStatus::ChannelSwitch(const cDevice *Device, int ChannelNumber)
{
  const cChannel* Channel = Channels.GetByNumber(Device->CurrentChannel());
#if VDRVERSNUM < 10333
	if(Channel->GetLinkChannels() != NULL && Channel->GetLinkChannels()->Count() > 1)
#else
	if(Channel->LinkChannels() != NULL && Channel->LinkChannels()->Count() > 1)
#endif
		if(parent && ChannelNumber != 0)
		{
			//dont know why Channelnumber != 0 is necessary, but it is
			cRemote::CallPlugin(parent->Name());
			//dsyslog("cDirectorStatus::ChannelSwitch  Channelnumber: %d, Links: %d", ChannelNumber, Channel->LinkChannels()->Count());
		}
}
#endif

cPluginDirector::cPluginDirector(void)
{
  config = new cMenuDirectorData();

#if VDRVERSNUM >= 10332
   directorStatus = NULL;
#endif
}

cPluginDirector::~cPluginDirector()
{
   delete config;

#if VDRVERSNUM >= 10332
   delete directorStatus;
#endif
}

bool cPluginDirector::Start(void)
{
  RegisterI18n(Phrases);

#if VDRVERSNUM >= 10332
  if (config->mAutoStart)
     directorStatus = new cDirectorStatus(this);
#endif

  return true;
}

void cPluginDirector::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginDirector::MainMenuAction(void)
{
   if (config->mShowOldDirectorOsd)
      return new cDirectorOsd(config);

   return new cOsdProxy(cMenuDirector::Create,
                        cMenuDirector::KeyHandlerFunction,
                        cMenuDirector::Release,
                        "Director", config, 
                        config->mHideButton, true);
}

bool cPluginDirector::SetupParse(const char *Name, const char *Value)
{
	if (!strcasecmp(Name, "HideMenu"))
		config->mHidePluginEntryInMainMenu = atoi(Value);
	else if (!strcasecmp(Name, "SwapKeys"))
		config->mSwapKeys = atoi(Value);
	else if (!strcasecmp(Name, "SwapKeysInMenu"))
		config->mSwapKeysInMenu = atoi(Value);
	else if (!strcasecmp(Name, "PortalMode"))
		config->mPortalmode = atoi(Value);
	else if (!strcasecmp(Name, "DisplayChannelInfo"))
		config->mDisplayChannelInfo = atoi(Value);
	else if (!strcasecmp(Name, "SwitchChannelOnItemSelect"))
		config->mSwitchChannelOnItemSelect = atoi(Value);
	else if (!strcasecmp(Name, "HidePluginAfterChannelSwitch"))
		config->mHidePluginAfterChannelSwitch = atoi(Value);
	else if (!strcasecmp(Name, "DisplayShortcutNumbers"))
		config->mDisplayShortcutNumbers= atoi(Value);
	else if (!strcasecmp(Name, "DisplayEpgInfo"))
		config->mDisplayEpgInfo= atoi(Value);
	else if (!strcasecmp(Name, "ShowOldDirectorOsd"))
		config->mShowOldDirectorOsd= atoi(Value);
	else if (!strcasecmp(Name, "ShowPluginButton"))
		config->mHideButtonIndex = atoi(Value);
#if VDRVERSNUM >= 10332
   else if (!strcasecmp(Name, "AutoStart"))
      config->mAutoStart = atoi(Value);
#endif

   config->mHideButton = cMenuSetupDirector::allowedButtons[config->mHideButtonIndex];

	return true;
}

//***************************************************************************
// Setup
//***************************************************************************

eKeys cMenuSetupDirector::allowedButtons[] =
{
   kOk,
   kRed,
   kGreen,
   kYellow,
   kBlue,
   kUser1,
   kUser2,
   kUser3,
   kUser4,
   kUser5,
   kUser6,
   kUser7,
   kUser8,
   kUser9
};

const char* cMenuSetupDirector::allowedButtonNames[] =
{
   "Ok",
   "Red",
   "Green",
   "Yellow",
   "Blue",
   "User1",
   "User2",
   "User3",
   "User4",
   "User5",
   "User6",
   "User7",
   "User8",
   "User9",
   0
};

cMenuSetupDirector::cMenuSetupDirector(cMenuDirectorData* Config)
{
   config = Config;

	Add(new cMenuEditBoolItem(tr("Hide main menu entry"),                    &config->mHidePluginEntryInMainMenu, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Swap up and down"),                        &config->mSwapKeys, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Swap up and down in menu"),                &config->mSwapKeysInMenu, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Portal Mode"),                             &config->mPortalmode, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Display info on channel change"),          &config->mDisplayChannelInfo, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Switch channel on linkchannel selection"), &config->mSwitchChannelOnItemSelect, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Hide plugin after channel switch"),        &config->mHidePluginAfterChannelSwitch, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Display channel shortcut numbers"),        &config->mDisplayShortcutNumbers, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Display EPG information"),                 &config->mDisplayEpgInfo, tr("no"), tr("yes")));
	Add(new cMenuEditBoolItem(tr("Show old director OSD"),                   &config->mShowOldDirectorOsd, tr("no"), tr("yes")));
	Add(new cMenuEditStraItemTr(tr("Show plugin again on button"),           &config->mHideButtonIndex, NOOFALLOWEDBUTTONS, allowedButtonNames));

#if VDRVERSNUM >= 10332
   Add(new cMenuEditBoolItem(tr("Autostart plugin on reference-channels"), &config->mAutoStart, tr("no"), tr("yes")));
#endif
}

void cMenuSetupDirector::Store(void)
{
	SetupStore("HideMenu", config->mHidePluginEntryInMainMenu);
	SetupStore("SwapKeys", config->mSwapKeys);
	SetupStore("SwapKeysInMenu", config->mSwapKeysInMenu);
	SetupStore("PortalMode", config->mPortalmode);
	SetupStore("DisplayChannelInfo", config->mDisplayChannelInfo);
	SetupStore("SwitchChannelOnItemSelect", config->mSwitchChannelOnItemSelect);
	SetupStore("DisplayShortcutNumbers", config->mDisplayShortcutNumbers);
	SetupStore("HidePluginAfterChannelSwitch", config->mHidePluginAfterChannelSwitch);
	SetupStore("DisplayEpgInfo", config->mDisplayEpgInfo);
	SetupStore("ShowOldDirectorOsd", config->mShowOldDirectorOsd);
	SetupStore("ShowPluginButton", config->mHideButtonIndex);
#if VDRVERSNUM >= 10332
   SetupStore("AutoStart", config->mAutoStart);
#endif

   config->mHideButton = allowedButtons[config->mHideButtonIndex];
}

cMenuSetupPage *cPluginDirector::SetupMenu(void)
{
  return new cMenuSetupDirector(config);
}

VDRPLUGINCREATOR(cPluginDirector); // Don't touch this!
