//-----------------------------------------------------------------------------
#include "ValuesFromUserDlg.h"
#include "wx/spinctrl.h"

using namespace std;

//=============================================================================
//============== Implementation ValuesFromUserDlg =====================
//=============================================================================

BEGIN_EVENT_TABLE(ValuesFromUserDlg, wxDialog)
	EVT_BUTTON(widBtnOk, ValuesFromUserDlg::OnBtnOk)
	EVT_BUTTON(widBtnCancel, ValuesFromUserDlg::OnBtnCancel)
END_EVENT_TABLE()

//-----------------------------------------------------------------------------
ValuesFromUserDlg::ValuesFromUserDlg( wxWindow* pParent, const wxString& title, const vector<ValueData*>& inputData )
: wxDialog(pParent, wxID_ANY, title)
//-----------------------------------------------------------------------------
{
	/*
		|-------------------------------------|
		| pTopDownSizer                       |
		|                spacer               |
		| |---------------------------------| |
		| | pInputControls                  | |
		| |---------------------------------| |
		|                spacer               |
		| |---------------------------------| |
		| | pButtonSizer                    | |
		| |---------------------------------| |
		|-------------------------------------|
	*/

	wxBoxSizer* pTopDownSizer = new wxBoxSizer(wxVERTICAL);
	pTopDownSizer->AddSpacer( 10 );
	
	wxPanel* pPanel = new wxPanel(this);

	int colCount = static_cast<int>(inputData.size());
	userInputData_.resize( colCount );
	ctrls_.resize( colCount );
	wxFlexGridSizer* pGridSizer = new wxFlexGridSizer(2, colCount);
	for( int i=0; i<colCount; i++ )
	{
		pGridSizer->Add( new wxStaticText(pPanel, wxID_ANY, inputData[i]->caption_));
		wxControl* p = 0;
		if( dynamic_cast<ValueRangeData*>(inputData[i]) )
		{
			ValueRangeData* pData = dynamic_cast<ValueRangeData*>(inputData[i]);
			p = new wxSpinCtrl(pPanel, widFirst + i, wxString::Format(wxT("%d"), pData->def_), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, pData->min_, pData->max_, pData->def_);
		}
		else if( dynamic_cast<ValueChoiceData*>(inputData[i]) )
		{
			ValueChoiceData* pData = dynamic_cast<ValueChoiceData*>(inputData[i]);
			p = new wxComboBox(pPanel, widFirst + i, pData->choices_[0], wxDefaultPosition, wxDefaultSize, pData->choices_);
		}
		else
		{
			wxASSERT( !"Invalid data type detected!" );
		}
		pGridSizer->Add( p, wxSizerFlags().Left() );
		ctrls_[i] = p;
	}
	pTopDownSizer->Add( pGridSizer );
	pTopDownSizer->AddSpacer( 10 );
	// lower line of buttons
	wxBoxSizer* pButtonSizer = new wxBoxSizer(wxHORIZONTAL);
	pButtonSizer->AddStretchSpacer( 100 );
	pBtnOk_ = new wxButton(pPanel, widBtnOk, wxT("&Ok"));
	pButtonSizer->Add( pBtnOk_, wxSizerFlags().Right().Border( wxALL, 7 ) );
	pBtnCancel_ = new wxButton(pPanel, widBtnCancel, wxT("&Cancel"));
	pButtonSizer->Add( pBtnCancel_, wxSizerFlags().Right().Border( wxALL, 7 ) );
	pTopDownSizer->Add( pButtonSizer, wxSizerFlags().Expand() );
	pPanel->SetSizer( pTopDownSizer );
	pTopDownSizer->SetSizeHints( this );
	SetClientSize( pTopDownSizer->GetMinSize() );
	SetSizeHints( GetSize() );
}

//-----------------------------------------------------------------------------
void ValuesFromUserDlg::OnBtnOk( wxCommandEvent& )
//-----------------------------------------------------------------------------
{
	EndModal( wxID_OK );
}

//-----------------------------------------------------------------------------
void ValuesFromUserDlg::OnBtnCancel( wxCommandEvent& )
//-----------------------------------------------------------------------------
{
	EndModal( wxID_CANCEL );
}
