///////////////////////////////////////////////////////////
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2008-04-15
// last modified:	2008-04-15
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
//	- Function Cmenu_db::_On_DB_ImportSimpleFeature(CSG_String SF_Name)
//		uses the default name: "geom" for the geometry column
//		This could lead to an error if the geometry column name
//		of the feature is different from "geom" !!!
///////////////////////////////////////////////////////////

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

#include "soil_salinity/dialogs/pg_menu_db_soil_salinity_congruence_map_dlg.h"
#include "pgsql/pg_menu_db.h"
#include <string>

#include "saga_gui/wksp_data_manager.h"
#include "saga_gui/wksp_simplefeature_manager.h"
#include "saga_api/saga_api.h"

#include "saga_gui/saga_frame.h"		// location of class Cpgdatabase
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#include "saga_gui/wksp_data_manager.h" // adding items to SAGA Workspace *g_pData

#include "saga_gui/res_dialogs.h"		// using DLG_Parameters
//#include "saga_gui/res_commands.h"		// Definitions of menu constants (event handler)

#define MSG_ADD(s)	MSG_General_Add(s, true, true);
#include "saga_api/convert_sl.h"
//---------------------------------------------------------------------

//---------------------------------------------------------------------
BEGIN_EVENT_TABLE(CSoilSalinity_CongruenceMap, wxDialog)
	EVT_BUTTON(ID_SSCCM_BUTTON_OVERLAY,		_On_Overlay)
END_EVENT_TABLE()
//---------------------------------------------------------------------

CSoilSalinity_CongruenceMap::CSoilSalinity_CongruenceMap()
{
	_Create_Dialog();
}
//---------------------------------------------------------------------

CSoilSalinity_CongruenceMap::CSoilSalinity_CongruenceMap(wxWindow *parent, wxWindowID id, const wxString &title)
	: wxDialog(parent, id, title,
	  wxPoint(200,50),//wxDefaultPosition,
	  wxDefaultSize,
	  wxDEFAULT_DIALOG_STYLE)
{
	_Create_Dialog();
}
//---------------------------------------------------------------------

CSoilSalinity_CongruenceMap::~CSoilSalinity_CongruenceMap()
{
	_Delete_Controls();
}
//---------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////
//
//	EVEN HANDLER FUNCTIONS
//
///////////////////////////////////////////////////////////////////////

//---------------------------------------------------------------------
void CSoilSalinity_CongruenceMap::_On_Overlay(wxCommandEvent &event)
{
	CSG_SimpleFeature*	pSF_AssessmentMap;
	CSG_SimpleFeature*	pSF_RiskMap;
	CSG_SimpleFeature*	pSF_CongruenceMap = NULL;

	pSF_AssessmentMap	= _Get_Map(m_Item_AssessmentMap[m_pComboAssessmentMap->GetCurrentSelection()]);

	pSF_RiskMap			= _Get_Map(m_Item_RiskMap[m_pComboRiskMap->GetCurrentSelection()]);

	pSF_CongruenceMap = _Create_CongruenceMap(pSF_AssessmentMap, pSF_RiskMap);

	g_pData->Add(pSF_CongruenceMap);

	MSG_ADD(pSF_AssessmentMap->Get_Name());
	MSG_ADD(pSF_RiskMap->Get_Name());
}
//---------------------------------------------------------------------



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

//---------------------------------------------------------------------
CSG_SimpleFeature* CSoilSalinity_CongruenceMap::_Create_CongruenceMap(CSG_SimpleFeature *pSF_AssessmentMap, CSG_SimpleFeature *pSF_RiskMap)
{
	CSG_SimpleFeature* pSF_CongruenceMap = NULL;
	Cmenu_db menu_db;
	std::string baseName = pSF_AssessmentMap->Get_Name();
	baseName = baseName.substr(4,baseName.length());
	CSG_Data_Object* pObject = menu_db._On_DB_ImportSimpleFeature(baseName.c_str());
	
	pSF_CongruenceMap = pObject->asSimpleFeature();

	_Add_Specific_Congruence_Attributes(pSF_CongruenceMap);

	_Calculate_Congruence(pSF_CongruenceMap, pSF_AssessmentMap, pSF_RiskMap);

	return(pSF_CongruenceMap);
}
//---------------------------------------------------------------------

