#include "simplefeature_export_dlg_srid.h"
#include "pgsql/pgdatabase.h"

#include "saga_gui/helper.h"
//#include "saga_api/saga_api.h"
#include "saga_gui/saga_frame.h"
#include "libpq-fe.h"

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

BEGIN_EVENT_TABLE(CSimpleFeature_export_dlg_srid, wxDialog)
	EVT_SIZE(On_Size)
END_EVENT_TABLE()

//---------------------------------------------------------------------
// CONSTRUCTORS
CSimpleFeature_export_dlg_srid::CSimpleFeature_export_dlg_srid()
{
}

CSimpleFeature_export_dlg_srid::CSimpleFeature_export_dlg_srid(wxWindow *parent, wxWindowID id, const wxString &title, int selection)
													: wxDialog(parent, id, title,
													  wxPoint(50,80), //wxDefaultPosition,
													  wxDefaultSize,
													  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)

{
	_Create_DLG(selection);
}

// DESTRUCTOR
CSimpleFeature_export_dlg_srid::~CSimpleFeature_export_dlg_srid()
{}
//---------------------------------------------------------------------

void CSimpleFeature_export_dlg_srid::On_Size(wxSizeEvent &event)
{
	//this->SetSizer( MainBoxSizer );
	wxSize *s = new wxSize;
	int h = s->GetHeight();
	int w = s->GetWidth();
	
	//MainBoxSizer->Fit(this);
   // MainBoxSizer->SetSizeHints( this );
	//this->SetSizeHints(this);
	//m_pGrid_SRID->SetSize(w,h);
	event.Skip();
}

void CSimpleFeature_export_dlg_srid::_Create_DLG(int selection)
{
    MainBoxSizer = new wxBoxSizer( wxVERTICAL );
	
	// wxGrid ---------------------------------------------------------
	/*
	wxStaticBox *statBoxGrid = new wxStaticBox( this, -1, wxT("Spatial Reference System") );
	statBoxGrid->SetFont( wxFont( 8, wxSWISS, wxNORMAL, wxBOLD ) );
    wxStaticBoxSizer *statBoxSizerGrid = new wxStaticBoxSizer( statBoxGrid, wxHORIZONTAL );
*/
    //m_pGrid_SRID = new wxGrid( this, ID_GRID_SRID1, wxDefaultPosition, wxSize(450,200), wxWANTS_CHARS,wxPanelNameStr);
    m_pGrid_SRID = new wxGrid( this, ID_GRID_SRID1, wxDefaultPosition, wxSize(600,500), wxWANTS_CHARS,wxPanelNameStr);
    m_pGrid_SRID->CreateGrid( 0, 3, wxGrid::wxGridSelectRows );
	m_pGrid_SRID->SetColLabelValue(0,"SRID");
	m_pGrid_SRID->SetColLabelValue(1,"srtext");
	m_pGrid_SRID->SetColLabelValue(2,"proj4text");
	m_pGrid_SRID->SetColSize(0,50);
	m_pGrid_SRID->SetColSize(1,400);
	m_pGrid_SRID->SetColSize(2,150);

	m_pGrid_SRID->SetEditable(false);
	//m_pGrid_SRID->SelectRow(445);

	//statBoxSizerGrid->Add(m_pGrid_SRID, 0, wxGROW|wxALL, 0);
	//m_pGrid_SRID->FitInside();

	if (_Assign_SRID_Grid(selection)) {}
	//-----------------------------------------------------------------
	//MainBoxSizer->Add( statBoxSizerGrid, 0, wxALIGN_CENTER|wxALL, 5 );
	MainBoxSizer->Add( m_pGrid_SRID, 1, wxGROW|wxALL, 0 );

	//-------------------------------------------
	// Button box
	//-------------------------------------------
    wxBoxSizer *ButtonBoxSizer = new wxBoxSizer( wxHORIZONTAL );

    wxButton *item20 = new wxButton( this, wxID_OK, wxT("OK"), wxDefaultPosition, wxDefaultSize, 0 );
    ButtonBoxSizer->Add( item20, 0, wxALIGN_CENTER|wxALL, 5 );

    wxButton *item21 = new wxButton( this, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    ButtonBoxSizer->Add( item21, 0, wxALIGN_CENTER|wxALL, 5 );

	MainBoxSizer->Add(ButtonBoxSizer, 0, wxALIGN_CENTER|wxALL, 5);
	
	//-------------------------------------------
	this->SetSizer( MainBoxSizer );
	MainBoxSizer->Fit(this);
    MainBoxSizer->SetSizeHints( this );
}

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

bool CSimpleFeature_export_dlg_srid::_Assign_SRID_Grid(int selection)
{
	int				nrows = 0;
	PGconn			*conn = g_pSAGA_Frame->db->GetConn();
	string			sql = "SELECT srid, srtext, proj4text from spatial_ref_sys";

	
	if (selection == 0)
		sql += " WHERE srtext LIKE 'PROJCS%'";
	if (selection == 1)
		sql += " WHERE srtext LIKE 'GEOGCS%'";

	PGresult		*res = PQexec(conn,sql.c_str());
	// check whether SQL command was successfull or not
	if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		PQclear(res);
		return(false);
	} else
	{
		nrows = PQntuples(res);
		for (int i = 0; i < nrows && PROGRESSBAR_Set_Position(i,nrows); i++)
		{
			m_pGrid_SRID->AppendRows(1, true);
			m_pGrid_SRID->SetCellValue(i,0,wxString::Format("%s",PQgetvalue(res,i,0)));
			m_pGrid_SRID->SetCellValue(i,1,wxString::Format("%s",PQgetvalue(res,i,1)));
			m_pGrid_SRID->SetCellValue(i,2,wxString::Format("%s",PQgetvalue(res,i,2)));
		}
	}
	PROCESS_Set_Okay(true);
	PQclear(res);
	return(true);
}

//---------------------------------------------------------------------
int CSimpleFeature_export_dlg_srid::Get_SRID()
{
	wxArrayInt selected_rows = m_pGrid_SRID->GetSelectedRows();
	if (selected_rows.size() > 0)
	{
		int row = selected_rows[0];
		return( atoi(m_pGrid_SRID->GetCellValue(row,0)) );
	} else{
		return(-1);
	}
}