///////////////////////////////////////////////////////////
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2008-04-09
// last modified:	2008-04-09
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
//	- There is a weak point in _Get_InsertString(), the function
//		is taking the selected value of m_pChoice_Feature
//		to set fid.
//	- Column date is of type varchar, but should be bettes of type date
//	- Move On_Change_ID_Column und On_Change_Feature to base class
///////////////////////////////////////////////////////////

#include "libpq-fe.h"

#include "pg_soil_salinity_risk_dlg.h"

#include "saga_gui/SAGA_Frame.h"
#include "saga_gui/wksp_table_manager.h"
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#define MSG_ADD(s)	MSG_General_Add(s, true, true);
//---------------------------------------------------------------------

BEGIN_EVENT_TABLE(CSoilSalinityRiskDlg, wxDialog)
	EVT_CHOICE(ID_SSR_CHOICE_ID_COLUMN,				_On_Change_ID_Column)
	EVT_CHOICE(ID_SSR_CHOICE_FEATURE,				_On_Change_Feature)
	EVT_BUTTON(ID_SSR_BUTTON_CALC_RISK,				_On_Calculate_Risk)
	EVT_BUTTON(ID_SSR_BUTTON_SAVE,					_On_Save)
END_EVENT_TABLE()
//---------------------------------------------------------------------


CSoilSalinityRiskDlg::CSoilSalinityRiskDlg()
{
}

CSoilSalinityRiskDlg::CSoilSalinityRiskDlg(wxWindow *parent, wxWindowID id, const wxString &title)
	: wxDialog(parent, id, title,
	  wxDefaultPosition, wxDefaultSize,
	  wxDEFAULT_DIALOG_STYLE),// | wxRESIZE_BORDER),
	  CSoilSalinityDlgBase()
{
	_CreateDialog(NULL);
}

CSoilSalinityRiskDlg::CSoilSalinityRiskDlg(CSG_SimpleFeature* pSimpleFeature, wxWindow *parent, wxWindowID id, const wxString &title)
	: wxDialog(parent, id, title,
	  wxPoint(200,50),//wxDefaultPosition,
	  wxDefaultSize,
	  wxDEFAULT_DIALOG_STYLE),// | wxRESIZE_BORDER),
	  CSoilSalinityDlgBase(pSimpleFeature)
{
	_CreateDialog(pSimpleFeature);
}
//---------------------------------------------------------------------

CSoilSalinityRiskDlg::~CSoilSalinityRiskDlg()
{
	_Delete_Controls();
	m_parameters.resize(0);
	m_weights.resize(0);
}

//---------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////
//
//	EVENT HANDLER FUNCTION
//
///////////////////////////////////////////////////////////////////////

//---------------------------------------------------------------------

void CSoilSalinityRiskDlg::_On_Change_ID_Column(wxCommandEvent &event)
{
	m_ID_Column = m_pChoice_IDColumn->GetCurrentSelection();
	_Set_ChoiceBox(m_pChoice_Feature, _Get_FeatureIDs(m_pSimpleFeature, m_ID_Column), _Get_Selection(m_pSimpleFeature) );
	event.Skip();
}

//---------------------------------------------------------------------

void CSoilSalinityRiskDlg::_On_Change_Feature(wxCommandEvent& event)
{
	m_pWKSP_SimpleFeature->Get_Shapes()->Get_Table().Select(m_pChoice_Feature->GetCurrentSelection(),false);
	m_pWKSP_SimpleFeature->Get_Shapes()->Select(m_pChoice_Feature->GetCurrentSelection(),false);
	m_pSimpleFeatureManager->Update_Views(m_pSimpleFeature);

	m_CurrentSelection = m_pChoice_Feature->GetCurrentSelection();

	_Zero_Controls();
	m_pButton_Save->Enable(false);

	event.Skip();
}
//---------------------------------------------------------------------