void CSoilSalinity_CongruenceMap::_Add_Specific_Congruence_Attributes(CSG_SimpleFeature *pSF_CongruenceMap)
{
	pSF_CongruenceMap->Get_Table().Add_Field("con_high", TABLE_FIELDTYPE_Float, 1);
	pSF_CongruenceMap->Get_Table().Add_Field("con_med", TABLE_FIELDTYPE_Float,1);
	pSF_CongruenceMap->Get_Table().Add_Field("con_low", TABLE_FIELDTYPE_Float,1);
	pSF_CongruenceMap->Get_Table().Add_Field("con_risk", TABLE_FIELDTYPE_Float,1);
}
//---------------------------------------------------------------------

void CSoilSalinity_CongruenceMap::_Calculate_Congruence(CSG_SimpleFeature *pSF_CongruenceMap, CSG_SimpleFeature *pSF_AssessmentMap, CSG_SimpleFeature *pSF_RiskMap)
{
	double assessment, risk;
	for (int i = 0; i < pSF_CongruenceMap->Get_Table().Get_Record_Count(); i++) {
		pSF_AssessmentMap->Get_Table().Get_Value(i,4,assessment);
		pSF_RiskMap->Get_Table().Get_Value(i,4,risk);
		pSF_CongruenceMap->Get_Table().Set_Value(i,1,_Get_Congruence_Value(assessment, risk));
	}
}
//---------------------------------------------------------------------

float CSoilSalinity_CongruenceMap::_Get_Congruence_Value(float assessment, float risk)
{
	if ( assessment - risk < 0 ) {
		return ( (assessment - risk) * -1 );
	} else {
		return (assessment - risk);
	}
}
//---------------------------------------------------------------------

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

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

