////////////////////////////////////////////////////////////////
// Creating the menubar 'Database'
//
// Author:
// Stefan Liersch: stefan.liersch@googlemail.com, stefan.liersch@ufz.de
// 
// Date:			2006-10-15
// last modified:	2008-08-10
////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
// ToDo:
//--------------------------------------------------------------
// - Implement a function update to update the menu !!!!!!!!
// maybe call in: wksp_base_control.cpp; 
////////////////////////////////////////////////////////////////


#ifndef __MENU_DB_H__
#define __MENU_DB_H__

//#include <wx/wx.h>
#include "wx/datetime.h"

#include "saga_api/saga_api.h"

#include "pgsql/pgdatabase.h"
#include "pgsql/sqltable.h"
#include "pgsql/textviewer.h"
#include "pgsql/dialogs/pg_dlg_sgtable2pgsql.h"
#include "hydrology/rr_preprocessing.h"
#include "soil_salinity/dialogs/pg_soil_salinity_assessment_dlg.h"
#include "soil_salinity/pg_soil_salinity_map.h"

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

class ConnectionParameters
{
public:
	// Database connection parameters
	string		host;
	string		hostaddr;
	string		port;
	string		options;
	string		tty;
	string		dbname;
	string		user;
	string		password;
};
//---------------------------------------------------------------------

class CYear
{
public:
	int			year;
	const char	*method;
	double		jan;
	double		feb;
	double		mar;
	double		apr;
	double		may;
	double		jun;
	double		jul;
	double		aug;
	double		sep;
	double		oct;
	double		nov;
	double		dec;

	void		zeroAll()
	{
		year = 0;
		method ="";
		jan = 0;
		feb = 0;
		mar = 0;
		apr = 0;
		may = 0;
		jun = 0;
		jul = 0;
		aug = 0;
		sep = 0;
		oct = 0;
		nov = 0;
		dec = 0;
	}

};
//---------------------------------------------------------------------

class Cmenu_db
{
public:
	Cmenu_db(void);
	Cmenu_db(wxMenu *pMenu);
	~Cmenu_db(void);


	void			Destroy				(void);

	void			Add					(class wxMenu *pMenu);
	void			Del					(class wxMenu *pMenu);

	void			On_Command			(wxCommandEvent& event);

	void			Create_DB_Menu		(class wxMenu *m_pDB_Menu);

	// --------------------------------------------------------------
	// Event handler funcions
	void			On_ConnectDB		(void);
	void			On_DeConnectDB		(void);
	
	// Table
	void			On_DB_ImportTable	(void);
	void			On_DB_ExportTable	(void);
	void			On_DB_CreateTable	(void);
	void			On_DB_DeleteTable	(void);
	void			On_DB_ModifyTable	(void);
	
	// Vector data
	void			On_DB_ImportSimpleFeature(TSG_SF_CATEGORY category);
	void			On_DB_ExportAsSimpleFeature(void);
	void			On_DB_DeleteSimpleFeature();
	
	// SQL
	void			On_DB_CreateQuery(void);
	void			On_DB_ExecUserSQLCommand(void);
	void			On_DB_Create_SpatialQuery(void);

	// Time series
	void			On_DB_TimeSeries_AutoAnalysis(void);
	void			On_DB_TimeSeries_Average(void);
	void			CreateTimeSeries_Table(CSG_Table *pTable);
	void			AddRecord2TimeSeries_Table(CSG_Table *pTable, CYear year, int record, bool bfirstColYear);
	CYear			&AddSummary2TimeSeries_Table(const char* method, const char* tablename, const char* date_col,const char* value_col, CYear &summary);

	// Hydrological Modeling
	void			On_DB_Hydrology_Modeling(void);

	// Soil Salinty Assessment
	void			On_DB_Create_SoilSalinity_Base_Map(void);
	void			On_DB_Delete_SoilSalinityMap(void);
	void			On_DB_Load_SoilSalinityMap(void);

	CSoilSalinityMap* Get_SoilSalinityMap() { return(m_pSSMap); }
	

	// NOT IMPLEMENTED
	void			On_DB_ImportAsShape(void);
	void			On_DB_ExportAsShape(void);
	void			On_DB_ImportAsGML(void);
	void			On_DB_ExportAsGML(void);
	// --------------------------------------------------------------

