/* -*- 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>

#include "exception.h"

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()) {
    if(group + (*g).first != "") s += group + (*g).first + " = {}\n";
    s += loadresultstring((*g).second, group + (*g).first + ".");
    g++;
  }

  return s;
}

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

  luaL_openlibs(L);

  clean_top = lua_gettop(L);
}

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

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

  PRACRO_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)
{
  Value v;

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

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

  PRACRO_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 in " + mapper);
    lua_pop(L, lua_gettop(L) - clean_top);
    return v;
  }

  // value, timestamp, source

  // Check if the types are right
  if(lua_isstring(L, lua_gettop(L)) == false) {
    error("Source is not a string in " + mapper);
    lua_pop(L, 3);
    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);
    lua_pop(L, 2);
    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);
    lua_pop(L, 1);
    return v;
  }
  v.value = lua_tostring(L, lua_gettop(L));
  lua_pop(L, 1);

  PRACRO_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("ERROR in LUAQueryMapper: " + message);
}

#ifdef TEST_LUAQUERYMAPPER

#include "queryhandlerpentominos.h"
#include "queryparser.h"
#include "tcpsocket.h"

int main()
{
  TCPSocket s;
  s.connect("localhost", 11108);

  QueryHandlerPentominos qh(&s, "2003791613");

  Query q1;
  q1.attributes["device_id"] = "lensmeter";
  q1.attributes["device_type"] = "lensmeter";

  QueryResult res = qh.exec(q1);
  
  printf("%s\n", loadresultstring(res).c_str());

  LUAQueryMapper mapper;
  mapper.addQueryResult(res);

  std::string luamap = "return status.errstr.value, 0, 0";
  Value value = mapper.map(luamap);
  printf("%s : %s, %lu\n", luamap.c_str(), value.value.c_str(), value.timestamp);

  luamap = "return math.sin(status.errstr.timestamp) * 2, 0, 0";
  value = mapper.map(luamap);
  printf("%s : %s, %lu\n", luamap.c_str(), value.value.c_str(), value.timestamp);

  return 0;
}

#endif/*TEST_LUAQUERYMAPPER*/