///////////////////////////////////////////////////////////
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2008-04-14
// last modified:	2008-04-14
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
//	- tho load data dialog is not yet save. if for instance data
//		of the first feature are missing for a certain year, the
//		attribute table is filled in not correctly. hence it is
//		required to check the fid in the db and to input the data 
//		to the SAGA attribute table according to the fid.
///////////////////////////////////////////////////////////

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

#include "pg_soil_salinity_load_risk_data_dlg.h"
#include "saga_gui/SAGA_Frame.h"

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

BEGIN_EVENT_TABLE(CSoilSalinityLoadRiskDataDlg, wxDialog)
	EVT_BUTTON(ID_SSLRD_LOAD_DATA,		_On_Button_Load)
END_EVENT_TABLE()
//---------------------------------------------------------------------

CSoilSalinityLoadRiskDataDlg::CSoilSalinityLoadRiskDataDlg(CSG_SimpleFeature* pSimpleFeature, wxWindow *parent, wxWindowID id, const wxString &title)
	: CSoilSalinityLoadDataDlg(pSimpleFeature, parent, id, title, true)
{
	m_pSimpleFeature = pSimpleFeature;
	if (g_pSAGA_Frame->db->CheckAccessibility(_Get_AttributeTableName().c_str()) ) {
		
		_CreateDialog(pSimpleFeature);
	}
}
//---------------------------------------------------------------------

CSoilSalinityLoadRiskDataDlg::~CSoilSalinityLoadRiskDataDlg()
{
	//_Delete_Controls();
}



///////////////////////////////////////////////////////////////////////
//
//	EVENT HANDLER FUNCTIONS
//
///////////////////////////////////////////////////////////////////////

//---------------------------------------------------------------------
void CSoilSalinityLoadRiskDataDlg::_On_Button_Load(wxCommandEvent &event)
{
	if ( Get_Active_Layer() ) { // CWKSP_Layer
		CSG_Data_Object* m_pObject = Get_Active_Layer()->Get_Object();
		// Check, if a SimpleFeature is active
		if (m_pObject->Get_ObjectType() == DATAOBJECT_TYPE_SimpleFeature &&
			m_pObject->asSimpleFeature()->Get_SF_Category() == SF_CATEGORY_SoilSalinityRiskMap) {

			m_pSQLTable = new Csqltable(m_pSimpleFeature->Get_FeatureName());

			if ( g_pSAGA_Frame->db->Get_QueryResult(_Get_Query().c_str(), m_pSQLTable) ) {
				_Modify_SFTable(m_pSQLTable, 7);
			}
			delete m_pSQLTable;

		} else {
			SG_UI_Dlg_Message("Please select the soil salinity map!","Error: Wrong feature selected!");
		}		
	} else {
		SG_UI_Dlg_Message("Please select a SimpleFeature Layer","Error: Wrong feature selected!");
	}

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

CSG_String CSoilSalinityLoadRiskDataDlg::_Get_Query()
{
	CSG_String sql = "SELECT fid, year, season, risk, risk_v, ms_low, ms_medium, ms_high FROM ";
	sql.Append(_Get_AttributeTableName());
	sql.Append(" WHERE year = ");
	sql.Append(m_pComboYear->GetValue().c_str());
	sql.Append(" AND season = '");
	//sql.Append(m_pComboSeason->GetStringSelection());
	sql.Append("'");
	sql.Append(" ORDER BY fid");
	return(sql);
}
//---------------------------------------------------------------------




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

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





///////////////////////////////////////////////////////////////////////
//
//	DIALOG
//
///////////////////////////////////////////////////////////////////////
void CSoilSalinityLoadRiskDataDlg::_CreateDialog(CSG_SimpleFeature* pSimpleFeature)
{
    wxBoxSizer *mainBox = new wxBoxSizer( wxVERTICAL );
	wxStaticBox *pStatBoxYear = new wxStaticBox( this, wxID_ANY, wxT("Year") );
	wxStaticBoxSizer* pStatBoxSizerYear = new wxStaticBoxSizer( pStatBoxYear, wxHORIZONTAL );
	wxBoxSizer *ButtonBox = new wxBoxSizer( wxHORIZONTAL );

	// Year
	m_pComboYear = new wxComboBox( this, ID_SSLD_COMBO_YEAR, wxT(""), wxDefaultPosition, wxSize(80,-1), 0, NULL, wxCB_SORT|wxCB_READONLY );
	wxArrayString as = _Get_Years(_Get_SQL_Get_Years());
	for (unsigned int i = 0; i < as.size(); i++) {
		m_pComboYear->Append(as[i]);
	}
	m_pComboYear->SetSelection(0);
	pStatBoxSizerYear->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_SSLD_COMBO_SEASON, wxT(""), wxDefaultPosition, wxSize(80,-1), 4, strs17, wxCB_READONLY );
	//m_pComboSeason->SetSelection(0);
	pStatBoxSizerYear->Add( m_pComboSeason, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );

	// Buttons
	m_pButton_LoadData = new wxButton( this, ID_SSLRD_LOAD_DATA, wxT("Load"), wxDefaultPosition, wxSize(75,-1), 0 );
	wxButton* pButtonCancel = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxSize(75,-1), 0 );
	ButtonBox->Add( m_pButton_LoadData, 0, wxALIGN_CENTER|wxALL, 5 );
    ButtonBox->Add( pButtonCancel, 0, wxALIGN_CENTER|wxALL, 5 );

	// add features to mainBox
	mainBox->Add(pStatBoxSizerYear, 0, wxALIGN_LEFT|wxALL, 5 );
	mainBox->Add(ButtonBox, 0, wxALIGN_CENTER|wxALL, 5);

    this->SetSizer( mainBox );
	mainBox->Fit(this);
    mainBox->SetSizeHints( this );
}
//---------------------------------------------------------------------
