#include "Global.h"
#include "Exception.h"
#include "__mysql.h"
#include <iostream>
#include <stdlib.h>

namespace jet {

   Global::Global() {
      
   }

   Global::~Global() {

   }

   void Global::dump() {
      for (auto i = variables.begin(); i != variables.end(); i++)
	std::cout << i->first << "=[" << i->second << "]" << std::endl; 
   }

   bool Global::sessionExists(coreutils::MString sessionId) {
      return sessions.find(sessionId) != sessions.end();
   }
   
   void Global::addSession(coreutils::MString sessionId, __mysql *mysql) {
      std::cout << "sessionId: " << sessionId << std::endl;
      if(sessionExists(sessionId))
	coreutils::Exception("sessionid already exists.");
      sessions[sessionId] = mysql;

      std::cout << "::count: " << sessions.size() << std::endl;
      std::cout << "::" << sessionId << std::endl;
   }
   
   void Global::removeSession(coreutils::MString sessionId) {
      sessions.erase(sessionId);
   }

   coreutils::ZString Global::getVariable(coreutils::ZString &variable, std::map<coreutils::ZString, coreutils::MString> &lvariables) {
      if(variable.ifNext("$[")) {
	 coreutils::MString name;
	 coreutils::MString modifier;
	 if(variable.ifNext("!")) {
	    renderVariableName(variable, name, modifier, lvariables);
	    return variables[name];
	 } if(variable.ifNext(":")) {
	    // TODO: should only allow CGI variable name. Allow variable substitution.
	 } if(variable.ifNext("@")) {
	    // TODO: should only allow session variables. Allow substitution.
	 } if(variable.ifNext("%")) {
	    renderVariableName(variable, name, modifier, lvariables);
	    return getenv(name.c_str());
	 } else {
	    renderVariableName(variable, name, modifier, lvariables);
	    name.split(".");
	    if(name.getList().size() == 1)
	      return variables[name[0]];
	    return getSessionVariable(name);
	 }
	 throw coreutils::Exception("expected variable name or type designator.");	 
      } if(variable.ifNext("#[")) {
	 coreutils::MString name;
	 coreutils::MString modifier;
	 renderVariableName(variable, name, modifier, lvariables);
	 return lvariables[name];	 
      }
      throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
   }

   void Global::renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier, std::map<coreutils::ZString, coreutils::MString> &lvariables) {
      while(!variable.ifNext("]")) {	       
	 name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
	 if(variable.ifNext(";")) {	   
	    renderVariableName(variable, modifier, modifier, lvariables);
	    return;
	 }      
	 else if(variable.startsWith("$[")) {	   
	    name << getVariable(variable, lvariables);	    
	 }
	 else if(variable.startsWith("#[")) {	   
	    name << getVariable(variable, lvariables);	    
	 }
      }	    
      return;
   }
      
   __mysql * Global::getSession(coreutils::MString sessionId) {
      if(sessions.find(sessionId) == sessions.end())
	throw coreutils::Exception("requested session is not available.");
      return sessions[sessionId];
   }
   
   coreutils::ZString Global::getSessionVariable(coreutils::MString &splitName) {
      if(sessions.find(splitName[0]) == sessions.end())
	throw coreutils::Exception("requested session is not available in variable.");
      return sessions[splitName[0]]->getColumnValue(splitName[1]);
   }

   void Global::outputHeaders() {
      if(headers.size() > 0) {	
	 for(auto header = headers.begin();
	     header != headers.end();
	     ++header) {
	    std::cout << header->first << ": " << header->second << std::endl;
	 }
	 std::cout << std::endl;
      }   
   }
   
}
