/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            luaquerymapper.cc
 *
 *  Mon May  5 15:43:52 CEST 2008
 *  Copyright 2008 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "luaquerymapper.h"

#include <sstream>

static std::string loadresultstring(QueryResult &res, std::string group = "")
{
  std::string s;
  std::stringstream timestamp; timestamp << res.timestamp;

  std::map< std::string, std::string >::iterator v = res.values.begin();
  while(v != res.values.end()) {
    s += group + (*v).first + " = {}\n";
    s += group + (*v).first + ".value = \"" + (*v).second + "\"\n";
    s += group + (*v).first + ".timestamp = " + timestamp.str() + "\n";
    s += group + (*v).first + ".source = \"" + res.source + "\"\n";
    v++;
  }

  std::map< std::string, QueryResult >::iterator g = res.groups.begin();
  while(g != res.groups.end()) {
    s += group + (*g).first + " = {}\n";
    s += loadresultstring((*g).second, group + (*g).first + ".");
    g++;
  }

  return s;
}

LUAQueryMapper::LUAQueryMapper() throw(Exception)
{
  clean_top = -1;

  L = luaL_newstate();
  if(L == NULL) {
    error("Could not create LUA state.");
    return;
  }

  luaL_openlibs(L);

  clean_top = lua_gettop(L);
}

LUAQueryMapper::~LUAQueryMapper()
{
  if(L) lua_close(L);
}

void LUAQueryMapper::addQueryResult(QueryResult &result) throw(Exception)
{
  std::string preload = loadresultstring(result);

  DEBUG(querymapper, "Preload:\n%s\n", preload.c_str());

  if(luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload")) {
    error(lua_tostring(L, lua_gettop(L)));
    return;
  }

  // Run program (init)
  if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
    error(lua_tostring(L, lua_gettop(L)));
    return;
  }

  clean_top = lua_gettop(L);
}

Value LUAQueryMapper::map(const std::string &mapper) throw(Exception)
{
  Value v;

  if(L == NULL) {
    error("LUA state not initialized!");
    return v;
  }

  if(mapper == "") {
    error("Empty LUA mapper detected in " + mapper);
    return v;
  }

  DEBUG(querymapper, "Mapper: %s\n", mapper.c_str());

  // Load the mapper
  if(luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper")) {
    error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper);
    return v;
  }

  // Run the loaded code
  if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
    error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper);
    return v;
  }

  // Check if app messed up the stack.
  if(lua_gettop(L) != clean_top + 3) {
    error("Wrong number of return values (should be value, timestamp, source) in " + mapper);
    return v;
  }

  // Check if the types are right
  if(lua_isstring(L, lua_gettop(L)) == false) {
    error("Source is not a string in " + mapper);
    return v;
  }
  v.source = lua_tostring(L, lua_gettop(L));
  lua_pop(L, 1);

  // Check if the types are right
  if(lua_isnumber(L, lua_gettop(L)) == false) {
    error("Timestamp is not an integer in " + mapper);
    return v;
  }
  v.timestamp = lua_tointeger(L, lua_gettop(L));
  lua_pop(L, 1);

  // Check if the types are right
  if(lua_isstring(L, lua_gettop(L)) == false) {
    error("Value is not a string in " + mapper);
    return v;
  }
  v.value = lua_tostring(L, lua_gettop(L));
  lua_pop(L, 1);

  DEBUG(querymapper, "Result: value=%s, src=%s, time=%d\n",
        v.value.c_str(), v.source.c_str(), (int)v.timestamp);

  return v;
}

void LUAQueryMapper::error(std::string message) throw(Exception)
{
  if(clean_top != -1) lua_pop(L, lua_gettop(L) - clean_top); // Clean up stack
  throw Exception("ERROR in LUAQueryMapper: " + message);
}

