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

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

CivBuilder::CivBuilder()
{
}

CivBuilder::~CivBuilder()
{
	delete mNewInp;
}

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


CivInpBuilder::CivInpBuilder()
	:mDbConn(nullptr)
{
    mNewInp = new CivNewInp();
	mNewInp->setTtitle("");
}

CivInpBuilder::CivInpBuilder(CivDbConn* dbConn)
	:mDbConn(dbConn)
{
    mNewInp = new CivNewInp();
	mNewInp->setTtitle("");
}


CivInpBuilder::~CivInpBuilder()
{
}

void CivInpBuilder::setQualityType(const QualityAnalyType analyType, const std::string& qulityName)
{
	mNewInp->setQualityType(analyType, qulityName);
}

void CivInpBuilder::setDuration(int duration)
{
	mNewInp->setDuration(duration);
}

void CivInpBuilder::registDb(CivDbConn* dbConn)
{
	mDbConn = dbConn;
}

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

	CivNode node;
	if (!mDbConn->getNode(node))
		return;

	mNewInp->setNode(node);
}

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

	CivPipe pipe;
	if (!mDbConn->getPipe(pipe))
		return;

	mNewInp->setPipes(pipe);
}

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

	CivTank tank;
	if (!mDbConn->getTank(tank))
		return;

	mNewInp->setTank(tank);
}

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

	CivValve valve;
	if (!mDbConn->getValve(valve))
		return;

	mNewInp->setValves(valve);
}

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

	CivPumps valve;
	if (!mDbConn->getPumps(valve))
		return;

	mNewInp->setPumps(valve);
}

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

	CivReservoirs res;
	if (!mDbConn->getReservoirs(res))
		return;

	mNewInp->setReservoirs(res);
}

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

	CivEmitters emmiters;
	if (!mDbConn->getEmitters(emmiters))
		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;
	if (!mDbConn->getCoordinates(coords))
		return;

	mNewInp->setCoordnates(coords);
}

void CivInpBuilder::buildVertices()
{
	
}

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

	CivLabels labels;
	if (!mDbConn->getLabels(labels))
		return;

	mNewInp->setLabels(labels);
}

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

	CivTags tag;
	if (!mDbConn->getTags(tag))
		return;

	mNewInp->setTags(tag);
}

void CivInpBuilder::buildBackDrop()
{

}

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


	CivQuality quality;
	if (!mDbConn->getQuality(quality))
		return;

	mNewInp->setQuality(quality);
}

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

	CivSources sources;
	if (!mDbConn->getSources(sources))
		return;

	mNewInp->setSources(sources);
}

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

	CivMixing mixing;
	if (!mDbConn->getMixing(mixing))
		return;

	mNewInp->setMixing(mixing);
}

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

	/*CivCurves curves;
	if (!mDbConn->getCurves(curves))
		return;

	mNewInp->setCurves(curves);*/
}

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

	CivStatus status;
	if (!mDbConn->getStatus(status))
		return;

	mNewInp->setStatus(status);
}

void CivInpBuilder::buildControls()
{

}

void CivInpBuilder::buildRules()
{
	
}

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

	CivDemands mand;
	if (!mDbConn->getDemands(mand))
		return;

	mNewInp->setDemands(mand);
}