/*******************************************************************
 *
 *	File:		Dialogs.cpp
 *
 *	Author:		Peter van Sebille (peter@yipton.net)
 *
 *  Modified/adapted for picodriveN by notaz, 2006
 *
 *  (c) Copyright 2006, notaz
 *	(c) Copyright 2002, Peter van Sebille
 *	All Rights Reserved
 *
 *******************************************************************/

#include "Dialogs.h"
#include "Engine.h"
#include "picodrive.hrh"
#include "rsc/picodrive.rsg"

#include "version.h"
#include "CSimpleTextParser.h"
#include <txtrich.h>				// CRichText
#include <eikrted.h>				// CEikRichTextEditor
#include <qikvertoptionbuttonlist.h> // CEikHorOptionButtonList
#include <eikopbut.h>   // CEikOptionButton
#include <eikedwin.h>   // CEikEdwin
#include <quartzkeys.h> // EQuartzKeyTwoWayDown

#include <qikcommand.h>
#include "../common/emu.h"


/************************************************
 *
 * config Dialog
 *
 ************************************************/

CPicoConfigDialog::CPicoConfigDialog(_currentConfig_t &cfg) : config(cfg)
{
}

void CPicoConfigDialog::PostLayoutDynInitL()
{
	CEikHorOptionButtonList *buttons_rot   = (CEikHorOptionButtonList*) Control( ECtlOptRotation );
	CEikHorOptionButtonList *buttons_disp  = (CEikHorOptionButtonList*) Control( ECtlOptScreenMode );
	CEikCheckBox            *chkbox_altrend= (CEikCheckBox*)            Control( ECtlOptUseAltRend );
//	CEikCheckBox            *chkbox_acctmng= (CEikCheckBox*)            Control( ECtlOptUseAccTiming );
	CEikCheckBox            *chkbox_sram   = (CEikCheckBox*)            Control( ECtlOptUseSRAM );
	CEikCheckBox            *chkbox_fps    = (CEikCheckBox*)            Control( ECtlOptShowFPS );
	CEikCheckBox            *chkbox_sound  = (CEikCheckBox*)            Control( ECtlOptEnableSound );
	CEikCheckBox            *chkbox_z80    = (CEikCheckBox*)            Control( ECtlOptEmulateZ80 );
	CEikCheckBox            *chkbox_ym2612 = (CEikCheckBox*)            Control( ECtlOptEmulateYM2612 );
	CEikCheckBox            *chkbox_sn76496= (CEikCheckBox*)            Control( ECtlOptEmulateSN76496 );
	CEikChoiceListBase      *combo_sndq    = (CEikChoiceListBase*)      Control( ECtlOptSndQuality );
	CEikCheckBox            *chkbox_6bpad  = (CEikCheckBox*)            Control( ECtlOpt6ButtonPad );
	CEikCheckBox            *chkbox_gzipst = (CEikCheckBox*)            Control( ECtlOptGzipStates );
//	CEikCheckBox            *chkbox_accsprt= (CEikCheckBox*)            Control( ECtlOptUseAccSprites );
	CEikChoiceListBase      *combo_region  = (CEikChoiceListBase*)      Control( ECtlOptRegion );
	CEikOptionButton        *opt_fit2      = (CEikOptionButton*)        buttons_disp->ComponentControl( TPicoConfig::PMFit2 );
	CEikCheckBox            *chkbox_cdda   = (CEikCheckBox*)            Control( ECtlOptCDcdda );
	CEikCheckBox            *chkbox_pcm    = (CEikCheckBox*)            Control( ECtlOptCDpcm );
	CEikCheckBox            *chkbox_ramcart= (CEikCheckBox*)            Control( ECtlOptCDramcart );
	CEikCheckBox            *chkbox_sclrot = (CEikCheckBox*)            Control( ECtlOptCDscalerot );
	CEikCheckBox            *chkbox_bsync  = (CEikCheckBox*)            Control( ECtlOptCDbettersync );

	buttons_rot ->SetButtonById(ECtlOptRotation0 + config.rotation);
	buttons_disp->SetButtonById(ECtlOptScreenModeCenter + config.scaling);
	chkbox_sram   ->SetState(config.EmuOpt & 1     ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_fps    ->SetState(config.EmuOpt & 2     ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_sound  ->SetState(config.EmuOpt & 4     ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_gzipst ->SetState(config.EmuOpt & 8     ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_z80    ->SetState(config.s_PicoOpt& 4   ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_ym2612 ->SetState(config.s_PicoOpt& 1   ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_sn76496->SetState(config.s_PicoOpt& 2   ? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_altrend->SetState(config.s_PicoOpt& 0x10? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_6bpad  ->SetState(config.s_PicoOpt& 0x20? CEikButtonBase::ESet : CEikButtonBase::EClear);
//	chkbox_acctmng->SetState(config.s_PicoOpt& 0x40? CEikButtonBase::ESet : CEikButtonBase::EClear);
//	chkbox_accsprt->SetState(config.s_PicoOpt& 0x80? CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_cdda   ->SetState(config.s_PicoOpt&0x0800?CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_pcm    ->SetState(config.s_PicoOpt&0x0400?CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_ramcart->SetState(config.s_PicoOpt&0x8000?CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_sclrot ->SetState(config.s_PicoOpt&0x1000?CEikButtonBase::ESet : CEikButtonBase::EClear);
	chkbox_bsync  ->SetState(config.s_PicoOpt&0x2000?CEikButtonBase::ESet : CEikButtonBase::EClear);

	// dim "fit2" if we are not in 0 or 180 mode
	if (config.rotation != TPicoConfig::PRot0 && config.rotation != TPicoConfig::PRot180)
		opt_fit2->SetDimmed(ETrue);
	// dim some stuff for alternative renderer
	if (config.s_PicoOpt & 0x10) {
		// dim accurate sprites
		//chkbox_accsprt->SetState(CEikButtonBase::EClear);
		//chkbox_accsprt->SetDimmed(ETrue);
		// dim fit
		if(buttons_rot->LabeledButtonId() == ECtlOptRotation0 || buttons_rot->LabeledButtonId() == ECtlOptRotation180)
			((CEikOptionButton*)(buttons_disp->ComponentControl(TPicoConfig::PMFit)))->SetDimmed(ETrue);
	}

	TInt sel = 0;
	switch (config.s_PsndRate) {
		case 11025: sel = 1; break;
		case 16000: sel = 2; break;
		case 22050: sel = 3; break;
		case 44100: sel = 4; break;
	}
	sel += (config.s_PicoOpt&8) ? 5 : 0;
	if (sel >= 10) sel = 0;
	combo_sndq->SetCurrentItem(sel);

	switch(config.s_PicoRegion) {
		case 1: sel = 4; break;
		case 2: sel = 3; break;
		case 4: sel = 2; break;
		case 8: sel = 1; break;
		default:sel = 0; // auto
	}
	combo_region->SetCurrentItem(sel);
}

TBool CPicoConfigDialog::OkToExitL(TInt aButtonId)
{
	if(aButtonId != EEikBidOk) return ETrue;

	CEikHorOptionButtonList *buttons_rot   = (CEikHorOptionButtonList*) Control( ECtlOptRotation );
	CEikHorOptionButtonList *buttons_disp  = (CEikHorOptionButtonList*) Control( ECtlOptScreenMode );
	CEikCheckBox            *chkbox_altrend= (CEikCheckBox*)            Control( ECtlOptUseAltRend );
//	CEikCheckBox            *chkbox_acctmng= (CEikCheckBox*)            Control( ECtlOptUseAccTiming );
	CEikCheckBox            *chkbox_sram   = (CEikCheckBox*)            Control( ECtlOptUseSRAM );
	CEikCheckBox            *chkbox_fps    = (CEikCheckBox*)            Control( ECtlOptShowFPS );
	CEikCheckBox            *chkbox_sound  = (CEikCheckBox*)            Control( ECtlOptEnableSound );
	CEikCheckBox            *chkbox_z80    = (CEikCheckBox*)            Control( ECtlOptEmulateZ80 );
	CEikCheckBox            *chkbox_ym2612 = (CEikCheckBox*)            Control( ECtlOptEmulateYM2612 );
	CEikCheckBox            *chkbox_sn76496= (CEikCheckBox*)            Control( ECtlOptEmulateSN76496 );
	CEikChoiceListBase      *combo_sndq    = (CEikChoiceListBase*)      Control( ECtlOptSndQuality );
	CEikCheckBox            *chkbox_6bpad  = (CEikCheckBox*)            Control( ECtlOpt6ButtonPad );
	CEikCheckBox            *chkbox_gzipst = (CEikCheckBox*)            Control( ECtlOptGzipStates );
//	CEikCheckBox            *chkbox_accsprt= (CEikCheckBox*)            Control( ECtlOptUseAccSprites );
	CEikChoiceListBase      *combo_region  = (CEikChoiceListBase*)      Control( ECtlOptRegion );
	CEikCheckBox            *chkbox_cdda   = (CEikCheckBox*)            Control( ECtlOptCDcdda );
	CEikCheckBox            *chkbox_pcm    = (CEikCheckBox*)            Control( ECtlOptCDpcm );
	CEikCheckBox            *chkbox_ramcart= (CEikCheckBox*)            Control( ECtlOptCDramcart );
	CEikCheckBox            *chkbox_sclrot = (CEikCheckBox*)            Control( ECtlOptCDscalerot );
	CEikCheckBox            *chkbox_bsync  = (CEikCheckBox*)            Control( ECtlOptCDbettersync );

	config.rotation = (TPicoConfig::TPicoScreenRotation) (buttons_rot->LabeledButtonId() - ECtlOptRotation0);
	config.scaling  = (TPicoConfig::TPicoScreenMode) (buttons_disp->LabeledButtonId() - ECtlOptScreenModeCenter);

	if(chkbox_sram   ->State() == CEikButtonBase::ESet) config.EmuOpt |= 1;     else config.EmuOpt   &= ~1;
	if(chkbox_fps    ->State() == CEikButtonBase::ESet) config.EmuOpt |= 2;     else config.EmuOpt   &= ~2;
	if(chkbox_sound  ->State() == CEikButtonBase::ESet) config.EmuOpt |= 4;     else config.EmuOpt   &= ~4;
	if(chkbox_gzipst ->State() == CEikButtonBase::ESet) config.EmuOpt |= 8;     else config.EmuOpt   &= ~8;
	if(chkbox_z80    ->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 4;   else config.s_PicoOpt&= ~4;
	if(chkbox_ym2612 ->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 1;   else config.s_PicoOpt&= ~1;
	if(chkbox_sn76496->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 2;   else config.s_PicoOpt&= ~2;
	if(chkbox_altrend->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 0x10;else config.s_PicoOpt&= ~0x10;
	if(chkbox_6bpad  ->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 0x20;else config.s_PicoOpt&= ~0x20;
//	if(chkbox_acctmng->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 0x40;else config.s_PicoOpt&= ~0x40;
//	if(chkbox_accsprt->State() == CEikButtonBase::ESet) config.s_PicoOpt|= 0x80;else config.s_PicoOpt&= ~0x80;
	if(chkbox_cdda   ->State() == CEikButtonBase::ESet) config.s_PicoOpt |= 0x0800; else config.s_PicoOpt&= ~0x0800;
	if(chkbox_pcm    ->State() == CEikButtonBase::ESet) config.s_PicoOpt |= 0x0400; else config.s_PicoOpt&= ~0x0400;
	if(chkbox_ramcart->State() == CEikButtonBase::ESet) config.s_PicoOpt |= 0x8000; else config.s_PicoOpt&= ~0x8000;
	if(chkbox_sclrot ->State() == CEikButtonBase::ESet) config.s_PicoOpt |= 0x1000; else config.s_PicoOpt&= ~0x1000;
	if(chkbox_bsync  ->State() == CEikButtonBase::ESet) config.s_PicoOpt |= 0x2000; else config.s_PicoOpt&= ~0x2000;

	TInt sel = combo_sndq->CurrentItem();
	if(sel >= 5) { config.s_PicoOpt |= 8; sel-=5; } else config.s_PicoOpt &= ~8;
	switch (sel) {
		default:config.s_PsndRate =  8000; break;
		case 1: config.s_PsndRate = 11025; break;
		case 2: config.s_PsndRate = 16000; break;
		case 3: config.s_PsndRate = 22050; break;
		case 4: config.s_PsndRate = 44100; break;
	}

	switch (combo_region->CurrentItem()) {
		case 4: config.s_PicoRegion = 1; break;
		case 3: config.s_PicoRegion = 2; break;
		case 2: config.s_PicoRegion = 4; break;
		case 1: config.s_PicoRegion = 8; break;
		default:config.s_PicoRegion = 0; // auto
	}

	return ETrue;
}

// simple GUI stuff needs lots of code
void CPicoConfigDialog::HandleControlStateChangeL(TInt aControlId)
{
	if (aControlId == ECtlOptEnableSound)
	{
		CEikCheckBox *chkbox_sound  = (CEikCheckBox*) Control( ECtlOptEnableSound );
		CEikCheckBox *chkbox_z80    = (CEikCheckBox*) Control( ECtlOptEmulateZ80 );
		CEikCheckBox *chkbox_ym2612 = (CEikCheckBox*) Control( ECtlOptEmulateYM2612 );
		CEikCheckBox *chkbox_sn76496= (CEikCheckBox*) Control( ECtlOptEmulateSN76496 );

		if(chkbox_sound->State() == CEikButtonBase::ESet) {
			// check all sound chips too, but only if they all are not set
			if((chkbox_z80->State() | chkbox_ym2612->State() | chkbox_sn76496->State()) == CEikButtonBase::EClear) { // (==0)
				chkbox_z80    ->SetState(CEikButtonBase::ESet);
				chkbox_ym2612 ->SetState(CEikButtonBase::ESet);
				chkbox_sn76496->SetState(CEikButtonBase::ESet);
				chkbox_z80    ->DrawDeferred();
				chkbox_ym2612 ->DrawDeferred();
				chkbox_sn76496->DrawDeferred();
			}
		} else {
			// clear everything, but only if everything is set
			if((chkbox_z80->State() & chkbox_ym2612->State() & chkbox_sn76496->State()) == CEikButtonBase::ESet) { // (==1)
				chkbox_z80    ->SetState(CEikButtonBase::EClear);
				chkbox_ym2612 ->SetState(CEikButtonBase::EClear);
				chkbox_sn76496->SetState(CEikButtonBase::EClear);
				chkbox_z80    ->DrawDeferred();
				chkbox_ym2612 ->DrawDeferred();
				chkbox_sn76496->DrawDeferred();
			}
		}
	}
	else if(aControlId == ECtlOptUseAltRend || aControlId == ECtlOptRotation)
	{
		CEikCheckBox            *chkbox_altrend= (CEikCheckBox*)            Control( ECtlOptUseAltRend );
//		CEikCheckBox            *chkbox_accsprt= (CEikCheckBox*)            Control( ECtlOptUseAccSprites );
		CEikHorOptionButtonList *buttons_rot   = (CEikHorOptionButtonList*) Control( ECtlOptRotation );
		CEikHorOptionButtonList *buttons_disp  = (CEikHorOptionButtonList*) Control( ECtlOptScreenMode );
		CEikOptionButton        *opt_fit       = (CEikOptionButton*)        buttons_disp->ComponentControl( TPicoConfig::PMFit );
		CEikOptionButton        *opt_fit2      = (CEikOptionButton*)        buttons_disp->ComponentControl( TPicoConfig::PMFit2 );

		TBool dimmed = chkbox_altrend->State() == CEikButtonBase::ESet;
		// show/hide more stuff for alternative renderer
//		chkbox_accsprt->SetDimmed(dimmed);
		if(buttons_rot->LabeledButtonId() == ECtlOptRotation0 || buttons_rot->LabeledButtonId() == ECtlOptRotation180) {
			opt_fit->SetDimmed(dimmed);
			if(dimmed && buttons_disp->LabeledButtonId() == ECtlOptScreenModeFit)
				buttons_disp->SetButtonById(ECtlOptScreenModeFit2);
		}
		else opt_fit->SetDimmed(EFalse);
//		chkbox_accsprt->DrawDeferred();
		buttons_disp->DrawDeferred();

		if(buttons_rot->LabeledButtonId() == ECtlOptRotation0 || buttons_rot->LabeledButtonId() == ECtlOptRotation180) {
			opt_fit2->SetDimmed(EFalse);
		} else {
			if(opt_fit2->State() == CEikButtonBase::ESet) {
				buttons_disp->SetButtonById(ECtlOptScreenModeFit);
				buttons_disp->DrawDeferred();
			}
			opt_fit2->SetDimmed(ETrue);
		}
		opt_fit2->DrawDeferred();
	}
}


/************************************************
 *
 * About Dialog
 *
 ************************************************/

void CAboutDialog::PostLayoutDynInitL()
{
	TBuf<16>	versionText;
	TBuf<512>	text;

	#if (KPicoBuildNumber != 0)
		versionText.Format(_L("%d.%d%d"), KPicoMajorVersionNumber, KPicoMinorVersionNumber, KPicoBuildNumber);
	#else
		versionText.Format(_L("%d.%d"), KPicoMajorVersionNumber, KPicoMinorVersionNumber);
	#endif

	CEikRichTextEditor* richTextEd = LocateControlByUniqueHandle<CEikRichTextEditor>(ECtlPicoAboutText);
	User::LeaveIfNull(richTextEd);

	HBufC *aboutFormat = CEikonEnv::Static()->AllocReadResourceLC(R_PICO_TEXT_ABOUT);
	CSimpleTextFormatParser *parser = CSimpleTextFormatParser::NewLC();

	text.Format(*aboutFormat, &versionText);
	parser->ParseL(text, *richTextEd->RichText());

	richTextEd->UpdateAllFieldsL(); // Updates all the fields in the document

	CleanupStack::PopAndDestroy(parser);
	CleanupStack::PopAndDestroy(aboutFormat);
}

/*************************************************************
*
* Credits dialog
*
**************************************************************/

void CCreditsDialog::PreLayoutDynInitL()
{
	CEikEdwin *edwin = LocateControlByUniqueHandle<CEikEdwin>(ECtlPicoCreditsText);
	User::LeaveIfNull(edwin);

	CDesCArrayFlat* desArray = CEikonEnv::Static()->ReadDesCArrayResourceL(R_PICO_TBUF_CREDITS);
	CleanupStack::PushL(desArray);

	edwin->SetTextLimit(2048); // to prevent stupid "too big" warning
	TInt count = desArray->Count();
	for (TInt i = 0; i < count; i++)
	{
		edwin->Text()->InsertL(edwin->TextLength(), desArray->operator[](i));
		edwin->Text()->InsertL(edwin->TextLength(), CEditableText::ELineBreak);
	}
	CleanupStack::PopAndDestroy(desArray);
}

TKeyResponse CCreditsDialog::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
	if (aType == EEventKey)
	{
		CEikEdwin *edwin = LocateControlByUniqueHandle<CEikEdwin>(ECtlPicoCreditsText);
		User::LeaveIfNull(edwin);

		if (aKeyEvent.iCode == EQuartzKeyTwoWayDown)
		{
			edwin->MoveDisplayL(TCursorPosition::EFLineDown);
			edwin->UpdateScrollBarsL();
			return EKeyWasConsumed;
		}
		else if (aKeyEvent.iCode == EQuartzKeyTwoWayUp)
		{
			edwin->MoveDisplayL(TCursorPosition::EFLineUp);
			edwin->UpdateScrollBarsL();
			return EKeyWasConsumed;
		}
	}
	return CQikSimpleDialog::OfferKeyEventL(aKeyEvent, aType);
}

/*************************************************************
*
* Debug dialog
*
**************************************************************/

CDebugDialog::CDebugDialog(char *t)
{
	Mem::Copy(iText, t, 1024);
	iText[1023] = 0;
}

void CDebugDialog::PreLayoutDynInitL()
{
	char *p = iText, *line = iText;
	TBool end=0;
	TBuf<128> tbuf;
	CEikEdwin *editor = LocateControlByUniqueHandle<CEikEdwin>(ECtlPicoCreditsText);

	while(!end) {
		while(*p && *p != '\r' && *p != '\n') p++;
		if(!*p) end=1;
		*p = 0;
		TPtrC8 ptr((TUint8*) line);
		tbuf.Copy(ptr);
		editor->Text()->InsertL(editor->TextLength(), tbuf);
		editor->Text()->InsertL(editor->TextLength(), CEditableText::ELineBreak);
		line = ++p;
	}
}
