/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef _WIZARDFORM_H_
#define _WIZARDFORM_H_

#include <mforms/mforms.h>
#include <vector>
#include <set>

#include <grtpp.h>
#include "grt/common.h"

#include "wbpublic_public_interface.h"



namespace bec {
  class GRTManager;
};

namespace grtui {

  class WizardPage;

  class WBPUBLICBACKEND_PUBLIC_FUNC WizardForm : public ::mforms::Wizard
  {
  public:
    WizardForm(bec::GRTManager *mgr);
    virtual ~WizardForm();

    virtual grt::DictRef run_modal(grt::DictRef &values);

    void add_page(WizardPage *page);

    void update_buttons();

    void set_problem(const std::string &text);
    void clear_problem();

    virtual void reset();
    
    void switch_to_page(WizardPage *page, bool advancing);
    WizardPage *get_active_page() { return _active_page; }
    int get_active_page_number();
    
    WizardPage *get_page_with_id(const std::string &id);

    bec::GRTManager *grtm() { return _grtm; }
    
    grt::DictRef values() { return _values; }
    
  public:
    // util stuff for storing state
    void set_wizard_option(const std::string &key, const std::string &value);
    std::string string_wizard_option(const std::string &key, const std::string &default_value= "");

    void set_wizard_option(const std::string &key, int value);
    int int_wizard_option(const std::string &key, int default_value= 0);

  protected:    
    bec::GRTManager *_grtm;
    
    grt::DictRef _values;
     
    std::string _problem;

    WizardPage *_active_page;
    std::vector<WizardPage*> _pages;
    std::list<WizardPage*> _turned_pages;

    virtual WizardPage *get_next_page(WizardPage *current);
    void refresh_step_list();

    void extra_clicked();
    
  public:
    void go_to_next();
    void go_to_back();
    void finish();
    virtual bool cancel();
  };


  /** A page of a wizard.
    */
  class WBPUBLICBACKEND_PUBLIC_FUNC WizardPage : public ::mforms::Box
  {
  public:
    WizardPage(WizardForm *form, const std::string &pageid);

    std::string get_id() const { return _id; }
    
    std::string get_title() const { return _title; }
    std::string get_short_title() const { return _short_title; }
    
    void set_title(const std::string &title);
    void set_short_title(const std::string &title);

    void validate();

    sigc::signal<void, bool> signal_enter() { return _signal_enter; }
    sigc::signal<void, bool> signal_leave() { return _signal_leave; }
    
  public:


  protected:
    friend class WizardForm;

    void enable_file_browsing(mforms::TextEntry *entry, mforms::Button *button, mforms::FileChooserType type,
                              const std::string &extensions= "", const std::string &directory_save_key= "");
    bool check_and_confirm_file_overwrite(mforms::TextEntry *entry);
    
    grt::DictRef values() { return _form->values(); }

    //! Subclasses must override this to implement validation.
    //! If there is a validation error, it must call _form->set_problem()
    virtual void do_validate() { }
    
    virtual int load() { return -1; } // delme XXX
    
    virtual bool pre_load();
    virtual void enter(bool advancing);
    virtual bool advance();
    virtual void leave(bool advancing);
    
    virtual bool allow_next() { return true; }
    virtual bool allow_back() { return true; }
    virtual bool allow_cancel() { return true; }
    
    //! return true if this is the last page and pressing next should close wizard
    virtual bool next_closes_wizard() { return false; }
    
    //! overrider may return "" for default caption
    virtual std::string next_button_caption() { return ""; }
    
    virtual std::string extra_button_caption() { return ""; }

    virtual void extra_clicked() {}
    
  protected:
    WizardForm *_form;
    std::string _id;
    sigc::signal<void, bool> _signal_enter;
    sigc::signal<void, bool> _signal_leave;
    std::string _title;
    std::string _short_title;
    std::set<mforms::TextEntry*> _file_entries_set_from_browse_dialog;
    
    std::string execute_caption() const
    {
#ifdef __APPLE__
      return _("Execute");
#elif defined(_WIN32)
      return _("_Execute >");
#else
      return _("_Execute");
#endif
    }

    std::string finish_caption() const
    {
#ifdef __APPLE__
      return _("Finish");
#else
      return _("_Finish");
#endif
    }

    std::string close_caption() const
    {
#ifdef __APPLE__
      return _("Close");
#else
      return _("_Close");
#endif
    }
    
  private:
    void filename_changed(mforms::TextEntry *entry);
    void browse_file_callback(mforms::TextEntry *entry, mforms::FileChooserType type,
                              const std::string &extensions);
  };

};


#endif
