#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()
{

}

std::string CivInpConvertor::convertPorjInp(
	const std::string& projCode,
	const std::string& start)
{
	CivHydrTableHelper helper(mUri);
	if (!helper.mainTain())
	{
		return "";
	}
		
	CivProjInpBuilder* builder = new CivProjInpBuilder(mUri);
	builder->setProjCode(projCode);
	builder->setTimes("Duration", "0");
	builder->setTimes("Start Clocktime", start);

	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::convertBaseInp()
{
	CivHydrTableHelper helper(mUri);
	if (!helper.mainTain())
	{
		return "";
	}

	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);
	if (!helper.mainTain())
	{
		return "";
	}
	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()
{
	CivHydrTableHelper helper(mUri);
	if (!helper.mainTain())
	{
		return "";
	}

	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;
}