#include "CivInpHelperAbs.h"
#include "CivConnection.h"
#include "CivPgDbConnection.h"
#include "CivTableFields.h"

CivInpHelperAbs::CivInpHelperAbs(const std::string& uri)
{
	mDbConn = new CivPgDbConnection();
	mDbConn->connect(uri);
}

CivInpHelperAbs::~CivInpHelperAbs()
{
	mDbConn->disconnect();
	delete mDbConn;
}

bool CivInpHelperAbs::getEmitters(CivEmitters& emitter)
{
	return true;
}

bool CivInpHelperAbs::getMixing(CivMixing& mixing)
{
	return true;
}

bool CivInpHelperAbs::getParameter(std::vector<CivParameter>& params)
{
	ParametorTable restemTable;

	std::vector<std::string> fields = {
		 restemTable.name,
		 restemTable.val,
		 restemTable.type
	};

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(OPTIONTTABLE, fields, resultVector);

	std::map<std::string, std::vector<CivParameter::ParamTable>> tempMap;
	for (int i = 0; i < resultVector.size(); i++)
	{
		CivParameter::ParamTable pTable;
		std::map<std::string, std::string> map = resultVector[i];
		pTable.name = map.find(fields[0])->second;
		pTable.val = map.find(fields[1])->second;

		std::string type = map.find(fields[2])->second;
		tempMap[type].push_back(pTable);
	}

	// 遍历临时存储的数据拼接参数对象
	for (auto iter = tempMap.begin(); iter != tempMap.end(); iter++)
	{
		CivParameter param;
		std::string type = iter->first;
		std::vector<CivParameter::ParamTable> paramTables = iter->second;

		param.setType(type);

		size_t total = paramTables.size();
		for (int i = 0; i < total; i++)
			param.addItem(paramTables[i]);

		params.push_back(param);
	}

	return true;
}

bool CivInpHelperAbs::getPatterns(CivPatterns& patterns)
{
	PatternTable patternTable;

	std::vector<std::string> fields = {
		patternTable.modelName,
		patternTable.modelData,
		patternTable.modelType
	};

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(PATTERNTABLE, fields, resultVector);

	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		std::map<std::string, std::string> map = resultVector[i];
		CivPatterns::PatternTable pTable;

		pTable.name = map.find(fields[0])->second;
		pTable.val = map.find(fields[1])->second;

		patterns.addItem(pTable);
	}

	return true;
}

bool CivInpHelperAbs::getCurves(CivCurves& curves)
{
	CurveTable curveTable;

	std::vector<std::string> fields = {
		curveTable.CurveName,
		curveTable.CurveType,
		curveTable.CurveData
	};

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(CURVETABLE, fields, resultVector);

	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		CivCurves::CurvesTable cTable;
		std::map<std::string, std::string> map = resultVector[i];

		cTable.ID = map.find(fields[0])->second;
		cTable.XCoord = map.find(fields[1])->second;
		cTable.YCoord = map.find(fields[2])->second;

		curves.addItem(cTable);
	}

	return true;
}

bool CivInpHelperAbs::getDemands(CivDemands& demands)
{
	return true;
}

bool CivInpHelperAbs::getSources(CivSources& sources)
{
	return true;
}

bool CivInpHelperAbs::getStatus(CivStatus& status)
{
	return true;
}

bool CivInpHelperAbs::getLabels(CivLabels& labels)
{
	return true;
}

bool CivInpHelperAbs::getTags(CivTags& tags)
{
	return true;
}