#pragma mark ----------

#define ALLOW_ACCESS_TO_INTERNALS_OF_MENUS

#include <PalmOS.h>
#include "SonyChars.h"
#include "SonyHwrOEMIDs.h"
#include "T-Suite.h"
#include "T-Suite.rsrc.h"

#pragma mark ----------

TSuitePreferenceType tempPrefs;

#pragma mark ----------

static void SetTSuitePreference(TSuitePreferenceType *prefs)
{
	PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum, 
		prefs, sizeof (TSuitePreferenceType), false);
}

static void GetTSuitePreference (TSuitePreferenceType *prefs)
{
	UInt16 prefsSize;
	prefsSize = sizeof (TSuitePreferenceType);
	if (PrefGetAppPreferences (appFileCreator, appPrefID, prefs, &prefsSize, false) == 
		noPreferenceFound)
		{
			UInt32 ftrValue;
			if (FtrGet (appFileCreator, ftr_appStatus, &ftrValue) == errNone)
			{
				TSuitePreferenceType *prefsP = (TSuitePreferenceType*)ftrValue;
				MemMove (prefs, prefsP, sizeof (TSuitePreferenceType));
			}
			else
			{
				prefs->appLocalID = NULL;				// TSut
				prefs->fldP = NULL;						// BlkS
				prefs->chr = NULL;						// BlkS
				prefs->capsLock = NULL;					// FTgl
				prefs->numLock = NULL;					// FTgl
				prefs->tempShift = NULL;				// FTgl
				prefs->autoShifted = NULL;				// FTgl
	 			prefs->OnF = IsOff;						// TSut
	 			prefs->OffF = IsOn;						// TSut
	 			prefs->AutoEnbleAfterResetF = IsOff;	// TSut
	 			prefs->BlockAutoShiftingF = IsOff;		// BlkS
				prefs->MenuHwrKeyControlF = IsOff;		// WbMn *Tungsten/Zire=IsOn Other=IsOff
				prefs->ToolbarHwrKeyControlF = IsOff;	// WbMn
				prefs->OpenHwrKeyControlF = IsOff;		// WbMn
				prefs->prevCmdBtnIndex = NULL;			// WbMn
				prefs->numButtons = NULL;				// WbMn
				prefs->keyControlHandled = NULL;		// WbMn:temp
				prefs->FEPModeControlF = IsOff;			// FTgl
	 			prefs->AddNichiEiBtnF = IsOff;			// Ftgl
	 			prefs->AddHiraKataBtnF = IsOff;			// FTgl
	 			prefs->AddHenkanBtnF = IsOff;			// FTgl
	 			prefs->UnUsed = NULL;					// UnUsed
			}
		}

	SetTSuitePreference (prefs);
}

#pragma mark ----------

static void Disable(void)
{
	UInt16 cardNo;
	LocalID dbID;
	SysCurAppDatabase (&cardNo, &dbID);

	UInt32 value;
	if (!FtrGet (appFileCreator, ftr_appStatus, &value))
		Err error = MemHandleUnlock (DmGetResource ('code', 1));

	DmDatabaseProtect (cardNo, dbID, false);

	UInt16 attributes;
	DmDatabaseInfo (cardNo, dbID, NULL, &attributes, NULL, NULL, NULL, NULL,
		NULL, NULL, NULL, NULL, NULL);
	attributes = attributes & (~dmHdrAttrReadOnly);
	DmSetDatabaseInfo (cardNo, dbID, NULL, &attributes, NULL, NULL, NULL, NULL, NULL,
		NULL, NULL, NULL, NULL);

	DmOpenRef FEPBtnDbRef;
	DmSearchResource ('Tbmp', Btn_NichiEiBitmapFamily, NULL, &FEPBtnDbRef);
	if (FEPBtnDbRef)
		DmCloseDatabase (FEPBtnDbRef);

	SysNotifyUnregister(cardNo, dbID,
			sysNotifyEventDequeuedEvent, BlkSPriority);

	SysNotifyUnregister(cardNo, dbID,
			sysNotifyEventDequeuedEvent, WbMnPriority);

	SysNotifyUnregister(cardNo, dbID,
			sysNotifyEventDequeuedEvent, WbMmPriority);

	SysNotifyUnregister(cardNo, dbID,
			sysNotifyMenuCmdBarOpenEvent, WbMtPriority);
	SysNotifyUnregister(cardNo, dbID,
			appFileCreator, WbMtPriority);
	SysNotifyUnregister(cardNo, dbID,
			sysNotifyEventDequeuedEvent, WbMtPriority);

	SysNotifyUnregister(cardNo, dbID,
			sysNotifyMenuCmdBarOpenEvent, FTglPriority);
	SysNotifyUnregister(cardNo, dbID,
			appFileCreator, FTglPriority);
	SysNotifyUnregister(cardNo, dbID,
			sysNotifyEventDequeuedEvent, FTglPriority);

	FtrPtrFree (appFileCreator, ftr_appStatus);
}

