#ifndef _WB_COMMAND_UI_H_
#define _WB_COMMAND_UI_H_


#include "wb_context.h"

namespace wb
{
  enum ToolbarItemType
  {
    ToolbarAction,
    ToolbarSeparator,
    ToolbarToggle,
    ToolbarCheck,
    ToolbarRadio,
    ToolbarLabel,
    ToolbarDropDown,
    ToolbarSearch
  };

  struct MYSQLWBBACKEND_PUBLIC_FUNC WBToolbarItem
  {
    bec::IconId icon;
    bec::IconId alt_icon;
    std::string caption;
    std::string name;
    std::string command;
    std::string tooltip;
    ToolbarItemType type;

    bool enabled;
    bool checked;

    WBToolbarItem() : icon(0), alt_icon(0), type(ToolbarAction), enabled(true), checked(false)
    {

    }
  };

  typedef std::vector<bec::MenuItem> MenuItemList;

  
  // Manager menus and toolbars.
  class MYSQLWBBACKEND_PUBLIC_FUNC CommandUI
  {
    friend class WBContextUI;
    
    struct BuiltinCommand
    {
      sigc::slot<void> execute;
      sigc::slot<bool> validate;
    };

    WBContext *_wb;
    // dynamic UI stuff
    std::vector<app_MenuItemRef> _main_menus;
    std::map<std::string, app_MenuItemRef> _menu_items;

    grt::ListRef<app_ShortcutItem> _shortcuts;

    std::map<std::string, BuiltinCommand> _builtin_commands;

    bool validate_command_item(const app_CommandItemRef &item);
    bool get_command_item_state(const app_CommandItemRef &item);
    std::string get_command_item_caption(const app_CommandItemRef &item);

    void append_menu_items(const grt::ListRef<app_MenuItem> &plist, const std::string &context,
      std::vector<bec::MenuItem> *items);
    void append_toolbar_items(const grt::ListRef<app_ToolbarItem> &plist, const std::string &context,
      std::vector<WBToolbarItem> *items);
    void append_shortcut_items(const grt::ListRef<app_ShortcutItem> &plist, const std::string &context,
      std::vector<WBShortcut> *items);

    void add_builtin_form_command(const std::string &name, 
      const sigc::slot<void,bec::UIForm*> &slot, 
      const sigc::slot<bool,bec::UIForm*> &validate, 
      bool main_form);
    bool is_builtin_command(const std::string &name);
    bool execute_builtin_command(const std::string &name);
    bool validate_builtin_command(const std::string &name);

    bool validate_active_form_command(const sigc::slot<bool,bec::UIForm*> &slot, bool main_form);
    void execute_active_form_command(const sigc::slot<void,bec::UIForm*> &slot, bool main_form);

  public:
    CommandUI(WBContext *wb);
    
    void load_data();

    void activate_command(const std::string &command);
    void activate_command_for_object(const GrtObjectRef &object, const std::string &command);
    
    std::vector<WBShortcut> get_shortcuts_for_context(const std::string &context);

    void add_frontend_commands(const std::list<std::string> &commands);
    void add_builtin_command(const std::string &name, 
                             const sigc::slot<void> &slot,
                             const sigc::slot<bool> &validate= sigc::slot<bool>());

    MenuItemList get_main_menus();
    MenuItemList get_menu_items(const std::string &menu);
    std::vector<WBToolbarItem> get_toolbar_items(const std::string &toolbar);

    std::vector<std::string> get_dropdown_items(const std::string &name, const std::string &option, std::string &selected);
    void select_dropdown_item(const std::string &name, const std::string &option, const std::string &item);
    void toggle_checkbox_item(const std::string &name, const std::string &option, bool state);
  };
};


#endif /* _WB_COMMAND_UI_H_ */
