#include "stdafx.h"

#include "wb_config.h"

#include "grt/grt_manager.h"
#include "grts/structs.db.h"
#include "grts/structs.workbench.physical.h"
#include "sql_import_be.h"
#include "grtsqlparser/sql_facade.h"


void Sql_import::grtm(bec::GRTManager *grtm)
{
  if (grtm)
  {
    grt::GRT *grt= grtm->get_grt();
    _options= grt::DictRef(grt);
    _doc= workbench_DocumentRef::cast_from(grt->get("/wb/doc"));

    // init some options based on global defaults
    // FE will query them to init controls state
    {
      grt::DictRef options= grt::DictRef::cast_from(grtm->get_grt()->get("/wb/options/options"));
      const char *option_names[]= { "SqlIdentifiersCS" };
      for (size_t n= 0, count= sizeof(option_names)/sizeof(option_names[0]); n < count; ++n)
        _options.set(option_names[n], options.get(option_names[n]));
    }
  }
  else
  {
    _doc= workbench_DocumentRef();
  }
}


db_CatalogRef Sql_import::target_catalog()
{
  return _doc->physicalModels().get(0)->catalog();
}


sigc::slot<grt::ValueRef,grt::GRT*> Sql_import::get_task_slot()
{
  grt::StringRef (Sql_import::*parse_sql_script)(grt::GRT *, db_CatalogRef, const std::string &)= &Sql_import::parse_sql_script;

  return sigc::bind<db_CatalogRef, const std::string>(
    sigc::mem_fun(this, parse_sql_script),
    target_catalog(),
    sql_script());
}


sigc::slot<grt::ValueRef,grt::GRT*> Sql_import::get_autoplace_task_slot()
{
  return sigc::mem_fun(this, &Sql_import::autoplace_grt);
}


grt::StringRef Sql_import::parse_sql_script(grt::GRT *grt, db_CatalogRef catalog, const std::string &sql_script)
{
  SqlFacade *sql_parser= SqlFacade::instance_for_rdbms(
    db_mgmt_RdbmsRef::cast_from(catalog->owner().get_member("rdbms")));

  grt::ListRef<GrtObject> created_objects(grt);
  _options.set("created_objects", created_objects);

  parse_sql_script(sql_parser, catalog, sql_script, _options);

  return grt::StringRef("The SQL script was parsed");
}


grt::ListRef<GrtObject> Sql_import::get_created_objects()
{
  return grt::ListRef<GrtObject>::cast_from(_options.get("created_objects"));
}


void Sql_import::parse_sql_script(SqlFacade *sql_parser, db_CatalogRef &catalog, const std::string &sql_script, grt::DictRef &options)
{
  grt::AutoUndo undo(_doc.get_grt());
  options.set("sql_script_codeset", grt::StringRef(_sql_script_codeset));
  sql_parser->parseSqlScriptFileEx(catalog, sql_script, options);
  undo.end(_("Reverse Engineer from SQL Script"));
}


grt::ValueRef Sql_import::autoplace_grt(grt::GRT *grt)
{
  db_CatalogRef catalog= target_catalog();

  workbench_physical_ModelRef model(workbench_physical_ModelRef::cast_from(catalog->owner()));

  grt::ListRef<db_DatabaseObject> dbobjects(grt);
  grt::ListRef<GrtObject> objects(grt::ListRef<GrtObject>::cast_from(_options.get("created_objects")));

  for (grt::ListRef<GrtObject>::const_iterator iter= objects.begin();
    iter != objects.end(); ++iter)
  {
    if ((*iter).is_instance<db_DatabaseObject>())
      dbobjects.insert(db_DatabaseObjectRef::cast_from(*iter));
  }

  if (dbobjects.count() > 0)
  {
    grt::Module *module= grt->get_module("WbModel");

    grt::BaseListRef args(grt);

    args.ginsert(model);
    args.ginsert(objects);

    module->call_function("createDiagramWithObjects", args);
  }

  return grt::ValueRef();
}
