#ifndef _DB_MYSQL_SQL_EXPORT_H_
#define _DB_MYSQL_SQL_EXPORT_H_

#include "db_mysql_public_interface.h"
#include "grt/grt_manager.h"
#include "grts/structs.db.mysql.h"
#include "grt/grt_string_list_model.h"
#include "db_mysql_validation_page.h"

class WBPLUGINDBMYSQLBE_PUBLIC_FUNC DbMySQLSQLExport : public DbMySQLValidationPage
{
  //bec::GRTManager *_manager;
  db_mysql_CatalogRef _catalog;

  // options
  bool _skip_FKs;
  bool _gen_drops;
  bool _gen_schema_drops;
  bool _gen_warnings;
  bool _gen_create_index;
  bool _no_users_just_privileges;
  bool _gen_inserts;
  std::string _output_filename;
  bool _tables_are_selected, _triggers_are_selected, 
      _routines_are_selected, _views_are_selected, 
      _users_are_selected;
  bool _omit_schemata;

  bec::GrtStringListModel *_users_model;
  bec::GrtStringListModel *_users_exc_model;
  bec::GrtStringListModel *_tables_model;
  bec::GrtStringListModel *_tables_exc_model;
  bec::GrtStringListModel *_views_model;
  bec::GrtStringListModel *_views_exc_model;
  bec::GrtStringListModel *_routines_model;
  bec::GrtStringListModel *_routines_exc_model;
  bec::GrtStringListModel *_triggers_model;
  bec::GrtStringListModel *_triggers_exc_model;

  std::map<std::string,GrtNamedObjectRef> _users_map;
  std::map<std::string,GrtNamedObjectRef> _tables_map;
  std::map<std::string,GrtNamedObjectRef> _views_map;
  std::map<std::string,GrtNamedObjectRef> _routines_map;
  std::map<std::string,GrtNamedObjectRef> _triggers_map;

  std::string get_q_name(const char *part1, const char *part2)
  {
    return std::string(part1).append(".").append(part2);
  }

  void init_from_ctor(bec::GRTManager *grtm, db_mysql_CatalogRef catalog);

  db_mysql_CatalogRef filter_catalog();

protected:
  virtual db_mysql_CatalogRef get_model_catalog();
  virtual grt::DictRef get_options_as_dict(grt::GRT*);
  //bec::MessageListBE messages_list;

public:

  DbMySQLSQLExport(bec::GRTManager *grtm);
  DbMySQLSQLExport(bec::GRTManager *grtm, db_mysql_CatalogRef catalog);

  //bec::GRTManager *get_grt_manager() const { return _manager; }
  db_mysql_CatalogRef get_catalog() const { return _catalog; }

  std::string get_output_filename() const { return _output_filename; }
  
  void set_option(const std::string& name, bool value);
  void set_option(const std::string& name, const std::string& value);

  void start_export(bool wait_finish);
  //void run_validation();

  void export_finished(grt::ValueRef res);
  grt::ValueRef export_task(grt::GRT*, grt::StringRef);

  //void validation_finished(grt::ValueRef res);
  //grt::ValueRef validation_task(grt::GRT*, grt::StringRef);

  //typedef sigc::slot<int> Validation_finished_cb;
  //void validation_messages(MYX_GRT_MSGS *);
  //void validation_finished_cb(Validation_finished_cb cb) { _validation_finished_cb= cb; }
  //
  //typedef sigc::slot<int, int> Validation_step_finished_cb;
  //void validation_step_finished_cb(Validation_step_finished_cb cb) { _validation_step_finished_cb= cb; }

  typedef sigc::slot<int> Task_finish_cb;
  void task_finish_cb(Task_finish_cb cb) { _task_finish_cb= cb; }

  //bec::MessageListBE *get_messages_list()
  //{
  //  return &messages_list;
  //}

  void setup_grt_string_list_models_from_catalog(bec::GrtStringListModel **users_model, 
                                                 bec::GrtStringListModel **users_exc_model,
                                                 bec::GrtStringListModel **tables_model, 
                                                 bec::GrtStringListModel **tables_exc_model,
                                                 bec::GrtStringListModel **views_model, 
                                                 bec::GrtStringListModel **views_exc_model, 
                                                 bec::GrtStringListModel **routines_model, 
                                                 bec::GrtStringListModel **routines_exc_model, 
                                                 bec::GrtStringListModel **triggers_model,
                                                 bec::GrtStringListModel **triggers_exc_model);

  std::string export_sql_script() { return _export_sql_script; }

private:
  //Validation_finished_cb _validation_finished_cb;
  //Validation_step_finished_cb _validation_step_finished_cb;
  Task_finish_cb _task_finish_cb;
  std::string _export_sql_script;
};


grt::StringListRef convert_string_vector_to_grt_list(grt::GRT *grt, const std::vector<std::string>& v);

#endif // _DB_MYSQL_SQL_EXPORT_H_
