#include "CivInpConvertor.h"
#include "CivProjInpBuilder.h"
#include "CivInpDirector.h"
#include "CivBaseInpBuilder.h"
#include "CivCommonUtils.h"
#include "CivHydrTableHelper.h"

CivInpConvertor::CivInpConvertor(const std::string& uri)
	:mUri(uri)
{

}

CivInpConvertor::~CivInpConvertor()
{

}

bool CivInpConvertor::maintainTopo()
{
	CivHydrTableHelper helper(mUri);
	if (!helper.mainTain())
	{
		return false;
	}
	return true;
}

std::string CivInpConvertor::convertPorjInp(const std::string& projCode, const std::string& start)
{

	CivProjInpBuilder* builder = new CivProjInpBuilder(mUri);
	builder->setProjCode(projCode);
	builder->setTimes("Duration", "0");
	builder->setTimes("Start Clocktime", start);
	builder->isDelayTimeSmuli(false);
	builder->setPatternTime(start);

	CivInpDirector director;
	director.setBuilder(builder);
	director.create();

	std::string inpFile = CivCommonUtils::getExePath() + "\\ptest.inp";
	if (!builder->getNewInp(inpFile))
	{
		delete builder;
		return "";
	}
	
	delete builder;
	return inpFile;
}

std::string CivInpConvertor::convertBaseInp()
{
	CivInpBuilder* builder = new CivBaseInpBuilder(mUri);
	CivInpDirector director;
	director.setBuilder(builder);
	director.create();

	std::string inpFile = CivCommonUtils::getExePath() + "\\test.inp";
	if (!builder->getNewInp(inpFile))
	{
		delete builder;
		return "";
	}

	delete builder;
	return inpFile;
}

std::string CivInpConvertor::convertTrackInp(const std::string& sn, const std::string& hours)
{
	CivHydrTableHelper helper(mUri);

	std::string codeTosn;
	helper.getCodeToSn(sn, codeTosn);

	CivInpBuilder* builder = new CivBaseInpBuilder(mUri);
	builder->setOption("Quality", "TRACE "+ codeTosn);
	builder->setTimes("Duration", hours);
	builder->resetAllToZero();
	builder->setQuality(sn, "1");

	CivInpDirector director;
	director.setBuilder(builder);
	director.create();

	std::string inpFile = CivCommonUtils::getExePath() + "\\test.inp";
	if (!builder->getNewInp(inpFile))
	{
		delete builder;
		return "";
	}

	delete builder;
	return inpFile;
}

std::string CivInpConvertor::convertAnalysisInp()
{
	CivInpBuilder* builder = new CivBaseInpBuilder(mUri);
	builder->setTimes("Duration", "1");

	CivInpDirector director;
	director.setBuilder(builder);
	director.create();

	std::string inpFile = CivCommonUtils::getExePath() + "\\test.inp";
	if (!builder->getNewInp(inpFile))
	{
		delete builder;
		return "";
	}

	delete builder;
	return inpFile;
}

std::string CivInpConvertor::convertResidualInp()
{
	CivInpBuilder* builder = new CivBaseInpBuilder(mUri);
	builder->setQualityType(CivInpHelperAbs::Chlorine);
	builder->setOption("Quality", "Chlorine mg/L");


	CivInpDirector director;
	director.setBuilder(builder);
	director.create();

	std::string inpFile = CivCommonUtils::getExePath() + "\\test.inp";
	if (!builder->getNewInp(inpFile))
	{
		delete builder;
		return "";
	}

	delete builder;
	return inpFile;
}

std::string CivInpConvertor::convertNotPorjInp(const std::string& simulTime)
{
	CivInpBuilder* builder = new CivBaseInpBuilder(mUri);
	builder->setTimes("Duration", "0");
	builder->setTimes("Start Clocktime", simulTime);
	builder->isDelayTimeSmuli(false);
	builder->setPatternTime(simulTime);

	CivInpDirector director;
	
	director.setBuilder(builder);
	director.create();

	std::string inpFile = CivCommonUtils::getExePath() + "\\test.inp";
	if (!builder->getNewInp(inpFile))
	{
		delete builder;
		return "";
	}

	delete builder;
	return inpFile;
}