static void Enable(void)
{

	TSuitePreferenceType prefs;
		GetTSuitePreference(&prefs);

	// clear temp values
	prefs.appLocalID = NULL;		// TSut
	prefs.fldP = NULL;				// BlkS
	prefs.chr = NULL;				// BlkS
	prefs.capsLock = NULL;			// FTgl
	prefs.numLock = NULL;			// FTgl
	prefs.tempShift = NULL;			// FTgl
	prefs.autoShifted = NULL;		// FTgl
	prefs.JogPushDetected = IsOff;	// WbMn
	prefs.prevCmdBtnIndex = NULL;	// WbMn
	prefs.numButtons = NULL;		// WbMn
	prefs.keyControlHandled = NULL;	// WbMn
	prefs.UnUsed = NULL;			// UnUsed

	UInt16 cardNo;
	LocalID dbID;
	SysCurAppDatabase (&cardNo, &dbID);

	UInt16 attributes;
	DmDatabaseInfo (cardNo, dbID, NULL, &attributes, NULL, NULL, NULL, NULL,
		NULL, NULL, NULL, NULL, NULL);
	attributes = attributes | dmHdrAttrReadOnly;
	DmSetDatabaseInfo (cardNo, dbID, NULL, &attributes, NULL, NULL, NULL, NULL, NULL,
		NULL, NULL, NULL, NULL);

	MemHandleLock (DmGetResource ('code', 1));

	DmDatabaseProtect (cardNo, dbID, true);

	prefs.appLocalID = dbID;

	TSuitePreferenceType *FEPTglPreferenceP; 
	FtrPtrNew (appFileCreator, ftr_appStatus,
			sizeof (TSuitePreferenceType), (void**)&FEPTglPreferenceP);
	DmWrite (FEPTglPreferenceP, 0, &prefs, sizeof (TSuitePreferenceType));

	if (prefs.BlockAutoShiftingF == IsOn)
	{
		SysNotifyRegister (cardNo, dbID, sysNotifyEventDequeuedEvent,
					 NULL, BlkSPriority, (MemPtr)'BlkS');
//					 &BlkSEventDequedHandle, BlkSPriority, (MemPtr)'BlkS');
	}

	if (prefs.OpenHwrKeyControlF == IsOn)
	{
		SysNotifyRegister (cardNo, dbID, sysNotifyEventDequeuedEvent,
					 NULL, WbMnPriority, (MemPtr)'WbMn');
//					 &WbMnJogEventPreHandle, WbMnPriority, (MemPtr)'WbMn');
	}

	if (prefs.MenuHwrKeyControlF == IsOn)
	{
		SysNotifyRegister (cardNo, dbID, sysNotifyEventDequeuedEvent,
					 NULL, WbMmPriority, (MemPtr)'WbMm');
//					 &WbMmMenuKeyDownHandle, WbMmPriority, (MemPtr)'WbMm');
	}

	if (prefs.ToolbarHwrKeyControlF == IsOn)
	{
		SysNotifyRegister (cardNo, dbID, sysNotifyMenuCmdBarOpenEvent,
					 NULL, WbMtPriority, (MemPtr)'WbMt');
//					 &WbMtCmdBarOpenHandle, WbMtPriority, (MemPtr)'WbMt');
	}

	if (prefs.FEPModeControlF == IsOn)
	{
		SysNotifyRegister (cardNo, dbID, sysNotifyMenuCmdBarOpenEvent,
					 NULL, FTglPriority, (MemPtr)'FTgl');
//					 &FTglCmdBarOpenHandle, FTglPriority, (MemPtr)'FTgl');
	}
}

