/* 
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
 *
 * This program 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; version 2 of the
 * License.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef _SERVER_INSTANCE_EDITOR_H_
#define _SERVER_INSTANCE_EDITOR_H_


#include "workbench/wb_backend_public_interface.h"
#include "grts/structs.db.mgmt.h"

#include "grt/grt_manager.h"

#include "mforms/form.h"
#include "mforms/box.h"
#include "mforms/textentry.h"
#include "mforms/treenodeview.h"
#include "mforms/selector.h"
#include "mforms/button.h"
#include "mforms/tabview.h"
#include "mforms/radiobutton.h"
#include "mforms/checkbox.h"
#include "mforms/label.h"
#include "mforms/textbox.h"

class MYSQLWBBACKEND_PUBLIC_FUNC ServerInstanceEditor : public mforms::Form 
{ 
  bec::GRTManager *_grtm;
  
  db_mgmt_ManagementRef _mgmt;
  grt::ListRef<db_mgmt_ServerInstance> _instances;
  
  
  mforms::Box _top_vbox;
  mforms::Box _top_hbox;
  
  mforms::TextEntry _name_entry;
  
  mforms::Box _content_box;
  
  mforms::Box    _inst_list_buttons_hbox;
  mforms::Button _add_inst_button;
  mforms::Button _del_inst_button;
  mforms::Button _dup_inst_button;
  mforms::Button _move_up_button;
  mforms::Button _move_down_button;
  mforms::TreeNodeView _stored_instance_list;
  
  mforms::TabView _tabview;
  mforms::Box _conn_box;
  
  mforms::Selector _connection_selector;
  mforms::TextEntry _server_version;

  mforms::RadioButton _no_remote_admin;
  mforms::RadioButton _win_remote_admin;
  mforms::RadioButton _ssh_remote_admin;

  mforms::Box _remote_param_box;
  mforms::TextEntry _remote_host;  
  mforms::TextEntry _ssh_port;
  mforms::TextEntry _remote_user;
  mforms::Box _password_box;
  mforms::Button _password_set;
  mforms::Button _password_clear;
  mforms::CheckBox _ssh_usekey;
  mforms::TextEntry _ssh_keypath;

  mforms::Button _autodetect_button;
  
  mforms::Box _sys_box;
  mforms::Selector _os_type;
  mforms::Selector _sys_profile_type;
  mforms::TextEntry _sys_config_path;
  mforms::TextEntry _sys_myini_section;
  mforms::Label *_sys_win_service_name_label;
  mforms::TextEntry _sys_win_service_name;
  mforms::Button _sys_config_path_browse;
  mforms::Label _sys_win_hint_label;
  
  mforms::Label _details_description;
  mforms::TabView _details_tabview;
  mforms::TextEntry _start_cmd;
  mforms::TextEntry _stop_cmd;
  mforms::TextEntry _status_cmd;
  mforms::CheckBox _sudo_check;
  mforms::CheckBox _sudo_status_check;
  mforms::Selector _sudo_cmd;
  
  mforms::TextBox _script_text;
  
//  mforms::Button _save_preset_button;
//  mforms::Button _delete_preset_button;
  
  mforms::Box _bottom_hbox;
  mforms::Button _ok_button;
  mforms::Button _test_button;
  
  std::map<std::string, std::vector<std::pair<std::string,grt::DictRef> > > _presets;
  
  db_mgmt_ServerInstanceRef selected_instance();
  
  void open_editor(bool edit_selected);
  void autodetect_system();
  void test_settings();
  
  void check_version();
  
  void toggle_administration();

  grt::DictRef get_preset(const std::string& system, const std::string& preset_name);
  
  void entry_changed(mforms::TextEntry *sender);
  void selector_changed(mforms::Selector *sender);
  void check_changed(mforms::CheckBox *check);
  void text_changed(mforms::TextBox *text);

//  void button_clicked(mforms::Button *button);
  void browse_file();
  void show_instance();
  void add_instance();
  void delete_instance();
  void duplicate_instance();
  void reorder_instance(bool up);
  
  void instance_changed();

  void system_type_changed();
  void connection_changed();
  void profile_changed();
  
  void refresh_options();
  
  void refresh_profile_list();
  void refresh_instance_list();

  void set_password(bool clear);
  
  void show_filter_help();
  void run_filters_debugger();
  void run_filechooser(mforms::TextEntry* entry);
  void run_filechooser_wrapper(mforms::TextEntry* entry); // Allows to run local or remote file selector
public:
  ServerInstanceEditor(bec::GRTManager *grtm, const db_mgmt_ManagementRef &mgmt);
  
  db_mgmt_ServerInstanceRef run();
};


#endif /* _SERVER_INSTANCE_EDITOR_H_ */
