///////////////////////////////////////////////////////////
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2008-04-11
// last modified:	2008-09-23
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
//
///////////////////////////////////////////////////////////

#include "dialogs\pg_soil_salinity_dlg_base.h"
#include "pg_menu_db_soil_salinity.h"
#include "pgsql/pgdatabase.h"
#include "pgsql/pg_menu_db.h"

#include "saga_api/convert_sl.h"

#include "saga_gui/saga_frame.h"		// location of class Cpgdatabase
										// using global *g_pSAGA_Frame pointer
#include "saga_gui/res_commands.h"		// Definitions of menu constants (event handler)
#include "saga_gui/res_dialogs.h"		// using DLG_Parameters
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#define MSG_ADD(s)	MSG_General_Add(s, true, true);

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

Cpg_menu_db_soil_salinity::Cpg_menu_db_soil_salinity()
{
}
//---------------------------------------------------------------------

Cpg_menu_db_soil_salinity::~Cpg_menu_db_soil_salinity()
{
}
////---------------------------------------------------------------------
//
//
//
//
//CSG_Shapes* Cpg_menu_db_soil_salinity::Select_Shapefile_for_Export()
//{
//	CSG_Parameters	P;
//
//	P.Set_Name(LNG("Select a Shapefile as basis map")
//	);
//	
//	P.Add_Shapes(NULL, "SHAPE", "Choose a basis map",
//		"Choose a shape file as basis for the soil map",
//		PARAMETER_INPUT, SHAPE_TYPE_Polygon
//	);
//
//	P.Add_String(NULL, "NAME", LNG("Name"), "Choose a name for the map",
//		"ssm_"
//	);
//
//	if( DLG_Parameters(&P) )
//	{
//		m_pShapes = P("SHAPE")	->asShapes();
//		m_FeatureName = P("NAME")->asString();
//		return(m_pShapes);		
//	}
//
//	m_pShapes = NULL;
//	return(NULL);
//}
//---------------------------------------------------------------------

CSG_String Cpg_menu_db_soil_salinity::Get_Shape_Output_Name(CSG_Shapes *pShapes)
{
	CSG_String sname = "ssm_"; // ssm = Soil Salinity Base Map
	return(sname.Append(pShapes->Get_Name()));
}
//---------------------------------------------------------------------

void Cpg_menu_db_soil_salinity::Export_Shape_As_SoilSalinityBase_Map()
{
	//CSG_Shapes* pShapes = Select_Shapefile_for_Export();

	//if (pShapes != NULL) {

		////CSG_String outputName = Get_Shape_Output_Name(pShapes);

		//// create new shapefile
		//CSG_Shapes* pShapesTemp = new CSG_Shapes(*pShapes);
		////pShapesTemp->Set_Name(outputName.c_str());
		//pShapesTemp->Set_Name(m_FeatureName.c_str());

		//// adding primary key column and id's
		//Add_PK_Field_To_AttributeTable(pShapesTemp);
		//Add_IDs_To_PK_Field(pShapesTemp);

		//// create attribute and parameter settings tables in PostgreSQL
		//Create_External_Attributes(g_pSAGA_Frame->db->Cut_FileNameExtension(pShapesTemp->Get_Name()).c_str());

		//// add new shape to SAGA workspace
		////g_pData->Add(pShapesTemp);

		//// export new shape to PostgreSQL database
		//Cmenu_db menu_db;
		//menu_db._On_DB_ExportAsSimpleFeature(pShapesTemp, false);

		//delete pShapesTemp;
	//}
}
//---------------------------------------------------------------------