void CSoilSalinity_CongruenceMap::_Create_Dialog()
{
	bool	bOverlay = false;

    wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );

    wxStaticBox *item2 = new wxStaticBox( this, -1, wxT("Soil Salinity Assessment Map") );
    item2->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *item1 = new wxStaticBoxSizer( item2, wxVERTICAL );

    wxBoxSizer *item3 = new wxBoxSizer( wxHORIZONTAL );

	// Assessment Map parameters
    wxStaticText *item4 = new wxStaticText( this, ID_SSCCM_TEXT, wxT("Assessment Map"), wxDefaultPosition, wxSize(100,-1), 0 );
    item3->Add( item4, 0, wxALIGN_CENTER|wxALL, 5 );

    m_pComboAssessmentMap = new wxComboBox( this, ID_SSCCM_COMBO_ASSESSMENT_MAP, wxT(""), wxDefaultPosition, wxSize(200,-1), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	wxArrayString astr = _Get_SoilSalinityMaps(SF_CATEGORY_SoilSalinityMap, m_Item_AssessmentMap);
	for (unsigned int i = 0; i < astr.size(); i++) {
		m_pComboAssessmentMap->Append(astr[i]);
	}
	m_pComboAssessmentMap->SetSelection(0);
    item3->Add( m_pComboAssessmentMap, 0, wxALIGN_CENTER|wxALL, 5 );
	if (astr.size() > 0) bOverlay = true;

    item1->Add( item3, 0, wxALIGN_CENTER|wxALL, 5 );

 //   wxBoxSizer *item6 = new wxBoxSizer( wxHORIZONTAL );

 //   wxStaticText *item7 = new wxStaticText( this, ID_SSCCM_TEXT, wxT("Year"), wxDefaultPosition, wxSize(100,-1), 0 );
 //   item6->Add( item7, 0, wxALIGN_CENTER|wxALL, 5 );


 //   wxComboBox *m_pComboYearAssessment = new wxComboBox( this, ID_SSCCM_COMBO_YEAR_ASSESSMENT, wxT(""), wxDefaultPosition, wxSize(120,-1), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	//m_pComboYearAssessment->SetSelection(0);
 //   item6->Add( m_pComboYearAssessment, 0, wxALIGN_CENTER|wxALL, 5 );

 //   item1->Add( item6, 0, wxALIGN_CENTER|wxALL, 5 );

 //   wxBoxSizer *item9 = new wxBoxSizer( wxHORIZONTAL );

 //   wxStaticText *item10 = new wxStaticText( this, ID_SSCCM_TEXT, wxT("Period"), wxDefaultPosition, wxSize(100,-1), 0 );
 //   item9->Add( item10, 0, wxALIGN_CENTER|wxALL, 5 );

 //   wxString strs11[] = 
 //   {
 //       wxT("Before Leaching"), 
 //       wxT("After Leaching")
 //   };
 //   wxComboBox *m_pComboPeriod = new wxComboBox( this, ID_SSCCM_COMBO_PERIOD, wxT(""), wxDefaultPosition, wxSize(120,-1), 2, strs11, wxCB_DROPDOWN|wxCB_READONLY );
	//m_pComboPeriod->SetSelection(0);
 //   item9->Add( m_pComboPeriod, 0, wxALIGN_CENTER|wxALL, 5 );

 //   item1->Add( item9, 0, wxALIGN_CENTER|wxALL, 5 );

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

	// Risk Map parameters
    wxStaticBox *item13 = new wxStaticBox( this, -1, wxT("Soil Salinity Risk Map") );
    item13->SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *item12 = new wxStaticBoxSizer( item13, wxVERTICAL );

    wxBoxSizer *item14 = new wxBoxSizer( wxHORIZONTAL );

    wxStaticText *item15 = new wxStaticText( this, ID_SSCCM_TEXT, wxT("Risk Map"), wxDefaultPosition, wxSize(100,-1), 0 );
    item14->Add( item15, 0, wxALIGN_CENTER|wxALL, 5 );

    m_pComboRiskMap = new wxComboBox( this, ID_SSCCM_COMBO_RISK_MAP, wxT(""), wxDefaultPosition, wxSize(200,-1), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	astr.Clear();
	astr = _Get_SoilSalinityMaps(SF_CATEGORY_SoilSalinityRiskMap, m_Item_RiskMap);
	for (unsigned int i = 0; i < astr.size(); i++) {
		m_pComboRiskMap->Append(astr[i]);
	}
	m_pComboRiskMap->SetSelection(0);
    item14->Add( m_pComboRiskMap, 0, wxALIGN_CENTER|wxALL, 5 );
	if (bOverlay && astr.size() > 0) {
		bOverlay = true;
	} else {
		bOverlay = false;
	}

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

 //   wxBoxSizer *item17 = new wxBoxSizer( wxHORIZONTAL );

 //   wxStaticText *item18 = new wxStaticText( this, ID_SSCCM_TEXT, wxT("Year"), wxDefaultPosition, wxSize(100,-1), 0 );
 //   item17->Add( item18, 0, wxALIGN_CENTER|wxALL, 5 );

 //   wxComboBox *m_pComboYearRisk = new wxComboBox( this, ID_SSCCM_COMBO_YEAR_RISK, wxT(""), wxDefaultPosition, wxSize(120,-1), 0, NULL, wxCB_DROPDOWN|wxCB_READONLY );
	//m_pComboYearRisk->SetSelection(0);
 //   item17->Add( m_pComboYearRisk, 0, wxALIGN_CENTER|wxALL, 5 );

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

 //   wxBoxSizer *item20 = new wxBoxSizer( wxHORIZONTAL );

 //   wxStaticText *item21 = new wxStaticText( this, ID_SSCCM_TEXT, wxT("Season"), wxDefaultPosition, wxSize(100,-1), 0 );
 //   item20->Add( item21, 0, wxALIGN_CENTER|wxALL, 5 );

 //   wxString strs22[] = 
 //   {
 //       wxT("Spring"), 
 //       wxT("Summer"), 
 //       wxT("Autumn"), 
 //       wxT("Winter")
 //   };
 //   wxComboBox *m_pComboSeason = new wxComboBox( this, ID_SSCCM_COMBO_SEASON, wxT(""), wxDefaultPosition, wxSize(120,-1), 4, strs22, wxCB_DROPDOWN|wxCB_READONLY );
	//m_pComboSeason->SetSelection(0);
 //   item20->Add( m_pComboSeason, 0, wxALIGN_CENTER|wxALL, 5 );

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

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


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

    wxButton *item24 = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxSize(150,-1), 0 );
    item23->Add( item24, 0, wxALIGN_CENTER|wxALL, 5 );

	m_pButton_Overlay = new wxButton( this, ID_SSCCM_BUTTON_OVERLAY, wxT("Overlay Maps"), wxDefaultPosition, wxSize(150,-1), 0 );
    item23->Add( m_pButton_Overlay, 0, wxALIGN_CENTER|wxALL, 5 );
	m_pButton_Overlay->Enable(bOverlay);

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

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




	//CSG_SimpleFeature* pSF_Assessment, *pSF_Risk;
	//CSG_Parameters	P;  // used to add Parameters in the second dialog
	//CSG_Parameter	*pNode;//, *pNode1;

	//P.Set_Name(LNG("Create Congruence Map"));
	//// Input file ----------------------------------------------
	//pNode = P.Add_SimpleFeatures(
	//	P("SF_ASSESSMENT")	, LNG("Soil Salinity Assessment Map"), 
	//	"Select Soil Salinity Assessment Map",
	//	"Select Soil Salinity Assessment Map",
	//	PARAMETER_INPUT
	//);
	//pNode = P.Add_SimpleFeatures(
	//	P("SF_RISK")	, LNG("Soil Salinity Risk Map"), 
	//	"Select Soil Salinity Risk Map",
	//	"Select Soil Salinity Risk Map",
	//	PARAMETER_INPUT
	//);

	//if( DLG_Parameters(&P) )
	//{
	//	// input table
	//	pSF_Assessment	= P("SF_ASSESSMENT")	->asSimpleFeature();
	//	pSF_Risk		= P("SF_RISK")			->asSimpleFeature();
	//}
}
//---------------------------------------------------------------------
//
void CSoilSalinity_CongruenceMap::_Delete_Controls()
{
	delete m_pComboAssessmentMap;
	//delete m_pComboYearAssessment;
	//delete m_pComboPeriod;
	delete m_pComboRiskMap;
	//delete m_pComboYearRisk;
	//delete m_pComboSeason;
	delete m_pButton_Overlay;
}
//---------------------------------------------------------------------

