/*
 * $Id: control.c,v 1.3 2006/07/15 21:15:00 thomas Exp $
 */

#include "control.h"
#include "config.h"
#include "common.h"

#include <vdr/remote.h>

#define TASTETIMEOUT (SetupTaste.MessageTimeout * 1000) // ms

bool cControlTaste::mRequested = false;

cControlTaste::cControlTaste(int LastChannel, const cChannel *Channel, const cEvent *Present, const cEvent *Following):
    cControl(new cPlayer),
    mLastChannel(LastChannel),
		mChannel(Channel),
		mPresent(Present),
		mFollowing(Following),
		mStart(0),
		mSwitch(true),
		mOsd(NULL)
{
#if VDRVERSNUM >= 10500
	SetNeedsFastResponse(true);
#else
	needsFastResponse = true;
#endif
	cRemote::Put(kOk, true); // Hide OSD
}

cControlTaste::~cControlTaste()
{
	if (mOsd != NULL)
		delete mOsd;

	if (mSwitch) {
		// possibly first or last available channel, fall back to old channel
		int direction = mChannel->Number() - mLastChannel;
		if (direction == 0)
			direction = 1;
		if (!cDevice::SwitchChannel(direction) && (mLastChannel != 0))
			Channels.SwitchTo(mLastChannel);
	}
}

void cControlTaste::Show(void)
{
	if (mOsd == NULL)
		mOsd = Skins.Current()->DisplayChannel(true);

	mOsd->SetChannel(mChannel, 0);
	mOsd->SetEvents(mPresent, mFollowing);
	mOsd->SetMessage(mtError, tr("Channel not acceptable!"));
	mOsd->Flush();

	mStart = time_ms();
}

eOSState cControlTaste::ProcessKey(eKeys Key)
{
	//dsyslog("cControlTaste::ProcessKey(%d) this = %p\n", Key, this);

  switch (Key) {
	case kNone:
		if (mStart == 0)
			Show();
		else if (time_ms() - mStart > TASTETIMEOUT) {
			mSwitch = true;
			return osEnd;
		}
	  return osContinue;

	case kOk:
		printf("kok\n");
		mRequested = true;
		printf("mswitch = false, return osEnd\n");
		mSwitch = false;
		if (mChannel != 0) {
			cDevice::PrimaryDevice()->SwitchChannel(mChannel, true);
		} else {
			isyslog("No previous channel known, won't zap!");
		}
		return osEnd;
		

	case kUp:
	case kDown:
	case kChanUp:
	case kChanDn:
		mSwitch = true;
		return osEnd;
		break;

  default:
    break;
  }
  return osContinue;
}

