#include "CivTrackingAnalysis.h"
#include "CivSimulResultCache.h"
#include "CivHydrSimulation.h"
#include "CivGraphFactory.h"
#include "CivInpConvertor.h"
#include "CivInpDbHelper.h"

CivTrackingAnalysis::CivTrackingAnalysis(const std::string& uri)
	:mGraph(new ALGraph<CivGraphJunction, CivGraphEdage>()),mUri(uri)
{

}

CivTrackingAnalysis::~CivTrackingAnalysis()
{
	delete mGraph;
}

bool CivTrackingAnalysis::createGraphFrom()
{
	CivInpConvertor convertor(mUri);
	std::string inpFile = convertor.convertTrackInp("", "0");
	if (inpFile.empty())
		return false;

	CivHydrCompute dyCompute;
	if (!dyCompute.hdyrCompute())
		return false;

	NodeResultItems nodeItems;
	LinkResultItems linkItems;
	dyCompute.getNodeItemByInterval(0, nodeItems);
	dyCompute.getPipeItemByInterval(0, linkItems);

	if (nodeItems.size() <= 0)
		return false;

	// 顶点坐标
	CivCoordinates coords;
	CivInpDbHelper helper(mUri);
	helper.getCoordinates(coords);

	std::list<CivCoordinates::CoordTable> coordTableLis = coords.mTables;
	std::list<CivCoordinates::CoordTable>::iterator iter;
	for (iter = coordTableLis.begin(); iter != coordTableLis.end(); iter++)
	{
		CivCoordinates::CoordTable coordTable = *iter;

		// 插入顶点数据
		CivGraphJunction graphJunction(coordTable.ID, coordTable.XCoord, coordTable.YCoord);
		auto iter = nodeItems.find(coordTable.ID);
		if (iter != nodeItems.end())
		{
			graphJunction.setHead(iter->second.dHead);
		}
		mGraph->insertAVertex(graphJunction);
	}

	// 管段
	CivPipe civPipe;
	helper.getPipe(civPipe);

	std::list<CivPipe::PipesTable> pipesTableLis = civPipe.mTables;
	std::list<CivPipe::PipesTable>::iterator pIter;

	for (pIter = pipesTableLis.begin(); pIter != pipesTableLis.end(); pIter++)
	{
		CivPipe::PipesTable pipeTable = *pIter;

		// 获取管段起点坐标
		CivGraphJunction graphJunction1(pipeTable.Node1);
		int index1 = mGraph->getVertexIndex(graphJunction1);
		CivGraphJunction sStartjuction = mGraph->getData(index1);

		// 获取管段终点坐标值
		CivGraphJunction graphJunction2(pipeTable.Node2);
		int index2 = mGraph->getVertexIndex(graphJunction2);
		CivGraphJunction endJunction = mGraph->getData(index2);


		if (sStartjuction.getHead() > endJunction.getHead())
		{
			CivGraphEdage edAge(pipeTable.ID, pipeTable.Length, { sStartjuction.getXCoord(),sStartjuction.getYCoord(),endJunction.getXCoord(),endJunction.getYCoord() });
			mGraph->insertAEdge(sStartjuction, endJunction, edAge);
		}
		else if (sStartjuction.getHead() == endJunction.getHead())
		{
			CivGraphEdage edAge1(pipeTable.ID, pipeTable.Length, { sStartjuction.getXCoord(),sStartjuction.getYCoord(),endJunction.getXCoord(),endJunction.getYCoord() });
			mGraph->insertAEdge(sStartjuction, endJunction, edAge1);

			CivGraphEdage edAge2(pipeTable.ID, pipeTable.Length, { endJunction.getXCoord(),endJunction.getYCoord(), sStartjuction.getXCoord(),sStartjuction.getYCoord() });
			mGraph->insertAEdge(endJunction, sStartjuction, edAge2);

		}
		else
		{
			CivGraphEdage edAge(pipeTable.ID, pipeTable.Length, { endJunction.getXCoord(),endJunction.getYCoord(), sStartjuction.getXCoord(),sStartjuction.getYCoord() });
			mGraph->insertAEdge(endJunction, sStartjuction, edAge);
		}

	}
	return true;
}

bool CivTrackingAnalysis::transformJson(const std::vector<CivGraphJunction>& junctions,
	const std::vector<CivGraphEdage>& pipes, std::string& jsonResult)
{

	jsonResult.append("[");
	// 管段
	size_t pipesTotal = pipes.size();
	for (int i = 0; i < pipesTotal; i++)
	{
		CivGraphEdage edge = pipes[i];

		if (edge.getSN().empty())
			continue;

		jsonResult.append("[[");
		jsonResult.append(edge.getStartX());
		jsonResult.append(",");
		jsonResult.append(edge.getStartY());
		jsonResult.append("],[");
		jsonResult.append(edge.getEndX());
		jsonResult.append(",");
		jsonResult.append(edge.getEndY());
		jsonResult.append("]],");
	}
	jsonResult = jsonResult.substr(0, jsonResult.length() - 1);
	jsonResult.append("]");

	return true;
}

bool CivTrackingAnalysis::upstreamTracking(const std::string& sN, std::string& jsonResult)
{	
	std::vector<CivGraphJunction> junctions;
	std::vector<CivGraphEdage> edges;
	
	if (!upstreamTracking(sN, junctions, edges))
	{
		return false;
	}

	// 将查询到的信息拼接成字符串
	if (!transformJson(junctions, edges, jsonResult))
	{
		return false;
	}

	return true;
}

bool CivTrackingAnalysis::downstreamTracking(const std::string& sN, std::string& jsonResult)
{
	std::vector<CivGraphJunction> junctions;
	std::vector<CivGraphEdage> edges;

	if (!downstreamTracking(sN, junctions, edges))
	{
		return false;
	}

	// 将查询到的信息拼接成字符串
	if (!transformJson(junctions, edges,jsonResult))
	{
		return false;
	}
	return true;
}

bool CivTrackingAnalysis::waterSupplyScopeAnalysis(const std::string& sN, std::string& jsonResult)
{

	return true;
}

bool CivTrackingAnalysis::upstreamTracking(const std::string& sN,
	std::vector<CivGraphJunction>& junctions,
	std::vector<CivGraphEdage>& pipes)
{
	// 上溯，图反向
	ALGraph<CivGraphJunction, CivGraphEdage>* graph = mGraph->reverseGraph();
	
	CivGraphJunction junction(sN);
	graph->getVisitedResult(junction, junctions, pipes);

	delete graph;
	return true;
}

bool CivTrackingAnalysis::downstreamTracking(const std::string& sN,
	std::vector<CivGraphJunction>& junctions,
	std::vector<CivGraphEdage>& pipes)
{

	CivGraphJunction junction(sN);
	mGraph->getVisitedResult(junction, junctions, pipes);

	return true;
}

bool CivTrackingAnalysis::waterSupplyScopeAnalysis(const std::string& sN,
	std::vector<CivGraphJunction>& junctions,
	std::vector<CivGraphEdage>& pipes)
{

	return true;
}