#pragma once

#include <cgv/base/node.h>
#include <cgv/gui/event_handler.h>
#include <cgv/render/drawable.h>

#include <density_estimation/state/density_estimator_state.h>

#include <cgv/signal/signal.h>

class planar_view_interactor
  : public cgv::base::node
  , public cgv::gui::event_handler
  , public cgv::render::drawable
{
private:
  density_estimator_state_ptr state;

  double magnification = 1.0;
  cgv::math::vec<double> target;

  bool moving = false;
  cgv::math::vec<double> movement_start;
  std::array<int,2> last_mouse_position;

public:
  planar_view_interactor(density_estimator_state_ptr state);

  bool handle(cgv::gui::event& event) override;

  void stream_help(std::ostream& os) override;

  void init_frame(cgv::render::context& ctx) override;

  cgv::signal::signal<> restriction_selected;

protected:
  void zoom(int x, int y, float ds);

  void begin_movement(int x, int y);

  void move(int x, int y);

  void end_movement();

  void reset();

  void begin_condition_selection();

  void update_condition_selection_value(int x, int y);

  void commit_condition_selection_value(int x, int y);

  void end_condition_selection();

  void clear_restrictions();

  void toggle_detail_selection();

  void select_detail(int x);

  void toggle_pdf_collection_bandwidths_visibility();

  void toggle_pdf_detail_visibility();

  void toggle_pdf_bandwidth_functional_visibility();

private:
  void get_current_position(int x, int y, cgv::math::vec<double>& position);
};
