

#include "linux_utilities/plugin_editor_base.h"
#include "../backend/mysql_schema_editor.h"
#include "linux_utilities/MGGladeXML.h"
#include "grtdb/db_object_helpers.h"
#include "linux_utilities/image_cache.h"
#include <gtkmm/image.h>
#include <gtkmm/comboboxtext.h>
#include <gtkmm/textview.h>

class SchemaEditor : public PluginEditorBase
{
  MySQLSchemaEditorBE *_be;
  std::string          _old_name;

  virtual bec::BaseEditor *get_be()
  {
    return _be;
  }

public:
  virtual ~SchemaEditor()
  {
    delete _be;
    _be = 0;
  }
  
  SchemaEditor(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
    : PluginEditorBase(m, grtm, args, "modules/data/editor_schema.glade")
    , _be(new MySQLSchemaEditorBE(grtm, db_SchemaRef::cast_from(args[0]), get_rdbms_for_db_object(args[0])))
  {
    set_border_width(8);

    Gtk::Widget *widget;
    xml()->get("base_table", &widget);

    Gtk::Image *image;
    xml()->get("image", &image);
    image->set(ImageCache::get_instance()->image_from_filename("db.Schema.editor.48x48.png", false));

    bind_entry_and_be_setter("name_entry", this, &SchemaEditor::set_name);  

    Gtk::ComboBoxText *combo;
    xml()->get("collation_combo", &combo);
    fill_combo_from_string_list(combo, _be->get_charset_collation_list());
    add_option_combo_change_handler(combo, "CHARACTER SET - COLLATE", sigc::mem_fun(this, &SchemaEditor::set_schema_option_by_name));

    Gtk::TextView *tview;
    xml()->get("text_view", &tview);
    add_text_change_timer(tview, sigc::mem_fun(this, &SchemaEditor::set_comment));
    
    widget->reparent(*this);

    Gtk::Entry* entry(0);
    xml()->get("name_entry", &entry);
    /*
    entry->set_icon_from_pixbuf(ImageCache::get_instance()->image_from_filename("execute_script.png", false), Gtk::ENTRY_ICON_SECONDARY);
    entry->set_icon_tooltip_text(_("Refactor SQL in the schema"), Gtk::ENTRY_ICON_SECONDARY);
    entry->set_icon_activatable(true, Gtk::ENTRY_ICON_SECONDARY);
    entry->set_icon_sensitive(Gtk::ENTRY_ICON_SECONDARY, false);
     **/

    show_all();

    Gtk::Button *btn;
    xml()->get("refactor_button", &btn);
    btn->hide();
    btn->set_tooltip_text(_("Refactor SQL in the schema"));
    btn->signal_clicked().connect(sigc::mem_fun(this, &SchemaEditor::refactor_schema));

    refresh_form_data();
  }
  
  void set_name(const std::string& name)
  {
    if (_be)
    {
      _be->set_name(name);
      Gtk::Button* btn(0);
      xml()->get("refactor_button", &btn);
      btn->show();
    }
  }

  void refactor_schema()
  {
    if (_be)
    {
      Gtk::Entry* entry(0);
      xml()->get("name_entry", &entry);
      std::string new_name = entry->get_text();

      Gtk::Button* btn(0);
      xml()->get("refactor_button", &btn);
      btn->hide();

      _be->refactor_catalog_upon_schema_rename(_old_name, new_name);
      _old_name = new_name;
    }
  }

  void set_comment(const std::string& text)
  {
    if (_be)
      _be->set_comment(text);
  }
  
  void set_schema_option_by_name(const std::string &name, const std::string &value)
  {
    if (_be)
      _be->set_schema_option_by_name(name, value);
  }
  
  virtual void do_refresh_form_data()
  {    
    Gtk::Entry *entry;
    xml()->get("name_entry", &entry);

    Gtk::TextView *tview;
    xml()->get("text_view", &tview);

    Gtk::ComboBoxText *combo;
    xml()->get("collation_combo", &combo);

    if (_be)
    {
      combo->set_active_text(_be->get_schema_option_by_name("CHARACTER SET - COLLATE"));

      _old_name = _be->get_name();
      entry->set_text(_old_name);

      tview->get_buffer()->set_text(_be->get_comment());
    }
  }
  
  virtual std::string get_title()
  {
    return "Schema";
  }

  virtual bool switch_edited_object(bec::GRTManager *grtm, const grt::BaseListRef &args);
};

//------------------------------------------------------------------------------
bool SchemaEditor::switch_edited_object(bec::GRTManager *grtm, const grt::BaseListRef &args)
{
  MySQLSchemaEditorBE *old_be = _be;
  _be = new MySQLSchemaEditorBE(grtm, db_SchemaRef::cast_from(args[0]), get_rdbms_for_db_object(args[0]));
  
  if (_be)
  {
    do_refresh_form_data();
  
    delete old_be;
    old_be = 0;
  }
  else
    _be = old_be;

  return true;
}


extern "C" 
{
  GUIPluginBase *createDbMysqlSchemaEditor(grt::Module *m,  bec::GRTManager *grtm, const grt::BaseListRef &args)
  {
    return Gtk::manage(new SchemaEditor(m, grtm, args));
  }
};