#pragma mark --- FTgl Dialogue Form -

static void GetFTglCtrlValue (FormPtr frmP, TSuitePreferenceType *prefs)
{
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, FEPModeControlAddNichiEiBtnCheckbox  )) == true)
		prefs->AddNichiEiBtnF = IsOn; else prefs->AddNichiEiBtnF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, FEPModeControlAddHiraKataBtnCheckbox )) == true)
		prefs->AddHiraKataBtnF = IsOn; else prefs->AddHiraKataBtnF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, FEPModeControlAddHenkanBtnCheckbox   )) == true)
		prefs->AddHenkanBtnF = IsOn; else prefs->AddHenkanBtnF = IsOff;

	prefs->BtnInsPosition = (UInt8)(FrmGetControlGroupSelection (frmP, FEPModeControlGroupID)) - 7;
}

static void SetFTglCtrlValue (FormPtr frmP, TSuitePreferenceType *prefs)
{
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, FEPModeControlAddNichiEiBtnCheckbox), prefs->AddNichiEiBtnF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, FEPModeControlAddHiraKataBtnCheckbox), prefs->AddHiraKataBtnF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, FEPModeControlAddHenkanBtnCheckbox), prefs->AddHenkanBtnF);

	FrmSetControlGroupSelection (frmP, FEPModeControlGroupID, FEPModeControlRPushButton + prefs->BtnInsPosition);
}

#pragma mark --- T-Suite Dialogue Form -

static void GetTSuiteCtrlValue (FormPtr frmP, TSuitePreferenceType *prefs)
{
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteONPushButton)) == true)
		prefs->OnF = IsOn; else prefs->OnF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteOFFPushButton)) == true)
		prefs->OffF = IsOn; else prefs->OffF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteAutoEnableafterResetCheckbox)) == true)
		prefs->AutoEnbleAfterResetF = IsOn; else prefs->AutoEnbleAfterResetF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteBlockAutoShiftingCheckbox)) == true)
		prefs->BlockAutoShiftingF = IsOn; else prefs->BlockAutoShiftingF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteMenuHwrKeyControlCheckbox)) == true)
		prefs->MenuHwrKeyControlF = IsOn; else prefs->MenuHwrKeyControlF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteToolbarHwrKeyControlCheckbox)) == true)
		prefs->ToolbarHwrKeyControlF = IsOn; else prefs->ToolbarHwrKeyControlF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteOpenHwrKeyControlCheckbox)) == true)
		prefs->OpenHwrKeyControlF = IsOn; else prefs->OpenHwrKeyControlF = IsOff;
	if (FrmGetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteFEPModeControlCheckbox)) == true)
		prefs->FEPModeControlF = IsOn; else prefs->FEPModeControlF = IsOff;
}

static void SetTSuiteCtrlValue (FormPtr frmP, TSuitePreferenceType *prefs)
{
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteONPushButton), prefs->OnF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteOFFPushButton), prefs->OffF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteAutoEnableafterResetCheckbox), prefs->AutoEnbleAfterResetF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteBlockAutoShiftingCheckbox), prefs->BlockAutoShiftingF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteMenuHwrKeyControlCheckbox), prefs->MenuHwrKeyControlF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteToolbarHwrKeyControlCheckbox), prefs->ToolbarHwrKeyControlF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteOpenHwrKeyControlCheckbox), prefs->OpenHwrKeyControlF);
	FrmSetControlValue (frmP, FrmGetObjectIndex (frmP, TSuiteFEPModeControlCheckbox), prefs->FEPModeControlF);

	// Hide [FEP Mode Control Config Btn] when Btn Rsource was NotFound.
	LocalID FEPBtnDbID = DmFindDatabase (0, FEPBtnDbName);
	UInt16 objIndex = FrmGetObjectIndex (frmP, TSuiteFEPModeControlCnfGraphicButton);
	if (FEPBtnDbID)
		FrmShowObject (frmP, objIndex);
	else
	{
		FrmHideObject (frmP, objIndex);
		prefs->AddNichiEiBtnF = IsOff;			// Ftgl
		prefs->AddHiraKataBtnF = IsOff;			// FTgl
		prefs->AddHenkanBtnF = IsOff;			// FTgl
		SetTSuitePreference (prefs);
	}
}

