/*
 * Context.cpp
 *
 *  Created on: Dec 30, 2012
 *      Author: att
 */

#include "Context.h"

Context::Context(std::string name) {
	// TODO Auto-generated constructor stub
	this->index = 0;
	this->name = name;
}

Context::~Context() {
	// TODO Auto-generated destructor stub
}

bool Context::contains_function(std::string name)
{
	 return contains_sfunction(name) || contains_nfunction(name);
}

SenchaObject Context::get_updated_string(std::string name)
{
	SenchaObject result = get(name);
	if(result.type == SenchaObject::string_literal)
	{
		for(unsigned int i = 0; i < result.text.size(); i++)
		{
			std::string indexed_name = "_" + name + "_" + to_string(i);
			if(object_store.count(indexed_name) != 0)
			{
				result.text[i] = object_store[indexed_name].text[0];
			}
		}
	}
	add(name, result);
	return result;
}

bool Context::contains_sfunction(std::string name)
{
	return this->registered_sfunctions.count(name) == 1;
}

bool Context::contains_nfunction(std::string name)
{
	return this->registered_functions.count(name);
}

void Context::register_function(std::string name, PointerToNativeFunction f)
{
	registered_functions[name] = f;
}

void Context::register_function(std::string name, SenchaFunction * f)
{
	registered_sfunctions[name] = f;
}


void Context::add(std::string name, SenchaObject object)
{
	object_store[name] = object;
}


SenchaObject Context::get(std::string name)
{
	if(object_store.count(name) != 0)
		return object_store[name];
	else return SenchaObject();
}

std::string Context::debug() {
	std::string debug_note = "";
	debug_note += "Context: " + this->name + " contains:\n";
	for( auto iter = this->object_store.begin(); iter != this->object_store.end(); iter++)
	{
		debug_note +=  "name: " + (*iter).first + ", value: " + (*iter).second.repr() + "\n";
	}
	return debug_note;


}
