#pragma once

#include <gtkmm/box.h>
#include <gtkmm/label.h>
#include <gtkmm/image.h>
#include <gtkmm/button.h>
#include <gtkmm/flowbox.h>
#include <gtkmm/entry.h>
#include <sigc++/connection.h>
#include <string>
#include "Book.hpp"
#include "DatabaseManager.hpp"
#include "BookList.hpp"

class BookDetails : public Gtk::Box {
public:
  BookDetails(DatabaseManager& db, BookList& book_list);
  ~BookDetails() override;

  void set_book(const Book* book);
  void clear();

  // Signal emitted when the displayed book is removed from the list
  sigc::signal<void()>& signalBookRemoved() { return m_signalBookRemoved; }

private:
  void on_open_button_clicked();
  void on_add_tag_button_clicked();
  void on_remove_tag_clicked(const std::string& tag_name);
  void refresh_tags();
  void refresh_display();
  Gtk::Widget* create_tag_chip(const std::string& tag_name);
  void on_book_list_changed();
  void on_book_removed(const std::string& id);

  DatabaseManager& m_db;
  BookList& m_book_list;
  std::string m_book_id;

  Gtk::Image m_cover;
  Gtk::Label m_title;
  Gtk::Label m_author;
  Gtk::Button m_open_button;
  Gtk::FlowBox m_tags_box;
  Gtk::Entry m_tag_entry;
  Gtk::Button m_add_tag_button;

  sigc::signal<void()> m_signalBookRemoved;
  sigc::connection m_conn_removed;
  sigc::connection m_conn_updated;
  sigc::connection m_conn_reset;
};