#pragma mark ----------

static Boolean TSuiteDialogueFormHandle(EventPtr eventP)
{
	Boolean handled = false;
	FormPtr TSutFrmP;
	FormPtr FTglFrmP;

//	TSuitePreferenceType tempPrefs;
//	GetTSuitePreference (&tempPrefs);

	switch (eventP->eType) 
		{
		case frmOpenEvent:
			TSutFrmP = FrmGetActiveForm();
			SetTSuiteCtrlValue (TSutFrmP, &tempPrefs);			//Set TSut dialogue control value
			FrmDrawForm (TSutFrmP);
			handled = true;
			break;

		case ctlSelectEvent:
			UInt32 clieDeviceValue;
			FtrGet(sysFtrCreator, sysFtrNumOEMDeviceID, &clieDeviceValue);
			switch (eventP->data.ctlSelect.controlID)
			{
			case TSuiteMenuHwrKeyControlInfGraphicButton:
				switch (clieDeviceValue)
				{
				case sonyHwrOEMDeviceIDGoku:
				case sonyHwrOEMDeviceIDGohan:
					FrmAlert (MenuHwrKeyControlTJAlert);
						break;
				default:
					FrmAlert (MenuHwrKeyControlAlert);
				}
				handled = true;
				break;

			case TSuiteToolbarHwrKeyControlInfGraphicButton:
				switch (clieDeviceValue)
				{
				case sonyHwrOEMDeviceIDGoku:
				case sonyHwrOEMDeviceIDGohan:
					FrmAlert (ToolbarHwrKeyControlTJAlert);
						break;
				default:
					FrmAlert (ToolbarHwrKeyControlAlert);
				}
				handled = true;
				break;

			case TSuiteOpenHwrKeyControlInfGraphicButton:
				FrmAlert (OpenHwrKeyControlAlert);
				handled = true;
				break;

			case TSuiteFEPModeControlInfGraphicButton:
				FrmAlert (FEPModeControlAlert);
				handled = true;
				break;

			case TSuiteFEPModeControlCnfGraphicButton:
				FTglFrmP = FrmInitForm (FEPModeControlForm);	//Initialize T-Suite dialogue form
				SetFTglCtrlValue (FTglFrmP, &tempPrefs);		//Set FTgl dialogue control value
				if (FrmDoDialog (FTglFrmP) == FEPModeControlDoneButton)
					GetFTglCtrlValue (FTglFrmP, &tempPrefs);	//Get FTgl dialogue control value
				FrmDeleteForm (FTglFrmP);
				handled = true;
				break;

			case TSuiteDoneButton:
				GetTSuiteCtrlValue (FrmGetActiveForm(), &tempPrefs);			//Get TSut dialogue control value
				SetTSuitePreference (&tempPrefs);

			case TSuiteCancelButton:
				GetTSuitePreference (&tempPrefs);

				if (tempPrefs.OnF)
				{	Disable ();
					Enable ();}
				else
					Disable ();
				
				EventType event;
				event.eType = appStopEvent;
				EvtAddEventToQueue (&event);

				FrmCloseAllForms ();

				handled = true;
				break;
			}
			break;

//		case keyDownEvent:
//			if (NavSelectPressed (eventP))
//			{
//				EventType event;
//				event.eType = appStopEvent;
//				EvtAddEventToQueue (&event);
//
//				FrmCloseAllForms ();
//			}
//			handled = true;
//			break;

		default:
			break;
		
		}
	
	return handled;
}

