#include "CivBuilder.h"
#include "CivTypes.h"
#include "CivAssembly.h"

#define ASSERT(A,B)  if ((A) == nullptr || (B) == nullptr) return;

CivBuilder::CivBuilder()
{
	mNewInp = nullptr;
}

CivBuilder::~CivBuilder()
{

}

CivNewInp* CivBuilder::getNewInp()
{
    return mNewInp;
}


CivInpBuilder::CivInpBuilder()
	:mDbConn(nullptr)
{
    mNewInp = new CivNewInp();
}

CivInpBuilder::CivInpBuilder(CivDbConn* dbConn)
	:mDbConn(dbConn)
{
    mNewInp = new CivNewInp();
}

CivInpBuilder::~CivInpBuilder()
{
	delete mDbConn;
}

void CivInpBuilder::registDb(CivDbConn* dbConn)
{
	if (mDbConn != nullptr)
	{
		delete mDbConn;
		mDbConn = nullptr;
	}
	mDbConn = dbConn;
}

void CivInpBuilder::buildNode()
{
	ASSERT(mDbConn, mNewInp)

	CivNode node;
	bool succ = mDbConn->getNode(node);
	if (succ)
		return;

	mNewInp->setNode(node);
}

void CivInpBuilder::buildPipe()
{
	ASSERT(mDbConn, mNewInp)

	CivPipe pipe;
	bool succ = mDbConn->getPipe(pipe);
	if (succ)
		return;

	mNewInp->setPipes(pipe);
}

void CivInpBuilder::buildTank()
{
	ASSERT(mDbConn, mNewInp)

	CivTank tank;
	bool succ = mDbConn->getTank(tank);
	if (succ)
		return;

	mNewInp->setTank(tank);
}

void CivInpBuilder::buildVavle()
{
	ASSERT(mDbConn, mNewInp)

	CivValve valve;
	bool succ = mDbConn->getValve(valve);
	if (succ)
		return;

	mNewInp->setValves(valve);
}

void CivInpBuilder::buildPump()
{
	ASSERT(mDbConn, mNewInp)

	CivPumps valve;
	bool succ = mDbConn->getPumps(valve);
	if (succ)
		return;

	mNewInp->setPumps(valve);
}

void CivInpBuilder::buildReservoir()
{
	ASSERT(mDbConn, mNewInp)

	CivReservoirs res;
	bool succ = mDbConn->getReservoirs(res);
	if (succ)
		return;

	mNewInp->setReservoirs(res);
}

void CivInpBuilder::buildEmitter()
{
	ASSERT(mDbConn, mNewInp)

	CivEmitters emmiters;
	bool succ = mDbConn->getEmitters(emmiters);
	if (succ)
		return;

	mNewInp->setEmitters(emmiters);
}

CivNewInp* CivInpBuilder::getNewInp()
{
	return mNewInp;
}

void CivInpBuilder::buildPatterns()
{
	ASSERT(mDbConn, mNewInp)

	CivPatterns pattern;
	if (!mDbConn->getPatterns(pattern))
		return;

	mNewInp->setPatterns(pattern);
}

void CivInpBuilder::buildParamter()
{
	ASSERT(mDbConn, mNewInp)

	std::vector<CivParameter> parameters;
	if (!mDbConn->getParameter(parameters))
		return;

	size_t total = parameters.size();
	for (int i = 0; i < total; i++)
	{
		CivParameter param = parameters[i];
		mNewInp->setParamter(param, param.mType);
	}	
}

void CivInpBuilder::buildCoordnates()
{
	ASSERT(mDbConn, mNewInp)

	CivCoordinates coords;
	bool succ = mDbConn->getCoordinates(coords);
	if (succ)
		return;

	mNewInp->setCoordnates(coords);
}

void CivInpBuilder::buildVertices()
{
	
}

void CivInpBuilder::buildLabels()
{
	ASSERT(mDbConn, mNewInp)

	CivLabels labels;
	bool succ = mDbConn->getLabels(labels);
	if (succ)
		return;

	mNewInp->setLabels(labels);
}

void CivInpBuilder::buildTags()
{
	ASSERT(mDbConn, mNewInp)

	CivTags tag;
	bool succ = mDbConn->getTags(tag);
	if (succ)
		return;

	mNewInp->setTags(tag);
}

void CivInpBuilder::buildBackDrop()
{

}

void CivInpBuilder::buildQuality()
{
	ASSERT(mDbConn, mNewInp)

	CivQuality quality;
	bool succ = mDbConn->getQuality(quality);
	if (succ)
		return;

	mNewInp->setQuality(quality);
}

void CivInpBuilder::buildSources()
{
	ASSERT(mDbConn, mNewInp)

	CivSources sources;
	bool succ = mDbConn->getSources(sources);
	if (succ)
		return;

	mNewInp->setSources(sources);
}

void CivInpBuilder::buildMixing()
{
	ASSERT(mDbConn, mNewInp)

	CivMixing mixing;
	bool succ = mDbConn->getMixing(mixing);
	if (succ)
		return;

	mNewInp->setMixing(mixing);
}

void CivInpBuilder::buildCurves()
{
	ASSERT(mDbConn, mNewInp)

	CivCurves curves;
	bool succ = mDbConn->getCurves(curves);
	if (succ)
		return;

	mNewInp->setCurves(curves);
}

void CivInpBuilder::buildStatus()
{
	ASSERT(mDbConn, mNewInp)

	CivStatus status;
	bool succ = mDbConn->getStatus(status);
	if (succ)
		return;

	mNewInp->setStatus(status);
}

void CivInpBuilder::buildControls()
{

}

void CivInpBuilder::buildRules()
{
	
}

void CivInpBuilder::buildDemands()
{
	ASSERT(mDbConn, mNewInp)

	CivDemands mand;
	bool succ = mDbConn->getDemands(mand);
	if (succ)
		return;

	mNewInp->setDemands(mand);
}