#include "CivInpBuilder.h"
#include "CivTypes.h"
#include "CivAssembly.h"

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

CivInpBuilder::CivInpBuilder(const std::string & uri)
	:mNewInp(new CivNewInp()), mUri(uri)
{
	mOptions.mType = "OPTIONS";
	mTimes.mType = "TIMES";
	mReactions.mType = "REACTIONS";
	mReport.mType = "REPORT";
}

CivInpBuilder::~CivInpBuilder()
{
	delete mNewInp;
	delete mHelper;
}

void CivInpBuilder::setOption(const std::string& name,
	const std::string& val)
{
	CivParameter::ParamTable table = { name,val };
	mOptions.addItem(table);
}

void CivInpBuilder::setTimes(const std::string& name,
	const std::string& val)
{
	CivParameter::ParamTable table = { name,val };
	mTimes.addItem(table);
}

void CivInpBuilder::setReport(const std::string& name,
	const std::string& val)
{
	CivParameter::ParamTable table = { name,val };
	mReport.addItem(table);
}

void CivInpBuilder::setReaction(const std::string& name,
	const std::string& val)
{
	CivParameter::ParamTable table = { name,val };
	mReactions.addItem(table);
}

bool CivInpBuilder::getNewInp(std::string& infile)
{
	if (!mNewInp->writeToFile(infile))
		return false;

    return true;
}

void CivInpBuilder::buildParamter()
{
	ASSERT(mHelper, mNewInp);

	std::vector<CivParameter> parameters;
	if (!mHelper->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::resetAllToZero()
{
	mResetToZero = true;
}

void CivInpBuilder::setQuality(const std::string& name, const std::string& val)
{
	mInitQuality.addItem({ name ,val });
}