#include "CivHydrSimulation.h"
#include "CivInpConvertor.h"
#include "CivSysLog.h"

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

}

CivHydrSimulation::~CivHydrSimulation()
{

}

bool CivHydrSimulation::hdyrSimulation()
{
    CivInpConvertor convertor(mUri);
    std::string inpFileS = convertor.convertBaseInp();
    if (inpFileS.empty())
        return false;

    mHydrCompute.cacheCodeSn(mUri);
    if (!mHydrCompute.hdyrCompute())
    {
        return false;
    }
    
    mHydrCompute.saveResult(mUri);

    return true;
}

bool CivHydrSimulation::qualitySimulation()
{
    CivSysLog::getInstance()->info("开始水质模拟", "CivSysLog", __FUNCTION__);
    CivSysLog::getInstance()->info("开始转inp文件", "CivSysLog", __FUNCTION__);


    CivInpConvertor convertor(mUri);
    // convertor.maintainTopo();

    std::string inpFileS = convertor.convertBaseInp();
    if (inpFileS.empty() || inpFileS == "")
    {
        CivSysLog::getInstance()->error("转inp文件失败", "CivSysLog", __FUNCTION__);
        return false;
    }
       
    CivSysLog::getInstance()->info("转inp文件成功", "CivSysLog", __FUNCTION__);

    CivSysLog::getInstance()->info("开始计算", "CivSysLog", __FUNCTION__);
    mHydrCompute.cacheCodeSn(mUri);
    if (!mHydrCompute.qualityCompute())
    {
        CivSysLog::getInstance()->error("计算失败", "CivSysLog", __FUNCTION__);
        return false;
    }
    
    CivSysLog::getInstance()->info("计算成功", "CivSysLog", __FUNCTION__);

    // 余氯计算
    inpFileS = convertor.convertResidualInp();
    if (inpFileS.empty() || inpFileS == "")
    {
        CivSysLog::getInstance()->error("转inp文件失败", "CivSysLog", __FUNCTION__);
        return false;
    }

    if (!mHydrCompute.chlorineCompute())
    {
        CivSysLog::getInstance()->error("计算失败", "CivSysLog", __FUNCTION__);
        return false;
    }
    mHydrCompute.saveResult(mUri);

    return true;
}

bool  CivHydrSimulation::trackingSimulation(char* snNode, int hours,std::string& jsonResult)
{ 
    CivInpConvertor convertor(mUri);
   
    std::string inpFileS = convertor.convertTrackInp(snNode, std::to_string(hours));
    if (inpFileS.empty())
        return false;

    if (!mHydrCompute.qualityCompute())
    {
        return false;
    }

    mHydrCompute.getTrackingResult(mUri,jsonResult);

    return true;
}