#include "VariableLoader.h"

vector<jassVariables> loadVariables;

void readGlobals(jassFile * file)
{
	int globalsStart;
	bool didFind = false;
	bool cantFind = false;
	string testCase;
	vector<string> toDisect;
	jassVariables singleVar;

	globalsStart = 0;
	while ((!didFind) && (!cantFind))
	{
		testCase = file->lines->at(globalsStart).line_delimited.at(0);
		
		if (testCase.compare("globals"))
		{
			didFind = true;
		}
		else if (testCase.compare("function"))
		{
			cantFind = true;
		}
		else
		{
			globalsStart++;
		}
	}
	if (cantFind)
	{
		cout << "011 Error: Unable to deal with no-globals case... program likely to crash or act unpredictably! \n";
		return;
	}
	didFind = false;
	while (!didFind)
	{
		toDisect = file->lines->at(globalsStart).line_delimited;
		if (toDisect.at(0).compare("endglobals"))
		{
			didFind = true;
		}
		else if (toDisect.size() > 1)
		{
			if (toDisect.at(0).substr(0,2).compare("//"))
			{
				singleVar.type = toDisect.at(0);
				if (toDisect.at(1).compare("array"))
				{
					singleVar.variable = toDisect.at(1);
				}
				else
				{
					singleVar.variable = toDisect.at(2);
				}
				singleVar.contextStatus = CONTEXT_NOT_USED;
				singleVar.contextType = "";
				loadVariables.push_back(singleVar);
			}
		}

	}

}


void addContextVariable(string type,string name)
{
	int i;
	bool found;
	jassVariables singleVar;

	found = false;

	for (i=0;i<loadVariables.size();i++)
	{
		if (!loadVariables.at(i).variable.compare(name))
		{
			loadVariables.at(i).contextStatus = CONTEXT_USED;
			loadVariables.at(i).contextType = type;
			found = true;
		}
	}
	
	if (!found)
	{
		singleVar.contextStatus = CONTEXT_ONLY_CONTEXTUAL;
		singleVar.contextType = type;
		singleVar.variable = name;
		loadVariables.push_back(singleVar);
	}
}



