/* 
 * © 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 _WIZARDPROGRESSPAGE_H_
#define _WIZARDPROGRESSPAGE_H_

#include "grt_wizard_form.h"

namespace grtui {

  class WBPUBLICBACKEND_PUBLIC_FUNC WizardProgressPage : public WizardPage
  {
  public:
    WizardProgressPage(WizardForm *form, const std::string &id);
    virtual ~WizardProgressPage();

    virtual ::mforms::View *get_advanced_panel() { return &_log_panel; }

    sigc::signal<void,bool> &signal_tasks_finished() { return _signal_finished; }

    void set_heading(const std::string &text);
    
  protected:
    enum TaskState {
      StateNormal,
      StateBusy,
      StateDone,
      StateWarning,
      StateError,
      StateDisabled
    };

    struct WBPUBLICBACKEND_PUBLIC_FUNC TaskRow
    {
      mforms::ImageBox icon;
      mforms::Label label;
      sigc::slot<bool> execute; //! return value indicates whether an asynchronous function was actually executed
      sigc::slot<bool> process_fail; //! return value indicates whether it can continue executing ok
      sigc::slot<void, grt::ValueRef> process_finish;
      std::string status_text;
      bool enabled;
      bool async;
      bool async_running;
      bool async_failed;
      bool async_errors;

      TaskRow() : enabled(true), async(false), async_running(false), async_failed(false), async_errors(0) {}
      
      void set_state(TaskState state);
      void set_enabled(bool flag);
    };

    mforms::Label _heading;

    std::vector<TaskRow*> _tasks;

    std::string _finish_message;

    mforms::Label _status_text;

    mforms::Table _task_table;
    
    mforms::ProgressBar *_progress_bar;
    mforms::Label *_progress_label;

    mforms::Panel _log_panel;
    mforms::TextBox _log_text;

    sigc::signal<void,bool> _signal_finished;

    int _current_task;
    bool _busy;
    bool _done;
    bool _got_warning_messages;
    bool _got_error_messages;
    

    TaskRow *add_async_task(const std::string &caption,
                            const sigc::slot<bool> &execute,
                            const std::string &status_text);

    TaskRow *add_task(const std::string &caption,
                      const sigc::slot<bool> &execute,
                      const std::string &status_text);

    TaskRow *add_disabled_task(const std::string &caption);

    void end_adding_tasks(bool add_progressbar, 
                          const std::string &finish_message);
    
    void reset_tasks();

    void start_tasks();

    void set_status_text(const std::string &text, bool is_error= false);

    void update_progress(float pct, const std::string &caption);

    void add_log_text(const std::string &text);
    
    virtual void extra_clicked();
  private:
    TaskRow *add_task(bool async, 
                      const std::string &caption,
                      const sigc::slot<bool> &execute,
                      const std::string &status_text);

  public:
    void execute_grt_task(const sigc::slot<grt::ValueRef,grt::GRT*> &slot, bool sync);

    void process_grt_task_message(const grt::Message &msg);
    void process_grt_task_fail(const std::exception &error);
    void process_grt_task_finish(const grt::ValueRef &result);

  protected:
    void perform_tasks();

    virtual bool allow_cancel();
    virtual bool allow_next();
    virtual bool allow_back();
    
    virtual std::string extra_button_caption();

    virtual void enter(bool advancing);
  };

};
  
  
#endif /* _WIZARDPROGRESSPAGE_H_ */