std::string LUAQueryMapper::automap(const std::string &name)
{
  std::string group;
  std::string groupcheck = "if(";

  for(size_t i = 0; i < name.length(); i++) {
    group += name[i];
    if(name[i] == '.') groupcheck += " and " + group;
    else groupcheck += name[i];
  }
  groupcheck += " and " + name + ".value and " + name + ".timestamp and " + name + ".source";
  groupcheck += ")\n";

  std::string automapstring =
    "-- Returning 0, 0 invalidates the result\n"
    "value = 0\n"
    "timestamp = 0\n"
    "source = 0\n"
    "\n"
    + groupcheck + 
    "then\n"
    "  value = " + name + ".value\n"
    "  timestamp = " + name + ".timestamp\n"
    "  source = " + name + ".source\n"
    "end\n"
    "return value, timestamp, source\n";

  DEBUG(widget, "Automap:\n%s\n", automapstring.c_str());

  return automapstring;
}


#ifdef TEST_LUAQUERYMAPPER

int main()
{
  QueryResult res;

  time_t now = time(NULL);

  res.groups["test"].timestamp = now;
  res.groups["test"].source = "test app";
  res.groups["test"].values["somevalue"] = "hello world";
  res.groups["test"].values["pi"] = "3.1416";
  
  printf("%s\n", loadresultstring(res).c_str());

  LUAQueryMapper mapper;
  mapper.addQueryResult(res);

  // Test simple value forwarding
  std::string luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source";
  Value value = mapper.map(luamap);
  printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
  if(value.value != "hello world" ||  value.timestamp != now || value.source != "test app")
    return 1;

  // Do some calculations
  luamap = "return 2 * tonumber(test.pi.value), test.pi.timestamp, test.pi.source";
  value = mapper.map(luamap);
  printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
  if(value.value != "6.2832" ||  value.timestamp != now || value.source != "test app")
    return 1;

  // Attempt to access nonexisting value (should throw an exception)
  try {
    luamap = "return test.somevalue2.value, test.somevalue2.timestamp, test.somevalue2.source";
    value = mapper.map(luamap);
    printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
    if(value.value != "hello world" ||  value.timestamp != now || value.source != "test app")
      return 1;
  } catch(Exception &e) {
    printf("ERROR: %s\n", e.what());
    goto onandon;
  }
  return 1;
 onandon:

  // Attempt to access nonexisting group (should throw an exception)
  try {
    luamap = "return test2.somevalue.value, test2.somevalue.timestamp, test2.somevalue.source";
    value = mapper.map(luamap);
    printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
    if(value.value != "hello world" ||  value.timestamp != now || value.source != "test app")
      return 1;
  } catch(Exception &e) {
    printf("ERROR: %s\n", e.what());
    goto stillonandon;
  }
  return 1;
 stillonandon:
  
  // Switch order of return vars (should throw an exception)
  try {
    luamap = "return test.somevalue.source, test.somevalue.value, test.somevalue.timestamp";
    value = mapper.map(luamap);
    printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
    if(value.value != "hello world" ||  value.timestamp != now || value.source != "test app")
      return 1;
  } catch(Exception &e) {
    printf("ERROR: %s\n", e.what());
    goto onandonagain;
  }
  return 1;
 onandonagain:

  // Syntax error (should throw an exception)
  try {
    luamap = "this(is{] not() - a != legal lua program!]";
    value = mapper.map(luamap);
    printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
    if(value.value != "hello world" ||  value.timestamp != now || value.source != "test app")
      return 1;
  } catch(Exception &e) {
    printf("ERROR: %s\n", e.what());
    goto stillonandonagain;
  }
  return 1;
 stillonandonagain:

  // And finally test if we haven't broken enything while being hostile to the lua engine...
  luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source";
  value = mapper.map(luamap);
  printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str());
  if(value.value != "hello world" ||  value.timestamp != now || value.source != "test app")
    return 1;

  return 0;
}

#endif/*TEST_LUAQUERYMAPPER*/