#include "CivSimulResultCache.h"

void CivSimulResultCache::addNodeQuality(float quality, int interval, Str sNo)
{
	if (mNodeItemsMap.find(interval) == mNodeItemsMap.end())
		return;

	auto nodeMap = mNodeItemsMap.find(interval)->second;
	if (nodeMap.find(sNo) == nodeMap.end())
		return;

	mNodeItemsMap[interval][sNo].dQuality = quality;
}

void CivSimulResultCache::addLinkQuality(float quality, int interval, Str sNo)
{
	if (mLinkItemsMap.find(interval) == mLinkItemsMap.end())
		return;

	auto linkMap = mLinkItemsMap.find(interval)->second;
	if (linkMap.find(sNo) == linkMap.end())
		return;

	mLinkItemsMap[interval][sNo].dQuality = quality;
}

void CivSimulResultCache::addNodeItems(const NodeResultItem& nodeItem)
{
	mNodeItemsMap[nodeItem.dInterval][nodeItem.szNo] = nodeItem;
}

void CivSimulResultCache::addLinkItems(const LinkResultItem& linkItem)
{
	mLinkItemsMap[linkItem.dInterval][linkItem.szNo] = linkItem;
}

bool CivSimulResultCache::updateToDb(CivDbConnection* dbConn)
{
	if (dbConn == nullptr)
		return false;

	for (auto iter = mNodeItemsMap.begin(); iter != mNodeItemsMap.end(); iter++)
	{
		NodeResultItems nodeItems = iter->second;
		dbConn->updateNode(nodeItems);
	}

	for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
	{
		LinkResultItems nodeItems = iter->second;
		dbConn->updateLink(nodeItems);
	}
	clear();
	return true;
}

void CivSimulResultCache::clear()
{
	mLinkItemsMap.clear();
	mNodeItemsMap.clear();
}