void Cpg_menu_db_soil_salinity::Add_PK_Field_To_AttributeTable(CSG_Shapes* pShapesOut)
{
	//// Add a column at the first position in the shape file attribute table
	//// that serves as primary key in the database
	//pShapesOut->Get_Table().Add_Field("FID",TABLE_FIELDTYPE_Int,0);

}
//---------------------------------------------------------------------
void Cpg_menu_db_soil_salinity::Create_External_Attributes(const char *tablename)
{
	////-----------------------------------------------------------------
	////	SOIL SALINITY ASSESSMENT
	////-----------------------------------------------------------------
	//Create_SoilSalinityAssessment_Attributes(tablename);

	////-----------------------------------------------------------------
	////	SOIL SALINITY parameter settings
	////-----------------------------------------------------------------
	//Create_ParameterSettings_Attributes(tablename);


	////-----------------------------------------------------------------
	////	SOIL SALINITY PARAMETER WEIGHTS
	////-----------------------------------------------------------------
	//Create_ParameterWeights(tablename);
	//Insert_DefaultWeights(tablename);
}
//---------------------------------------------------------------------

void Cpg_menu_db_soil_salinity::Create_SoilSalinityAssessment_Attributes(const char *tablename)
{
	//m_PK_colname = "PK_ID";

	//CSG_String sql;
	//CSG_String name;
	//name.Append("_attr_");
	//name.Append(tablename);

	//if ( g_pSAGA_Frame->db->CheckAccessibility(name.c_str()) ) {
	//	g_pSAGA_Frame->db->DropTable(name.c_str());
	//}

	//sql.Append("CREATE TABLE ");
	//sql.Append(name);
	//sql.Append("(");
	//sql.Append(m_PK_colname);
	//sql.Append(" serial primary key, ");		// ID primary key
	//sql.Append("FID int4 NOT NULL, Yr int4, Mnth varchar(3), Salinity varchar, Salinity_v float4,");
	//sql.Append("MS_LOW float4, MS_MEDIUM float4, MS_HIGH float4, Plant varchar)");
	//MSG_ADD(sql.c_str());

	//g_pSAGA_Frame->db->ExecSQL_wx(sql.c_str());

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

void Cpg_menu_db_soil_salinity::Create_ParameterSettings_Attributes(const char *tablename)
{
	//CSG_String sql;
	//CSG_String name;
	//CSG_String attr_name = "_attr_";
	//

	//name.Append("_settings_");
	//name.Append(tablename);
	//attr_name.Append(tablename);

	//if ( g_pSAGA_Frame->db->CheckAccessibility(name.c_str()) ) {
	//	g_pSAGA_Frame->db->DropTable(name.c_str());
	//}

	//sql.Append("CREATE TABLE ");
	//sql.Append(name);
	//sql.Append("(FK_ID int4 REFERENCES ");				// FK_ID = foreign key
	//sql.Append(attr_name);							//	= references primary key of attribute table
	//sql.Append(" (PK_ID) ON DELETE CASCADE, ");
	//sql.Append("FID int4 NOT NULL, sc_l int4, ");	// FID, soil colour (local knowledge)
	//sql.Append("nleaching int4, wq_l int4, ");	// number of leaching, water quality leaching
	//sql.Append("wq_i int4, wv_i int4, ");			// water quality irrigation; volume irrigation
	//sql.Append("even int4, morph int4, ");		// evenness, morphology
	//sql.Append("sc_e int4, pg int4, ");			// soil colour (expert), plant growth
	//sql.Append("nchannels int4, distance int4, ");	// number of channels, distance to nearest channel
	//sql.Append("maintanance int4)");				// maintanance of drainage network
	//MSG_ADD(sql.c_str());

	//g_pSAGA_Frame->db->ExecSQL_wx(sql.c_str());
}
//---------------------------------------------------------------------

void Cpg_menu_db_soil_salinity::Create_ParameterWeights(const char* tablename)
{
	//m_PK_colname = "PK_ID";

	//std::ostringstream sql;
	//CSG_String name;
	//name.Append("_weights_");
	//name.Append(tablename);

	//if ( g_pSAGA_Frame->db->CheckAccessibility(name.c_str()) ) {
	//	g_pSAGA_Frame->db->DropTable(name.c_str());
	//}

	//sql << "CREATE TABLE " << name <<"(";
	//sql << "sc_l int4 NOT NULL, ";
	//sql << "nleaching int4 NOT NULL, wq_l int4 NOT NULL, ";	
	//sql << "wq_i int4 NOT NULL, wv_i int4 NOT NULL, ";
	//sql << "even int4 NOT NULL, morph int4 NOT NULL, ";
	//sql << "sc_e int4 NOT NULL, pg int4 NOT NULL, ";
	//sql << "nchannels int4 NOT NULL, distance int4 NOT NULL, maintanance int4 NOT NULL, ";
	//sql << m_PK_colname << " serial primary key)";		// ID primary key
	//MSG_ADD(sql.str().c_str());

	//g_pSAGA_Frame->db->ExecSQL_wx(sql.str().c_str());
}
//---------------------------------------------------------------------

void Cpg_menu_db_soil_salinity::Insert_DefaultWeights(const char *tablename)
{
	//std::ostringstream sql;
	//CParmWeights w;
	//
	//sql << "INSERT INTO " << "_weights_" << tablename << " (";
	//sql << "sc_l, nleaching, wq_l, wq_i, wv_i, even, morph, ";
	//sql << "sc_e, pg, nchannels, distance, maintanance) ";
	//sql << "VALUES (";
	//sql << w.SoilColourLocal << ", " << w.nLeaching << ", ";
	//sql << w.WQ_Leaching << ", " << w.WQ_Irrigation << ", ";
	//sql << w.WVolumeIrrigation << ", " << w.Evenness << ", ";
	//sql << w.Morphology << ", " << w.SoilColourExpert << ", ";
	//sql << w.PlantGrowth << ", " << w.nChannels << ", ";
	//sql << w.Distance << ", " << w.Maintanance << ")";
	//MSG_ADD(sql.str().c_str());

	//g_pSAGA_Frame->db->ExecSQL_wx(sql.str().c_str());
}

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

void Cpg_menu_db_soil_salinity::Add_IDs_To_PK_Field(CSG_Shapes* pShapesTemp)
{
	//// add numbers to primary key column FID
	//CSG_Table_Record	*pRecord;
	//CSG_String str;
	//for (int i = 0; i < pShapesTemp->Get_Count(); i++) {
	//	pRecord = pShapesTemp->Get_Table().Get_Record(i);
	//	str.Append(convert_sl::Int2String(i+1).c_str());
	//	pRecord->Set_Value(0,str.c_str());
	//	str.Clear();
	//}
}

///////////////////////////////////////////////////////////////////////
//
//	IMPORT
//
///////////////////////////////////////////////////////////////////////
void Cpg_menu_db_soil_salinity::Load_Map()
{
}
//---------------------------------------------------------------------
CSG_String Cpg_menu_db_soil_salinity::Get_Feature_Name(CSG_Data_Object* pObject)
{
	return(pObject->Get_Name());
}
//---------------------------------------------------------------------
void Cpg_menu_db_soil_salinity::Set_Feature_Name(CSG_Data_Object* pObject)
{
}

//---------------------------------------------------------------------
CSG_Data_Object* Cpg_menu_db_soil_salinity::Select_Import_Map()
{
	CSG_Data_Object		*pObject;
	string				tablename;
	string				geom_name;
	Cmenu_db			menu_db;

	//-----------------------------------------------------
	// DIALOG
	//-----------------------------------------------------
	CSG_Parameters		P;

	P.Set_Name(LNG("Import Simple Feature from PostGIS"));

	P.Add_Choice(
		NULL	, "SF_NAME"	, LNG("Select Simple Feature"),
		"",
		g_pSAGA_Frame->db->GetDBGeometryTables().c_str()
	);
	//-----------------------------------------------------
	
	if( DLG_Parameters(&P) )
	{
		
		tablename	= menu_db._GetFeatureNameFromSelection(P("SF_NAME")->asString());
		geom_name	= menu_db._Get_geom_name(P("SF_NAME")->asString());

		pObject = new CSG_SimpleFeature(g_pSAGA_Frame->db->GetConn(),tablename.c_str(),geom_name);
		
		return(pObject);
	} else {
		return(NULL);
	}
}


void Cpg_menu_db_soil_salinity::Add_Specific_Fields(CSG_Data_Object* pObject)
{
}