void CSoilSalinityRiskDlg::_On_Calculate_Risk(wxCommandEvent& event)
{
	_Resize_Vectors(8);
	_Assign_Parms();
	_Assign_Weights();

	CRisk* pRisk = new CRisk(m_parameters,m_weights);

	m_pTXTCtrlResultStr->SetValue(pRisk->Get_Salinity_3str().c_str());
	m_pTXTCtrlResult->SetValue(convert_sl::Double2String(pRisk->Get_Risk()).c_str());

	if (pRisk->Get_MembershipDegree(0) < 0.0) 
		m_pTXTCtrl_MSLOW->SetValue("0");
	else
		m_pTXTCtrl_MSLOW->SetValue(convert_sl::Double2String(pRisk->Get_MembershipDegree(0)).c_str());

	if (pRisk->Get_MembershipDegree(1) < 0.0) 
		m_pTXTCtrl_MSMEDIUM->SetValue("0");
	else
		m_pTXTCtrl_MSMEDIUM->SetValue(convert_sl::Double2String(pRisk->Get_MembershipDegree(1)).c_str());

	if (pRisk->Get_MembershipDegree(2) < 0.0) 
		m_pTXTCtrl_MSHIGH->SetValue("0");
	else
		m_pTXTCtrl_MSHIGH->SetValue(convert_sl::Double2String(pRisk->Get_MembershipDegree(2)).c_str());

	m_pButton_Save->Enable(true);

	delete pRisk;

	event.Skip();
}
//---------------------------------------------------------------------
void CSoilSalinityRiskDlg::_On_Save(wxCommandEvent &event)
{
	if (_Record_Exists()) {
		if (wxYES == wxMessageBox(_Get_Message_Exist().c_str(),"Record exists!",
			wxNO_DEFAULT|wxYES_NO|wxICON_INFORMATION,this) ) {
				// UPDATE record
				if (g_pSAGA_Frame->db->ExecSQL_wx(_Get_SQL_Update_Record().c_str()) ) {
					MSG_ADD("Salinity values have been successfully updated");
				} else {
					MSG_ADD("UPDATE was NOT successful!");
				}
		}
	} else {
		if ( g_pSAGA_Frame->db->ExecSQL_wx(_Get_InsertString().c_str()) ) {
			MSG_ADD("Salinity values have been successfully saved to the database");
		} else {
			MSG_ADD("Saving was NOT successful!");
		}
	}

	m_pButton_Save->Enable(false);

	event.Skip();

}
//---------------------------------------------------------------------



///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------------------

