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

#ifndef CONTEXT_H_
#define CONTEXT_H_
#include <map>
#include <string>
#include "Utils/to_string.h"
#include "Elements/SenchaObject.h"
#include "Elements/SenchaFunction.h"
#include "AST/ASTExpression.h"



/**
 * Context is object used to store variables for some execution context.
 * It's most useful in function calls. Every function has to have its own execution contexts,
 * for its variables. Arguments are passed to functions by predefining them in new Context, assigned to
 * function.
 */
class Context {
public:
	/**
	 * Context has unique name, which is passed to it in constructor.
	 */
	Context(std::string name);
	std::string name;

	/**
	 * Typedef to make further declarations easier to read.
	 */
	typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);

	/**
	 * Function have to be declared, which means here, registered before usage.
	 * You can register them with register_function functions.
	 */
	void register_function(std::string name, PointerToNativeFunction f);
	void register_function(std::string name, SenchaFunction * f);
	/**
	 * Map with function written in C++ stored like {"my_function" : &my_function}
	 */
	std::map<std::string, PointerToNativeFunction> registered_functions;
	/**
	 * Map with function written in Sencha stored like {"my_function" : &my_function}
	 */
	std::map<std::string, SenchaFunction *> registered_sfunctions;

	/**
	 * contains_function("x") returns true if x can be found in registered native (c++) or sencha functions.
	 */
	bool contains_function(std::string name);

	/**
	 * contains_function("x") returns true if x can be found in registered sencha functions.
	 */
	bool contains_sfunction(std::string name);

	/**
	 * contains_function("x") returns true if x can be found in registered native (c++).
	 */
	bool contains_nfunction(std::string name);

	/**
	 * Executes function written in c++
	 */
	SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);

	/**
	 * get_updated_string(name) return string of given name with all updates done on its elements such as name[9] = "a";
	 */
	SenchaObject get_updated_string(std::string name);

	/**
	 * debug() provides very useful info about context. What variables are stored, what are their values.
	 */
	std::string debug() ;

	/**
	 * add(name, value) adds value called name to object store.
	 */
	void add(std::string name, SenchaObject object);

	/**
	 * get(name) returns value from object store. If there isn't such a value. It returns SenchaObject();
	 */
	SenchaObject get(std::string name);


	virtual ~Context();

private:
	unsigned int index;
	std::map<std::string, SenchaObject> object_store;
};

#endif /* CONTEXT_H_ */