static Boolean AppHandleEvent(EventPtr eventP)
{
	UInt16 formId;
	FormPtr frmP;

	if (eventP->eType == frmLoadEvent)
		{
		// Load the form resource.
		formId = eventP->data.frmLoad.formID;
		frmP = FrmInitForm(formId);
		FrmSetActiveForm(frmP);
		GetTSuitePreference (&tempPrefs);

		// Set the event handler for the form.  The handler of the currently
		// active form is called by FrmHandleEvent each time is receives an
		// event.
		switch (formId)
			{
			case TSuiteForm:
				FrmSetEventHandler(frmP, TSuiteDialogueFormHandle);
				break;

			default:
				break;
			}
		return true;
		}
	return false;
}

static void AppEventLoop(void)
{
	UInt16 error;
	EventType event;

	do {
		EvtGetEvent(&event, evtWaitForever);

		if (! SysHandleEvent(&event))
			if (! MenuHandleEvent(0, &event, &error))
				if (! AppHandleEvent(&event))
					FrmDispatchEvent(&event);

	} while (event.eType != appStopEvent);
}

static UInt32 StarterPalmMain (UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
//	Err error;

	switch (cmd)
		{
		case sysAppLaunchCmdNormalLaunch:
			FrmGotoForm(TSuiteForm);
			AppEventLoop();
			FrmCloseAllForms ();
			break;

		case sysAppLaunchCmdNotify:
			SysNotifyParamType* NtfyPrm = (SysNotifyParamType*)cmdPBP;
			switch (NtfyPrm->notifyType)
				{
				case sysNotifyEventDequeuedEvent:
					switch ((UInt32)(NtfyPrm->userDataP))
					{
					case 'BlkS':
						BlkSEventDequedHandle (NtfyPrm);
						break;
					case 'WbMn':
						WbMnJogEventPreHandle (NtfyPrm);
						break;
					case 'WbMm':
						WbMmMenuKeyDownHandle (NtfyPrm);
						break;
					case 'WbMt':
						UInt32 clieDeviceValue;
						FtrGet(sysFtrCreator, sysFtrNumOEMDeviceID, &clieDeviceValue);
						switch (clieDeviceValue)
						{
						case sonyHwrOEMDeviceIDGoku:
						case sonyHwrOEMDeviceIDGohan:
							WbMtCmdBarKeyDownHandleTJ (NtfyPrm);
							break;
						default:
							WbMtCmdBarKeyDownHandle (NtfyPrm);
						}
						break;
					case 'FTgl':
						FTglCmdBarKeyDownHandle (NtfyPrm);
						break;
					}
					break;

				case sysNotifyMenuCmdBarOpenEvent:
					switch ((UInt32)(NtfyPrm->userDataP))
					{
					case  'WbMt':
						WbMtCmdBarOpenHandle (NtfyPrm);
						break;
					case  'FTgl':
						FTglCmdBarOpenHandle (NtfyPrm);
						break;
					}
					break;

				case appFileCreator:
					switch ((UInt32)(NtfyPrm->userDataP))
					{
//					case 'WbMm':
//						break;
					case 'WbMt':
						WbMtCmdBarOpenedHandle (NtfyPrm);
						break;
					case 'FTgl':
						FTglCmdBarOpenedHandle (NtfyPrm);
						break;
					}
					break;
				}
			break;

		case sysAppLaunchCmdSystemReset:
			TSuitePreferenceType prefs;
			GetTSuitePreference (&prefs);
			if (prefs.OnF == IsOn)
			{
				if (prefs.AutoEnbleAfterResetF == IsOn)
					Enable();						//activate
				else
				{
					Disable();
					prefs.OnF  = IsOff;
					prefs.OffF = IsOn;
				}
				SetTSuitePreference (&prefs);
			}
			break;

		default:
			break;
		}

	return errNone;
}

UInt32 PilotMain ( UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
	return StarterPalmMain (cmd, cmdPBP, launchFlags);
}