bool CSoilSalinityRiskDlg::_Record_Exists()
{
	return(false);
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityRiskDlg::_Get_Message_Exist()
{
	return("");
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityRiskDlg::_Get_SQL_Update_Record()
{
	std::ostringstream update;
	update << "UPDATE " << _Get_AttributeTableName();
	update << " SET risk = '" << m_pTXTCtrlResultStr->GetValue() << "',";
	update << " risk_v = " << m_pTXTCtrlResult->GetValue() << ",";
	update << " ms_low = " << m_pTXTCtrl_MSLOW->GetValue() << ",";
	update << " ms_medium = " << m_pTXTCtrl_MSMEDIUM->GetValue() << ",";
	update << " ms_high = " << m_pTXTCtrl_MSHIGH->GetValue() << ",";
	update << " WHERE fid = " << m_pChoice_Feature->GetStringSelection();
	update << " AND year = " << m_pComboYear->GetStringSelection();//m_pComboYear->GetValue();
	update << " AND season = " << m_pComboSeason->GetStringSelection();

	return(update.str().c_str());
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityRiskDlg::_Get_InsertString()
{
	CSG_String sql;
	sql.Append("INSERT INTO ");
	sql.Append(_Get_AttributeTableName());
	sql.Append(" (fid, year, season, risk, risk_v, ms_low, ms_medium, ms_high) ");
	sql.Append("VALUES (");
	sql.Append(m_pChoice_Feature->GetStringSelection().c_str());
	sql.Append(", '");
	sql.Append(m_pComboYear->GetStringSelection());
	sql.Append("', '");
	sql.Append(m_pComboSeason->GetStringSelection());
	sql.Append("', '");
	sql.Append(m_pTXTCtrlResultStr->GetValue());
	sql.Append("', ");
	sql.Append(m_pTXTCtrlResult->GetValue());
	sql.Append(", ");
	sql.Append(m_pTXTCtrl_MSLOW->GetValue());
	sql.Append(", ");
	sql.Append(m_pTXTCtrl_MSMEDIUM->GetValue());
	sql.Append(", ");
	sql.Append(m_pTXTCtrl_MSHIGH->GetValue());
	sql.Append(")");

	return(sql);
}

//---------------------------------------------------------------------



///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------------------

void CSoilSalinityRiskDlg::_Resize_Vectors(int size)
{
	m_parameters.resize(size);
	m_weights.resize(size);
}
//---------------------------------------------------------------------

void CSoilSalinityRiskDlg::_Assign_Parms()
{
	m_parameters[0] = _Evaluate_Selection(m_pCombo_GWLevel->GetCurrentSelection(),4,false);
	m_parameters[1] = _Evaluate_Selection(m_pCombo_GWSalinity->GetCurrentSelection(),3, true); //0.5; // GW salinity is not yet solved
	//m_parameters[2] = _Evaluate_Selection(m_pCombo_SoilTexture->GetCurrentSelection(),3,false);
	m_parameters[2] = double(m_pSlider_SoilTexture->GetValue()) / double(m_pSlider_SoilTexture->GetMax());
	m_parameters[3] = double(m_pSlider_Planeness->GetValue()) / double(m_pSlider_Planeness->GetMax());
	m_parameters[4] = _Evaluate_Selection(m_pCombo_Morphology->GetCurrentSelection(),3,false);
	m_parameters[5] = _Evaluate_Selection(m_pCombo_nChannels->GetCurrentSelection(),5,false);
	m_parameters[6] = _Evaluate_Selection(m_pCombo_Distance->GetCurrentSelection(),3,false);
	m_parameters[7] = _Evaluate_Selection(m_pCombo_Maintenance->GetCurrentSelection(),3,false);

}
//---------------------------------------------------------------------

double CSoilSalinityRiskDlg::_Evaluate_Selection(int selection, int nPossibilities, bool bGoodFirst)
{
	// if bGoodFirst = true:
	//	best  value (selection) = first selection
	//	worst value (selection) = last selection
	// if bGoodFirst = false: the other way around
	if (bGoodFirst) {
		if (selection > 0) {
			return( double(selection) / double((nPossibilities-1)) );
		}
		return(0.0);
	}
	if ( selection > 0 ) {
		return( 1 - (double(selection) / double(nPossibilities-1)) );
	}
	return(1.0);
}
//---------------------------------------------------------------------

void CSoilSalinityRiskDlg::_Assign_Weights()
{
	m_weights[0] = 0.25;	// GW level
	m_weights[1] = 0.25;	// GW salinity
	m_weights[2] = 0.20;	// Soil texture
	m_weights[3] = 0.05;	// Planeness of field
	m_weights[4] = 0.05;	// Morphology of field
	m_weights[5] = 0.10;	// Number of drainage channels surrounding the field
	m_weights[6] = 0.05;	// Distance to the next drainage channel
	m_weights[7] = 0.05;	// Maintenance of the drainage system
}
//---------------------------------------------------------------------


///////////////////////////////////////////////////////////////////////
//
//	DIALOG
//
///////////////////////////////////////////////////////////////////////

void CSoilSalinityRiskDlg::_CreateDialog(CSG_SimpleFeature* pSimpleFeature)
{

	wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );

	// Feature (Map) Settings
	wxStaticBox *pStatBoxMapSettings = new wxStaticBox( this, -1, wxT("Map Settings") );
    pStatBoxMapSettings->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *pStatBoxSizerMapSettings = new wxStaticBoxSizer( pStatBoxMapSettings, wxVERTICAL );

    wxBoxSizer *item3_1 = new wxBoxSizer( wxHORIZONTAL );

    wxStaticText *item4_1 = new wxStaticText( this, ID_SSR_TEXT, wxT("Select ID-Column"), wxDefaultPosition, wxSize(95,-1), 0 );
    item3_1->Add( item4_1, 0, wxALIGN_CENTER|wxALL, 5 );
	//item3_1->Add( 5, 20, 0, wxALIGN_CENTER|wxALL, 5 );

   	m_pChoice_IDColumn = new wxChoice( this, ID_SSR_CHOICE_ID_COLUMN, wxDefaultPosition, wxSize(200,-1), NULL, 0 );
    wxArrayString as = _Get_ColumnNames(pSimpleFeature);
	for (unsigned int i = 0; i < as.size(); i++) {
		m_pChoice_IDColumn->Append(as[i]);
	}
	m_pChoice_IDColumn->SetSelection(0);
	item3_1->Add( m_pChoice_IDColumn, 0, wxALIGN_CENTER|wxALL, 5 );

    pStatBoxSizerMapSettings->Add( item3_1, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 );

    wxBoxSizer *item71 = new wxBoxSizer( wxHORIZONTAL );

    wxStaticText *item81 = new wxStaticText( this, ID_SSR_TEXT, wxT("Selected Feature ID"), wxDefaultPosition, wxSize(95,-1), 0 );
    item71->Add( item81, 0, wxALIGN_CENTER|wxALL, 5 );
	//item71->Add( 5, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pChoice_Feature = new wxChoice( this, ID_SSR_CHOICE_FEATURE, wxDefaultPosition, wxSize(200,-1),NULL, 0 );
	wxArrayString asCF = _Get_FeatureIDs(pSimpleFeature, 0); //m_ID_Column);
	for (unsigned int i = 0; i < asCF.size(); i++) {
		m_pChoice_Feature->Append(asCF[i]);
	}
	m_pChoice_Feature->SetSelection(m_CurrentSelection);
	item71->Add( m_pChoice_Feature, 0, wxALIGN_CENTER|wxALL, 5 );

	pStatBoxSizerMapSettings->Add( item71, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 );

	//---- Box Sizer Period ----
	wxBoxSizer *BSPeriod = new wxBoxSizer( wxHORIZONTAL );

    wxStaticText *txtTime = new wxStaticText( this, ID_SSR_TEXT, wxT("Time"), wxDefaultPosition, wxSize(60,-1), 0 );
    BSPeriod->Add( txtTime, 0, wxALIGN_CENTER|wxALL, 5 );
	BSPeriod->Add( 25, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	// Year
	m_pComboYear = new wxComboBox( this, ID_SSR_COMBO_YEAR, wxT(""), wxDefaultPosition, wxSize(80,-1), 0, NULL, wxCB_SORT );
	for (int i = 2000; i < 2051; i++) {
		m_pComboYear->Append(convert_sl::Int2String(i).c_str());
	}
	m_pComboYear->SetSelection(8);
	BSPeriod->Add( m_pComboYear, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	// Season
    wxString strs17[] = 
    {
        wxT("Spring"), 
        wxT("Summer"),
		wxT("Autumn"),
		wxT("Winter")
    };
	m_pComboSeason = new wxComboBox( this, ID_SSR_COMBO_SEASON, wxT(""), wxDefaultPosition, wxSize(80,-1), 4, strs17, wxCB_READONLY );
	m_pComboSeason->SetSelection(0);
	BSPeriod->Add( m_pComboSeason, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	
	pStatBoxSizerMapSettings->Add( BSPeriod, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 );

	item0->Add( pStatBoxSizerMapSettings, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	//-----------------------------------------------------------------

	// Groundwater Box
	wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticBox *item3 = new wxStaticBox( this, -1, wxT("Groundwater") );
	item3->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer *item2 = new wxStaticBoxSizer( item3, wxVERTICAL );

	wxBoxSizer *item4 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item5 = new wxStaticText( this, ID_SSR_TEXT, wxT("Level"), wxDefaultPosition, wxSize(60,-1), 0 );
	item4->Add( item5, 0, wxALIGN_CENTER|wxALL, 5 );

	item4->Add( 25, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	wxString strs6[] = 
	{
		wxT("< 70 cm"), 
		wxT("70 - 80cm"), 
		wxT("80 - 170 cm"), 
		wxT("> 170 cm")
	};
	m_pCombo_GWLevel = new wxComboBox( this, ID_SSR_COMBO_GW_LEVEL, wxT(""), wxDefaultPosition, wxSize(100,-1), 4, strs6, wxCB_DROPDOWN );
	m_pCombo_GWLevel->SetSelection(0);
	item4->Add( m_pCombo_GWLevel, 0, wxALIGN_CENTER|wxALL, 5 );

	item4->Add( 50, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	item2->Add( item4, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	wxBoxSizer *item7 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item8 = new wxStaticText( this, ID_SSR_TEXT, wxT("Salinity"), wxDefaultPosition, wxSize(60,-1), 0 );
	item7->Add( item8, 0, wxALIGN_CENTER|wxALL, 5 );

	item7->Add( 25, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	wxString strs9[] =
	{
		wxT("low"),
		wxT("medium"),
		wxT("high")
	};
	m_pCombo_GWSalinity = new wxComboBox( this, ID_SSR_COMBO_GW_SALINITY, wxT(""), wxDefaultPosition, wxSize(100,-1), 3, strs9, wxCB_DROPDOWN );
	m_pCombo_GWSalinity->SetSelection(0);
	item7->Add( m_pCombo_GWSalinity, 0, wxALIGN_CENTER|wxALL, 5 );

	item7->Add( 50, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	item2->Add( item7, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	item1->Add( item2, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5 );
	//-----------------------------------------------------------------

	// Soil Box
	wxStaticBox *item11 = new wxStaticBox( this, -1, wxT("Soil") );
	item11->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer *item10 = new wxStaticBoxSizer( item11, wxVERTICAL );

	wxBoxSizer *item12 = new wxBoxSizer( wxVERTICAL );

	wxBoxSizer *item13 = new wxBoxSizer( wxHORIZONTAL );

	//wxStaticText *item14 = new wxStaticText( this, ID_SSR_TEXT, wxT("Texture"), wxDefaultPosition, wxSize(60,-1), 0 );
	//item12->Add( item14, 0, wxALIGN_CENTER|wxALL, 5 );

	//wxString strs15[] = 
	//{
	//	wxT("sandy"), 
	//	wxT("mixed"), 
	//	wxT("brown")
	//};
	//m_pCombo_SoilTexture = new wxComboBox( this, ID_SSR_COMBO_SOIL_TEXTURE, wxT(""), wxDefaultPosition, wxSize(100,-1), 3, strs15, wxCB_DROPDOWN );
	//m_pCombo_SoilTexture->SetSelection(0);
	//item13->Add( m_pCombo_SoilTexture, 0, wxALIGN_CENTER|wxALL, 5 );

	//item12->Add( item13, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText *itembrown = new wxStaticText( this, ID_SSR_TEXT, wxT("clay"), wxDefaultPosition, wxSize(30,-1), 0 );
	item13->Add( itembrown, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pSlider_SoilTexture = new wxSlider( this, ID_SSR_SLIDER_SOIL_TEXTURE, 0, 0, 10, wxDefaultPosition, wxSize(100,-1), wxSL_HORIZONTAL );
	item13->Add( m_pSlider_SoilTexture, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText *itemsandy = new wxStaticText( this, ID_SSR_TEXT, wxT("sandy"), wxDefaultPosition, wxSize(30,-1), 0 );
	item13->Add( itemsandy, 0, wxALIGN_CENTER|wxALL, 5 );
	
	item12->Add( item13, 0, wxALIGN_CENTER|wxALL, 5 );

	item10->Add( item12, 0, wxALIGN_CENTER|wxALL, 5 );

	item1->Add( item10, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5 );

	item0->Add( item1, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	//-----------------------------------------------------------------


	// Surface Box
	wxBoxSizer *item16 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticBox *item18 = new wxStaticBox( this, -1, wxT("Surface") );
	item18->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer *item17 = new wxStaticBoxSizer( item18, wxVERTICAL );

	wxBoxSizer *item19 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item20 = new wxStaticText( this, ID_SSR_TEXT, wxT("Planeness"), wxDefaultPosition, wxSize(60,-1), 0 );
	item19->Add( item20, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText *item21 = new wxStaticText( this, ID_SSR_TEXT, wxT("plane"), wxDefaultPosition, wxSize(25,-1), 0 );
	item19->Add( item21, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pSlider_Planeness = new wxSlider( this, ID_SSR_SLIDER_PLANENESS, 0, 0, 10, wxDefaultPosition, wxSize(100,-1), wxSL_HORIZONTAL );
	item19->Add( m_pSlider_Planeness, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText *item23 = new wxStaticText( this, ID_SSR_TEXT, wxT("not plane"), wxDefaultPosition, wxSize(50,-1), 0 );
	item19->Add( item23, 0, wxALIGN_CENTER|wxALL, 5 );

	item17->Add( item19, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	wxBoxSizer *item24 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item25 = new wxStaticText( this, ID_SSR_TEXT, wxT("Morphology"), wxDefaultPosition, wxSize(60,-1), 0 );
	item24->Add( item25, 0, wxALIGN_CENTER|wxALL, 5 );

	item24->Add( 25, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	wxString strs26[] = 
	{
		wxT("depression"), 
		wxT("in between"), 
		wxT("hill")
	};
	m_pCombo_Morphology = new wxComboBox( this, ID_SSR_COMBO_MORPHOLOGY, wxT(""), wxDefaultPosition, wxSize(100,-1), 3, strs26, wxCB_DROPDOWN );
	m_pCombo_Morphology->SetSelection(0);
	item24->Add( m_pCombo_Morphology, 0, wxALIGN_CENTER|wxALL, 5 );

	item24->Add( 50, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	item17->Add( item24, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	item16->Add( item17, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5 );
	//-----------------------------------------------------------------

	// Drainage System Box
	wxStaticBox *item28 = new wxStaticBox( this, -1, wxT("Drainage System") );
	item28->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer *item27 = new wxStaticBoxSizer( item28, wxVERTICAL );

	wxBoxSizer *item29 = new wxBoxSizer( wxVERTICAL );

	wxBoxSizer *item30 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item31 = new wxStaticText( this, ID_SSR_TEXT, wxT("Channels"), wxDefaultPosition, wxSize(60,-1), 0 );
	item30->Add( item31, 0, wxALIGN_CENTER|wxALL, 5 );

	wxString strs32[] = 
	{
		wxT("0"), 
		wxT("1"), 
		wxT("2"), 
		wxT("3"), 
		wxT("4")
	};
	m_pCombo_nChannels = new wxComboBox( this, ID_SSR_COMBO_N_CHANNELS, wxT(""), wxDefaultPosition, wxSize(100,-1), 5, strs32, wxCB_DROPDOWN );
	m_pCombo_nChannels->SetSelection(0);
	item30->Add( m_pCombo_nChannels, 0, wxALIGN_CENTER|wxALL, 5 );

	item29->Add( item30, 0, wxALIGN_CENTER|wxALL, 5 );

	wxBoxSizer *item33 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item34 = new wxStaticText( this, ID_SSR_TEXT, wxT("Distance to channel"), wxDefaultPosition, wxSize(60,-1), 0 );
	item33->Add( item34, 0, wxALIGN_CENTER|wxALL, 5 );

	wxString strs35[] = 
	{
		wxT("far"), 
		wxT("moderately far"), 
		wxT("close")
	};
	m_pCombo_Distance = new wxComboBox( this, ID_SSR_COMBO_DISTANCE, wxT(""), wxDefaultPosition, wxSize(100,-1), 3, strs35, wxCB_DROPDOWN );
	m_pCombo_Distance->SetSelection(0);
	item33->Add( m_pCombo_Distance, 0, wxALIGN_CENTER|wxALL, 5 );

	item29->Add( item33, 0, wxALIGN_CENTER|wxALL, 5 );

	wxBoxSizer *item36 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item37 = new wxStaticText( this, ID_SSR_TEXT, wxT("Maintanance"), wxDefaultPosition, wxSize(60,-1), 0 );
	item36->Add( item37, 0, wxALIGN_CENTER|wxALL, 5 );

	wxString strs38[] = 
	{
		wxT("bad"), 
		wxT("moderate"), 
		wxT("good")
	};
	m_pCombo_Maintenance = new wxComboBox( this, ID_SSR_COMBO_MAINTANANCE, wxT(""), wxDefaultPosition, wxSize(100,-1), 3, strs38, wxCB_DROPDOWN );
	m_pCombo_Maintenance->SetSelection(0);
	item36->Add( m_pCombo_Maintenance, 0, wxALIGN_CENTER|wxALL, 5 );

	item29->Add( item36, 0, wxALIGN_CENTER|wxALL, 5 );

	item27->Add( item29, 0, wxALIGN_CENTER|wxALL, 5 );

	item16->Add( item27, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5 );

	item0->Add( item16, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	//-----------------------------------------------------------------

	// Salinity Risk Box
	wxBoxSizer *item39 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticBox *item41 = new wxStaticBox( this, -1, wxT("Calculate Salinity Risk") );
	item41->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
	wxStaticBoxSizer *item40 = new wxStaticBoxSizer( item41, wxVERTICAL );

	wxBoxSizer *item42 = new wxBoxSizer( wxHORIZONTAL );

	item42->Add( 107, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pButton_CalcRisk = new wxButton( this, ID_SSR_BUTTON_CALC_RISK, wxT("Calculate Salinization Risk"), wxDefaultPosition, wxSize(250,-1), 0 );
	item42->Add( m_pButton_CalcRisk, 0, wxALIGN_CENTER|wxALL, 5 );

	item42->Add( 108, 20, 0, wxALIGN_CENTER|wxALL, 5 );

	item40->Add( item42, 0, wxALIGN_CENTER|wxALL, 5 );

	wxBoxSizer *item44 = new wxBoxSizer( wxHORIZONTAL );

	wxStaticText *item45 = new wxStaticText( this, ID_SSR_TEXT, wxT("Risk"), wxDefaultPosition, wxDefaultSize, 0 );
	item44->Add( item45, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pTXTCtrlResultStr = new wxTextCtrl( this, ID_SSR_TEXTCTRL_RISK, wxT(""), wxDefaultPosition, wxSize(60,-1), wxTE_READONLY );
	item44->Add( m_pTXTCtrlResultStr, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pTXTCtrlResult = new wxTextCtrl( this, ID_SSR_TEXTCTRL_RISK_V, wxT(""), wxDefaultPosition, wxSize(60,-1), wxTE_READONLY );
	item44->Add( m_pTXTCtrlResult, 0, wxALIGN_CENTER|wxALL, 5 );

	item40->Add( item44, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	wxStaticBox *item48 = new wxStaticBox( this, -1, wxT("Membership Degree") );
	wxStaticBoxSizer *item47 = new wxStaticBoxSizer( item48, wxHORIZONTAL );

	wxStaticText *item49 = new wxStaticText( this, ID_SSR_TEXT, wxT("low"), wxDefaultPosition, wxDefaultSize, 0 );
	item47->Add( item49, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pTXTCtrl_MSLOW = new wxTextCtrl( this, ID_SSR_TEXTCTRL_MSLOW, wxT(""), wxDefaultPosition, wxSize(60,-1), wxTE_READONLY );
	item47->Add( m_pTXTCtrl_MSLOW, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText *item51 = new wxStaticText( this, ID_SSR_TEXT, wxT("medium"), wxDefaultPosition, wxDefaultSize, 0 );
	item47->Add( item51, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pTXTCtrl_MSMEDIUM = new wxTextCtrl( this, ID_SSR_TEXTCTRL_MSMDEDIUM, wxT(""), wxDefaultPosition, wxSize(60,-1), wxTE_READONLY );
	item47->Add( m_pTXTCtrl_MSMEDIUM, 0, wxALIGN_CENTER|wxALL, 5 );

	wxStaticText *item53 = new wxStaticText( this, ID_SSR_TEXT, wxT("high"), wxDefaultPosition, wxDefaultSize, 0 );
	item47->Add( item53, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pTXTCtrl_MSHIGH = new wxTextCtrl( this, ID_SSR_TEXTCTRL_MSHIGH, wxT(""), wxDefaultPosition, wxSize(60,-1), wxTE_READONLY );
	item47->Add( m_pTXTCtrl_MSHIGH, 0, wxALIGN_CENTER|wxALL, 5 );

	item40->Add( item47, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	item39->Add( item40, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	item0->Add( item39, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	//-----------------------------------------------------------------

	// Button Box
	wxBoxSizer *item55 = new wxBoxSizer( wxHORIZONTAL );

	m_pButton_Cancel = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxSize(120,-1), 0 );
	item55->Add( m_pButton_Cancel, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pButton_Save = new wxButton( this, ID_SSR_BUTTON_SAVE, wxT("Save"), wxDefaultPosition, wxSize(120,-1), 0 );
	m_pButton_Save->SetToolTip( wxT("Save record to database") );
	m_pButton_Save->Enable(false);
	item55->Add( m_pButton_Save, 0, wxALIGN_CENTER|wxALL, 5 );

	item0->Add( item55, 0, wxALIGN_CENTER|wxALL, 5 );
	//-----------------------------------------------------------------


	this->SetSizer( item0 );
	item0->Fit( this );
	item0->SetSizeHints( this );
}

//---------------------------------------------------------------------
void CSoilSalinityRiskDlg::_Delete_Controls()
{
	delete m_pChoice_IDColumn;
	delete m_pChoice_Feature;
	delete m_pComboYear;
	delete m_pComboSeason;
	delete m_pCombo_GWLevel;
	delete m_pCombo_GWSalinity;
	//delete m_pCombo_SoilTexture;
	delete m_pSlider_SoilTexture;
	delete m_pSlider_Planeness;
	delete m_pCombo_Morphology;
	delete m_pCombo_nChannels;
	delete m_pCombo_Distance;
	delete m_pCombo_Maintenance;
	delete m_pButton_CalcRisk;
	//delete m_pTXTCtrl_Risk;
	//delete m_pTXTCtrlResult;
	//delete m_pTXTCtrl_MSLOW;
	//delete m_pTXTCtrl_MSMEDIUM;
	//delete m_pTXTCtrl_MSHIGH;
	//delete m_pButton_Save;
}
