//Make Online Chart
//Copyright (c) 2014 State of Vermont Legislative Joint Fiscal Office.
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//                  
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License. 
//
#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Text::RegularExpressions;

void processDataFile();


int main(array<System::String ^> ^args)
{
	processDataFile();

    return 0;
}


void processDataFile() {

	System::String^ JSONdata = "";

	//Input file for data processing
	System::String^ inFilePath = "C:\\YourDirectory\\YourSpreadsheet.csv";
	StreamReader^ fileIn = gcnew StreamReader(inFilePath);

	//File is read line by line, this holds the current line
	System::String^ thisLine = gcnew String("");

	//regular expression for matching fields in a CSV file
	Regex^ matchReg = gcnew Regex("\"([^\"]*)\"|(?<=,|^)([^,]*)(?=,|$)");
	
	MatchCollection^ lineMatches;

	JSONdata += "[";

	int numMatches = 0;

	bool firstLine = true;

	
	//Output file for writing JSON data
	//System::String^ outFilePath = "C:\\Custom Apps\\MakeAppropsChart\\Approps_Chart_JSON.txt";
	System::String^ outFilePath = "C:\\YourDirectory\\Output_JSON.txt";
	StreamWriter^ fileOut = gcnew StreamWriter(outFilePath);

	bool lastLine = false;

	System::String^ lineProcessed = gcnew String("");

	Regex^ matchString = gcnew Regex("[a-z]|[A-Z]");

	//Loop through every line in the file
	do {
		
		thisLine = fileIn->ReadLine();
		
		lineMatches = matchReg->Matches(thisLine);
		if(firstLine) {
			JSONdata += "[";
			firstLine = false;
		}
		else {
			JSONdata += ", [";
		}
		numMatches = lineMatches->Count;
		for(int i = 0; i < numMatches; i++) {
			//Place any text formatting commands here - to strip out dollar signs, or other symbols, like parentheses for negative amounts
			lineProcessed = lineMatches[i]->ToString()->Replace("$", "")->Replace(".00", "")->Replace("(", "-")->Replace(")", "");

			//If string values aren't padded with double quotes, add the double quotes
			if(matchString->IsMatch(lineProcessed)) {
				if(lineProcessed->Substring(0, 1) != "\"") {
					lineProcessed = "\"" + lineProcessed + "\"";
				}
			}

			if(i == numMatches - 1) {
				//JSONdata += "\"" + lineProcessed + "\"";
				JSONdata += lineProcessed;
			}
			else {
				//JSONdata += "\"" + lineProcessed + "\"";
				JSONdata += lineProcessed;
				JSONdata += ", ";
			}
		}

		JSONdata += "]\n";
		//fileOut->WriteLine(JSONdata);
		//JSONdata = "";
	} while(fileIn->EndOfStream == false);

	JSONdata += "]";

	fileOut->WriteLine(JSONdata);
	fileOut->Close();

}