#define ALLOW_ACCESS_TO_INTERNALS_OF_MENUS

#include <PalmOS.h>
#include "PalmChars.h"
#include "T-Suite.h"

static Boolean IsFiveWayNavPress (UInt16 keyCode)
{
	return ((keyCode == (navBitUp | navChangeUp)) ||
			(keyCode == (navBitDown | navChangeDown)) ||
			(keyCode == (navBitLeft | navChangeLeft)) ||
			(keyCode == (navBitRight | navChangeRight)) ||
			(keyCode == (navBitSelect | navChangeSelect)));
}

static Boolean IsFiveWaySelectUp (UInt16 keyCode)
{
//	return ((keyCode & 0x00FF) == (navBitUp | navBitLeft));
	return ((keyCode & 0x00FF) == (navBitUp | navBitSelect));
}

static Boolean IsFiveWaySelectDown (UInt16 keyCode)
{
//	return ((keyCode & 0x00FF) == (navBitDown | navBitRight));
	return ((keyCode & 0x00FF) == (navBitDown | navBitSelect));
}

static Boolean IsFiveWaySelectLeft (UInt16 keyCode)
{
//	return ((keyCode & 0x00FF) == (navBitDown | navBitRight));
	return ((keyCode & 0x00FF) == (navBitLeft | navBitSelect));
}

Err WbMnFiveWayEventPreHandle (SysNotifyParamType *notifyParamsP)
{
	EventType *event = (EventType*)notifyParamsP->notifyDetailsP;

// is keyDownEvent
	if (event->eType != Swap16 (keyDownEvent))
		return errNone;

// is FiveWayEvent
	EventType curEvent;
	MemSet (&curEvent, sizeof (EventType), 0);

	curEvent.eType =  (eventsEnum)Swap16 (event->eType);
	curEvent.data.keyDown.chr = Swap16 (event->data.keyDown.chr);
	curEvent.data.keyDown.keyCode = Swap16 (event->data.keyDown.keyCode);
	curEvent.data.keyDown.modifiers = Swap16 (event->data.keyDown.modifiers);

	if (!IsFiveWayNavEvent(&curEvent))
		return errNone;

// get Feature Value and Set Temp Feature Value
	UInt32 ftrValue;
	FtrGet (appFileCreator, ftr_appStatus, &ftrValue);
	TSuitePreferenceType *prefsP = (TSuitePreferenceType*)ftrValue;

	TSuitePreferenceType prefs;
		MemMove (&prefs, prefsP, sizeof (TSuitePreferenceType));

// check NavPush. if true then update Feature Value (FiveWayPushDetected)
	if (IsFiveWayNavPress (curEvent.data.keyDown.keyCode))
	{
		prefs.FiveWayPushDetected = IsOn;

		if (!prefsP->FiveWayPushDetected)
			DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));
	}

	// if FiveWay Push was not detected. change FiveWayEvent to nilEvent.
	if (!prefs.FiveWayPushDetected)
	{
		MemSet (event, sizeof (EventType), 0);
		return errNone;
	}

// check NavConbination Press.
	MenuBarType *menuP;

	menuP = MenuGetActiveMenu();

	// if FiveWay Select and Down combination press, then Menu Open.
	if (IsFiveWaySelectDown (curEvent.data.keyDown.keyCode))
	{
		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		if (MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL))
			return errNone;

		EvtEnqueueKey (vchrMenu, 0, commandKeyMask);
		goto SelectUpDownHandled;
	}

	// if FiveWay Select and Up combination press, then CommandToolBar Open.
	if (IsFiveWaySelectUp (curEvent.data.keyDown.keyCode))
	{
		menuP = MenuGetActiveMenu();

		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		EvtEnqueueKey (vchrCommand, 0, commandKeyMask);
		goto SelectUpDownHandled;
	}

	// if FiveWay Select and Up combination press, then Launch.
	if (IsFiveWaySelectLeft (curEvent.data.keyDown.keyCode))
	{
		menuP = MenuGetActiveMenu();

		if (menuP != NULL && menuP->attr.visible)
			return errNone;

		if (MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL))
			return errNone;

		EvtEnqueueKey (vchrLaunch, 0, commandKeyMask);
		goto SelectLeftHandled;
	}

	return errNone;

SelectUpDownHandled:
	MemSet (event, sizeof (EventType), 0);

SelectLeftHandled:
	prefs.FiveWayPushDetected = IsOff;
	DmWrite (prefsP, 0, &prefs, sizeof (TSuitePreferenceType));

	return errNone;
}