	// create a table in SAGA workspace from database query
	void			CreateSAGATable(Csqltable *table, CSG_Table *table_in);
	void			Add_SAGATableField(int oid, const char* FieldName, CSG_Table *SAGATable);
	void			Add_SAGATableRecord(PGresult *res, CSG_Table *CSG_SAGATable, int row, int ncols);

	wxString		_Reduce_TableName(wxString tablename, char c);
	
	// --------------------------------------------------------------
	// Functions are implemented in pg_menu_db_functions.cpp
	// --------------------------------------------------------------
	bool			_On_DB_ExportAsSimpleFeature(CSG_Shapes *pShapes, CSG_String& FeatureName, bool bNameEditable);
	CSG_String		_GetFeatureNameFromSelection(string name);
	//CSG_String			_Get_geom_name(string tablename);

	bool			Select_Import_Map(CSG_String& FeatureName, CSG_String& GeomName);


private:

	void			_EnableDisableMenu(bool bconnected, wxMenu* pMenu);

	// --------------------------------------------------------------
	// Functions are implemented in pg_menu_db_functions.cpp
	// --------------------------------------------------------------
	void			_On_ConnectDB(void);
	void			_On_DeConnectDB(void);
	void			_On_DB_ImportTable(void);
	void			_On_DB_ExportTable(bool bSQLScript);
	void			_On_DB_DeleteTable(void);
	void			_On_DB_CreateQuery(void);
	void			_On_DB_ExecUserSQLCommand(void);
	void			_On_DB_Create_SpatialQuery(void);
	string			_GenerateConnectionString(ConnectionParameters connParms);

	// SimpleFeature
	void				_On_DB_ImportSimpleFeature(TSG_SF_CATEGORY category);
public:
	CSG_Data_Object*	_On_DB_ImportSimpleFeature(CSG_String SF_Name);
private:
	
	CSG_Shapes*			Select_Shapefile_for_Export();

	void				_On_DB_ExportAsSimpleFeature();

	void				_On_DB_DeleteSimpleFeature();
	void				_Drop_SimpleFeature(CSG_String SF_Name);

	// Functions used to parse and modify the table name.
	// This is necessary, because the geometry column was added to the
	// table name in function 'string Cpgdatabase::GetDBGeometryTables()'
	string			_Reduce_TableName(string tablename);
	string			_EraseBlanks(string &input);
	unsigned int	_FindFirstCharPosition(string &s, char search_char);

	// TimeSeries
	void			_On_DB_TimeSeries_AutoAnalysis(void);
	void			_On_DB_TimeSeries_Average(void);
	
	// returns SQL field type corresponding to SAGA table field types
	string			Get_FieldTypeFromSAGA(TSG_Table_Field_Type SG_ft);
	bool			is_numeric(TSG_Table_Field_Type SG_FT);
	// --------------------------------------------------------------

	// --------------------------------------------------------------
	// Hydrological Modeling
	// --------------------------------------------------------------
	// Functions are implemented in 'pg_menu_db_functions.cpp'
	// --------------------------------------------------------------
	void			_On_DB_Hydrology_Modeling(void);
	// Setting the first day of time series as begin
	void			_Set_Period_Begin(wxDateTime& begin_period, const char* tablename, const char* date_col);
	// Setting the last day of time series as end
	void			_Set_Period_End(wxDateTime& end_period, const char* tablename, const char* date_col);
	// Helper function to set the month to from database time series wxDate
	bool			_AssignMonthByInt(wxDateTime& date, int month); // where january = 0
	// --------------------------------------------------------------
	
	// --------------------------------------------------------------


	bool			DLG_ConnectDB();
	
	CSG_Table		*SelectSAGATable();

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

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


	int						m_nMenus;

	class wxMenu			**m_Menus;
	class CSoilSalinityMap	*m_pSSMap;

	wxMenu					*m_pDB_Menu;
	Csqltable				*m_psqltable;
	CSimpleFeature			*m_pSimpleFeature;

	CTextViewer				*txtView;
	ConnectionParameters	connParms;
	Cdlg_table2pg			*m_pExportTableDLG; // Export table dialog
	//CSQL_Record				*m_pRecord;
	CSoilSalinityAssessmentDlg		*m_pSoilSalinityDlg;
	// --------------------------------------------------------------
};
//-------------------------------------

#endif __MENU_DB_H__