#include "menu_db.h"
#include "convert_sl.h"

#include "saga_gui/saga_frame.h"
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#define MSG_ADD(s)	MSG_General_Add(s, true, true);

#include <fstream>

bool Cmenu_db::create_shape_fromSF(CSG_Shapes *pShapes)
{
	CSG_Table_Record	*pRecord;

	// get SHAPE_TYPE
	switch (m_pSimpleFeature->GetGeoType())
	{
		default: 
			return(false);
		case 1: pShapes->Create(SHAPE_TYPE_Points,	m_pSimpleFeature->GetFeatureName().c_str()); break;
		case 2: pShapes->Create(SHAPE_TYPE_Line,	m_pSimpleFeature->GetFeatureName().c_str()); break;
		case 3: pShapes->Create(SHAPE_TYPE_Polygon,	m_pSimpleFeature->GetFeatureName().c_str()); break;
		case 4: pShapes->Create(SHAPE_TYPE_Points,	m_pSimpleFeature->GetFeatureName().c_str()); break;
		case 5: pShapes->Create(SHAPE_TYPE_Line,	m_pSimpleFeature->GetFeatureName().c_str()); break;
		case 6: pShapes->Create(SHAPE_TYPE_Polygon,	m_pSimpleFeature->GetFeatureName().c_str()); break;
		//case 7: // SF_GeometryCollection not yet implemented
	} // end switch

	// Assign coordinates
	for (unsigned int i = 0; i < m_pSimpleFeature->GetNumFeatures(); i++)
	{
		pShapes->Add_Shape();
		for (unsigned int z = 0; z < m_pSimpleFeature->GetNumPointsFeature(i); z++)
			pShapes->Get_Shape(i)->Add_Point(m_pSimpleFeature->Get_X_Value(i,z),
											 m_pSimpleFeature->Get_Y_Value(i,z),i);
	}

	std::ofstream file("_sf.txt");
	for (int y = 0; y < m_pSimpleFeature->Get_nrows(); y++)
	{
		for (int x = 0; x < m_pSimpleFeature->Get_ncols(); x++)
		{
			//if (x != m_pSimpleFeature->GetGeometryColumnNumber())
				file << y <<", " << x << ", " << m_pSimpleFeature->GetFieldName(x) << ", " <<  m_pSimpleFeature->GetValue(x,y) << endl;
		}
		
	}

	// assign fieldtypes to Shapefile attribute table
	for (int x = 0; x < m_pSimpleFeature->Get_ncols()-1; x++)
	{
		//if (x != m_pSimpleFeature->GetGeometryColumnNumber())
		pShapes->Get_Table().Add_Field(m_pSimpleFeature->GetFieldName(x).c_str(),
										g_pSAGA_Frame->db->SQLFieldTypeToSAGAFieldtype(m_pSimpleFeature->GetFieldTypeInt(x))
										,x);
	}

	// assign data to attribute table
	for (int y = 0; y < m_pSimpleFeature->Get_nrows(); y++)
	{
		pShapes->Get_Table().Add_Record();
		pRecord = pShapes->Get_Table().Get_Record(y);
		for (int x = 0; x < m_pSimpleFeature->Get_ncols()-1; x++)
		{
			//if (x != m_pSimpleFeature->GetGeometryColumnNumber())
			{
				if (g_pSAGA_Frame->db->SQLFieldTypeToSAGAFieldtype(m_pSimpleFeature->GetFieldTypeInt(x))
					== TABLE_FIELDTYPE_Int)
				{
					file << "1: " << convert_sl::StringToInt(m_pSimpleFeature->GetValue(x,y)) << endl;
					pRecord->Set_Value(x, convert_sl::StringToInt(m_pSimpleFeature->GetValue(x,y)));
				}
				
				if (g_pSAGA_Frame->db->SQLFieldTypeToSAGAFieldtype(m_pSimpleFeature->GetFieldTypeInt(x))
					== TABLE_FIELDTYPE_Double)
				{
					pRecord->Set_Value(x, convert_sl::StringToDouble(m_pSimpleFeature->GetValue(x,y)));
					file << "2: " << convert_sl::StringToDouble(m_pSimpleFeature->GetValue(x,y)) << endl;
				}
				
				if (g_pSAGA_Frame->db->SQLFieldTypeToSAGAFieldtype(m_pSimpleFeature->GetFieldTypeInt(x))
					== TABLE_FIELDTYPE_String)
				{
					pRecord->Set_Value(x,m_pSimpleFeature->GetValue(x,y).c_str());
					file << "3: " << m_pSimpleFeature->GetValue(x,y).c_str() << endl;
				}
			}
		}
	}

	return(true);
}

