#pragma once

#include <Data/Preferences.h>
#include <Data/Telemetry.h>
#include <Groom/Groom.h>
#include <Groom/GroomParameters.h>

#include <QElapsedTimer>
#include <QObject>
#include <QProgressDialog>
#include <QSharedPointer>
#include <QWidget>
#include <string>

class Ui_GroomTool;

Q_DECLARE_METATYPE(std::string)

namespace shapeworks {

class Session;

class GroomTool : public QWidget {
  Q_OBJECT;

 public:
  GroomTool(Preferences& prefs, Telemetry& telemetry);
  ~GroomTool();

  //! Set the pointer to the session
  void set_session(QSharedPointer<Session> session);

  //! activate this tool
  void activate();

  //! Load params from project
  void load_params();
  //! Store params to project
  void store_params();

  //! Disable action buttons
  void disable_actions();
  //! Enable action buttons
  void enable_actions();

  //! shut down any running threads
  void shutdown_threads();

 Q_SIGNALS:
  void groom_start();
  void groom_complete();
  void progress(int);

 public Q_SLOTS:

  void on_antialias_checkbox_stateChanged(int state);
  void on_blur_checkbox_stateChanged(int state);
  void on_autopad_checkbox_stateChanged(int state);
  void skip_grooming_toggled();
  void on_restore_defaults_clicked();

  void update_ui();

  void domain_changed();

  void alignment_checkbox_changed(int state);
  void alignment_option_changed(int index);

  void reflect_checkbox_changed(int state);
  void reflect_column_changed(int index);
  void reflect_choice_changed(int index);
  void reflect_axis_changed(int index);

  //! Run groom tool
  void on_run_groom_button_clicked();

  void handle_thread_complete();
  void handle_progress(int val);
  void handle_error(QString msg);

  void add_shared_boundary_clicked();
  void delete_shared_boundary_clicked();

 private:
  void set_ui_from_params(GroomParameters params);

  void update_page();
  void update_domain_box();
  void apply_to_all_domains_changed();
  void update_shared_boundary_table();

  void update_reflect_columns();
  void update_reflect_choices();

  void set_session_modified();

  Preferences& preferences_;
  Telemetry& telemetry_;

  QList<QThread*> threads_;

  Ui_GroomTool* ui_;
  QSharedPointer<Session> session_;

  QSharedPointer<shapeworks::Groom> groom_;

  QElapsedTimer timer_;

  bool groom_is_running_ = false;

  std::string current_domain_;

  QStringList reflect_columns_;

  bool block_signals_ = false;

  bool block_session_modify_ = false;
};
}  // namespace shapeworks
