#include "gui/guecstra.hpp"
#include "game_level.hpp"

namespace guecs {

  Clickable make_action(guecs::Entity gui_id, Events::GUI event) {
    return {[&, gui_id, event](auto){
      auto world = GameDB::current_world();
      world->send<Events::GUI>(event, gui_id, {});
    }};
  }

  Clickable make_action(guecs::Entity gui_id, Events::GUI event, std::any data) {
    return {[&, event, data](auto){
      auto world = GameDB::current_world();
      world->send<Events::GUI>(event, gui_id, data);
    }};
  }

  DinkyECS::Entity GrabSource::grab() {
    fmt::println("> Grab entity {}", world_entity);
    return world_entity;
  }

  void GrabSource::setSprite(guecs::UI& gui, guecs::Entity gui_id) {
    if(auto sp = gui.get_if<guecs::Icon>(gui_id)) {
      sprite = sp->sprite;
    } else if(auto sp = gui.get_if<guecs::Sprite>(gui_id)) {
      sprite = sp->sprite;
    } else {
      dbc::sentinel("GrabSource given sprite gui_id that doesn't exist");
    }
  }

  void GrabSource::move(sf::Vector2f pos) {
    if(sprite) {
      sprite->setPosition(pos);
    }
  }
}
