#include "CivInp.h"
#include<time.h>


bool CivInp::writeToFile(const char* fileName)
{

	FILE* file;
	std::string ss;
	if (file = fopen(fileName, "wb"))
	{
		fprintf(file, mTextStream.str().c_str());
		return true;
	}
	return false;
}

std::string CivInp::currentTime()
{
	time_t timep;
	struct tm* p;
	time(&timep);
	p = gmtime(&timep);
	std::string currTime = std::to_string(1900 + p->tm_year) + "/" + std::to_string(1 + p->tm_mon) + \
		"/" + std::to_string(p->tm_mday) + "  " + std::to_string(p->tm_hour) + ":" + std::to_string(p->tm_min) + ":" + \
		std::to_string(p->tm_sec);

	return currTime;
}

void CivInp::writeTitles()
{
	writeString("[TITLE]");  // ÏÈÐ´Èë±êÇ©
	writeString("Scenario: Base");
	writeString("Date:"+currentTime());
	writeString("");
	writeString("");
}

void CivInp::writeJunctions(Civitems items)
{
	writeString("[JUNCTIONS]");
	writeString(";ID              	Elev        	Demand      	Pattern    ");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeItems(const Civitems& items)
{
	size_t rows = items.size();
	for (int i = 0; i < rows; i++)
	{
		CivItem item = items[i];
		size_t itemSize = item.size();
		int index = 0;
		std::string itemStr = item[0] + "  ";
		while ((++index) < itemSize)
			itemStr = item[index] + "  ";

		writeString(itemStr);
	}
}

void CivInp::writePipes(Civitems items)
{
	writeString("[PIPES]");
	writeString(";ID  Node1   Node2   Length   Diameter  	Roughness  MinorLoss  Status");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeTanks(Civitems items)
{
	writeString("[TANKS]");
	writeString(";ID  Elevation  InitLevel   	MinLevel    MaxLevel   Diameter    MinVol   VolCurve");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeReservoirs(Civitems items)
{
	writeString("[RESERVOIRS]");
	writeString(";ID  Node1   Node2   Length   Diameter  	Roughness  MinorLoss  Status");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writePumps(Civitems items)
{
	writeString("[PUMPS]");
	writeString(";ID  Node1   Node2   Length   Diameter  	Roughness  MinorLoss  Status");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeValves(Civitems items)
{
	writeString("[VALVES]");
	writeString(";ID              	Node1           	Node2           	Diameter    	Type	Setting     	MinorLoss   ");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeCoordinates(Civitems items)
{
	writeString("[PIPES]");
	writeString(";ID  Node1   Node2   Length   Diameter  	Roughness  MinorLoss  Status");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeLabels(Civitems items)
{
	writeString("[PIPES]");
	writeString(";ID  Node1   Node2   Length   Diameter  	Roughness  MinorLoss  Status");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeVertices(Civitems items)
{
	writeString("[PIPES]");
	writeString(";ID  Node1   Node2   Length   Diameter  	Roughness  MinorLoss  Status");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeDemand(Civitems items)
{
	writeString("[DEMANDS]");
	writeString(";Junction        	Demand      	Pattern         	Category");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeOptions(const Civitems& items)
{
	writeString("[OPTIONS]");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writePatterns(Civitems items)
{
	writeString("[PATTERNS]");
	writeString(";ID              	Multipliers");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeBackdrop(Civitems items)
{
	writeString("[BACKDROP]");
	writeItems(items);

	writeString("");
	writeString("");
}

void CivInp::writeEnd()
{
	writeString("[END]");
}

void CivInp::writeString(const std::string& s)
{
	mTextStream << s << "\n";
}