wxArrayString CSoilSalinity_CongruenceMap::_Get_SoilSalinityMaps(TSG_SF_CATEGORY SF_Category, vector_int& map)
{
	wxArrayString astr = NULL;
	
	if ( CWKSP_SimpleFeature_Manager* pSF_Manager = g_pData->Get_SimpleFeatures() ) {
		
		if ( CWKSP_SimpleFeature_Type* pSF_Type = pSF_Manager->Get_Shapes_Type(SHAPE_TYPE_Polygon) ) {

			map.clear();

			for (int i = 0; i < pSF_Type->Get_Count(); i++ ) {

				if ( pSF_Type->Get_SimpleFeatures(i)->Get_Shapes()->Get_SF_Category() == SF_Category ) {

					astr.Add(pSF_Type->Get_SimpleFeatures(i)->Get_Name());
					
					map.push_back(i);
				}
			}
		}
	}
	
	return(astr);

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

CSG_SimpleFeature* CSoilSalinity_CongruenceMap::_Get_Map(int Item)
{
	CSG_SimpleFeature* pSF = NULL;

	if ( CWKSP_SimpleFeature_Manager* pSF_Manager = g_pData->Get_SimpleFeatures() ) {
		
		if ( CWKSP_SimpleFeature_Type* pSF_Type = pSF_Manager->Get_Shapes_Type(SHAPE_TYPE_Polygon) ) {

			return ( pSF_Type->Get_SimpleFeatures(Item)->Get_Shapes() );
		}
	}
	return(NULL);
}
//---------------------------------------------------------------------


//CWKSP_SimpleFeature* CSoilSalinity_CongruenceMap::_Get_WKSP_SimpleFeature(CSG_SimpleFeature* pSimpleFeature)
//{
//	//if (Get_Active_Layer())	// CWKSP_Layer
//	//{
//		//CWKSP_Shapes_Manager* pShapesManager = g_pData->Get_Shapes();
//		CWKSP_SimpleFeature_Manager* pSimpleFeatureManager = g_pData->Get_SimpleFeatures();
//		//pSimpleFeatureManager->
//		//CSG_Parameter_SimpleFeature_List* pSF_List = new CSG_Parameter_SimpleFeature_List(
//		return( pSimpleFeatureManager->Get_Shapes(pSimpleFeature) );
//	//} else {
//	//	return(NULL);
//	//}
//}
//---------------------------------------------------------------------
