#include "StdAfx.h"

#include "choicesDlg.h"
#include "PluginResource.h"

// dialog control IDs
#define		kDialogID             12000
#define		kOK                   1
#define		kCancel               2
#define		kChoicesList          4
#define		kAddChoice            5
#define		kEditChoice           6
#define		kDelChoice            7
#define		kMoveUp               8
#define		kMoveDown             9

// custom strings IDs
#define		kStr_Addnewchoice     (short)12000, (short)19		// Add new choice
#define		kStr_Editchoice       (short)12000, (short)20		// Edit choice
#define		kStr_Choicetext       (short)12000, (short)21		// Choice text:
#define		kStr_Areyousureyouwanttodeletethatchoice  (short)12000, (short)22		// Are you sure you want to delete that choice?


EVENT_DISPATCH_MAP_BEGIN( CChoicesDlg );
ADD_DISPATCH_EVENT( kChoicesList, OnChoicesList );
ADD_DISPATCH_EVENT( kAddChoice, OnAddChoice );
ADD_DISPATCH_EVENT( kEditChoice, OnEditChoice );
ADD_DISPATCH_EVENT( kDelChoice, OnDelChoice );
ADD_DISPATCH_EVENT( kMoveUp, OnMoveUp );
ADD_DISPATCH_EVENT( kMoveDown, OnMoveDown );
EVENT_DISPATCH_MAP_END;

CChoicesDlg::CChoicesDlg(const TVWMemoryStringArray& arrList, size_t selIndex)
	: farrList( arrList )
{
	fLastListSel	= selIndex;
}

CChoicesDlg::~CChoicesDlg()
{
}

void CChoicesDlg::GetCoices(TVWMemoryStringArray& outArrList) const
{
	outArrList	= farrList;
}

size_t CChoicesDlg::GetSelection() const
{
	return fLastListSel;
}

bool CChoicesDlg::CreateDialogLayout()
{
	return this->CreateDialogLayoutFromRsrc( kDialogID );
}

void CChoicesDlg::OnInizializeContent()
{
	VWDialog::OnInizializeContent();

	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );
	size_t			choicesCnt		= farrList.size();
	for(size_t i=0; i<choicesCnt; i++) {
		const TXString&	strChoice	= farrList[ i ];
		pChoicesList->AddItem( strChoice );
	}

	if ( fLastListSel != size_t(-1) && fLastListSel < choicesCnt ) {
		pChoicesList->SelectIndex( fLastListSel );
	}
}

void CChoicesDlg::OnDDXInitialize()
{

}

void CChoicesDlg::OnUpdateUI()
{
	VWDialog::OnUpdateUI();

	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );

	size_t			choiceSel		= pChoicesList->GetSelectedIndex();
	size_t			choicesCnt		= pChoicesList->GetItemsCount();

	this->EnableControl( kDelChoice, choiceSel != size_t(-1) && choicesCnt > 0 );
	this->EnableControl( kEditChoice, choiceSel != size_t(-1) && choicesCnt > 0 );
	this->EnableControl( kMoveUp, choiceSel != size_t(-1) && choiceSel > 0 && choicesCnt > 0 );
	this->EnableControl( kMoveDown, choiceSel != size_t(-1) && choicesCnt > 0 && choiceSel < choicesCnt-1 );
}

void CChoicesDlg::OnChoicesList(long controlID, VWDialogEventArgs& eventArgs)
{
	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );
	fLastListSel					= pChoicesList->GetSelectedIndex();
}

void CChoicesDlg::OnAddChoice(long controlID, VWDialogEventArgs& eventArgs)
{
	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );

	CStandardEditTextBoxDlg	dlg(	TXString( kStr_Addnewchoice ),
									TXString( kStr_Choicetext ),
									"",
									32,
									1,
									TXString( (short)kDialogID, kOK ),
									TXString( (short)kDialogID, kCancel ) );
	dlg.SetAllowEmptyResult( false );
	if ( dlg.RunDialogLayout() == kDialogButton_Ok ) {
		TXString	newChoice	= dlg.GetValue();

		farrList.push_back( newChoice );
		pChoicesList->AddItem( newChoice );

		pChoicesList->SelectIndex( pChoicesList->GetItemsCount() );
	}
}

void CChoicesDlg::OnEditChoice(long controlID, VWDialogEventArgs& eventArgs)
{
	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );
	size_t			selIndex		= pChoicesList->GetSelectedIndex();
	if ( selIndex != size_t(-1) && selIndex < farrList.size() ) {
		TXString	currValue	= farrList[ selIndex ];

		CStandardEditTextBoxDlg	dlg(	TXString( kStr_Editchoice ),
										TXString( kStr_Choicetext ),
										currValue,
										32,
										1,
										TXString( (short)kDialogID, kOK ),
										TXString( (short)kDialogID, kCancel ) );
		dlg.SetAllowEmptyResult( false );
		if ( dlg.RunDialogLayout() == kDialogButton_Ok ) {
			TXString	newChoice	= dlg.GetValue();

			farrList[ selIndex ]	= newChoice;
			pChoicesList->SetItemText( selIndex, newChoice );

			pChoicesList->SelectIndex( pChoicesList->GetItemsCount() );
		}
	}
}

void CChoicesDlg::OnDelChoice(long controlID, VWDialogEventArgs& eventArgs)
{
	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );
	size_t			selIndex		= pChoicesList->GetSelectedIndex();
	if ( selIndex != size_t(-1) && selIndex < farrList.size() ) {
		if ( VWDialog::AlertQuestion( TXString(kStr_Areyousureyouwanttodeletethatchoice) ) == kDialogButton_Ok ) {
			farrList.erase( farrList.begin() + selIndex );
			pChoicesList->RemoveItem( selIndex );
		}
	}
}

void CChoicesDlg::OnMoveUp(long controlID, VWDialogEventArgs& eventArgs)
{
	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );
	size_t			selIndex		= pChoicesList->GetSelectedIndex();
	if ( selIndex != size_t(-1) && selIndex > 0 && selIndex < farrList.size() ) {
		TXString	c1				= farrList[ selIndex - 1 ];
		TXString	c2				= farrList[ selIndex ];

		farrList[ selIndex - 1 ]	= c2;
		farrList[ selIndex ]		= c1;
		pChoicesList->SetItemText( selIndex - 1,	c2 );
		pChoicesList->SetItemText( selIndex,		c1 );

		pChoicesList->SelectIndex( selIndex - 1 );
		fLastListSel		= selIndex - 1;
	}
}

void CChoicesDlg::OnMoveDown(long controlID, VWDialogEventArgs& eventArgs)
{
	VWListBoxCtrl*	pChoicesList	= this->GetListBoxCtrlByID( kChoicesList );
	size_t			selIndex		= pChoicesList->GetSelectedIndex();
	size_t			cnt				= farrList.size();
	if ( selIndex != size_t(-1) && cnt > 0 && selIndex < cnt-1 ) {
		TXString	c1				= farrList[ selIndex ];
		TXString	c2				= farrList[ selIndex + 1 ];

		farrList[ selIndex ]		= c2;
		farrList[ selIndex + 1 ]	= c1;
		pChoicesList->SetItemText( selIndex,		c2 );
		pChoicesList->SetItemText( selIndex + 1,	c1 );

		pChoicesList->SelectIndex( selIndex + 1 );
		fLastListSel		= selIndex + 1;
	}
}


