#include "gui/status_ui.hpp"
#include "components.hpp"
#include <guecs/ui.hpp>
#include "rand.hpp"
#include <fmt/xchar.h>
#include "gui/guecstra.hpp"
#include "systems.hpp"
#include "inventory.hpp"

namespace gui {
  using namespace guecs;
  using std::any, std::any_cast, std::string, std::make_any;

  StatusUI::StatusUI(GameLevel level) :
    $level(level), $ritual_ui(level)
  {
    $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
    $gui.layout(
        "[ritual_ui]"
        "[=earring|=armor_head|=amulet]"
        "[=back|=*%(200,300)character_view|_|=armor_bdy]"
        "[=hand_r|_|_                        |=hand_l]"
        "[=ring_r|_|_                        |=ring_l]"
        "[=pocket_r|=armor_leg|=pocket_l]");
  }

  void StatusUI::init() {
    $gui.set<Background>($gui.MAIN, {$gui.$parser, });

    for(auto& [name, cell] : $gui.cells()) {
      auto gui_id = $gui.entity(name);

      if(name == "character_view") {
        $gui.set<Rectangle>(gui_id, {});
        $gui.set<Sprite>(gui_id, {"armored_knight"});
      } else {
        $gui.set<Rectangle>(gui_id, {});
        $gui.set<ActionData>(gui_id, {make_any<string>(name)});

        if(name == "ritual_ui") {
          $gui.set<Clickable>(gui_id, {
              [this](auto, auto){ select_ritual(); }
          });
          $gui.set<Sound>(gui_id, {"pickup"});
        } else {
          $gui.set<Text>(gui_id, {guecs::to_wstring(name)});
          $gui.set<Clickable>(gui_id, {
            guecs::make_action($level, Events::GUI::INV_SELECT, {gui_id})
          });
          $gui.set<DropTarget>(gui_id, {
              .commit=[&, gui_id](DinkyECS::Entity world_target) -> bool {
                return place_slot(gui_id, world_target);
              }
          });
        }
      }
    }

    $ritual_ui.event(ritual::Event::STARTED);
    $gui.init();
    update();
  }

  bool StatusUI::mouse(float x, float y, bool hover) {
    if($ritual_ui.is_open()) {
      return $ritual_ui.mouse(x, y, hover);
    } else {
      return $gui.mouse(x, y, hover);
    }
  }

  void StatusUI::select_ritual() {
    $ritual_ui.event(ritual::Event::TOGGLE);
  }

  void StatusUI::update() {
    auto player = $level.world->get_the<components::Player>();
    auto& inventory = $level.world->get<inventory::Model>(player.entity);
    for(auto& [slot, world_entity] : inventory.by_slot) {
      auto gui_id = $gui.entity(slot);

      auto& sprite = $level.world->get<components::Sprite>(world_entity);
      $gui.set_init<guecs::Icon>(gui_id, {sprite.name});
      guecs::GrabSource grabber{ world_entity,
          [&, gui_id]() { return remove_slot(gui_id); }};
      grabber.setSprite($gui, gui_id);
      $gui.set<guecs::GrabSource>(gui_id, grabber);
    }
  }

  void StatusUI::render(sf::RenderWindow &window) {
    $gui.render(window);
    // $gui.debug_layout(window);
    $ritual_ui.render(window);
  }

  void StatusUI::update_level(GameLevel &level) {
    $level = level;
    init();
    $ritual_ui.update_level(level);
  }

  bool StatusUI::place_slot(guecs::Entity gui_id, DinkyECS::Entity world_entity) {
    auto& slot_name = $gui.name_for(gui_id);
    auto player = $level.world->get_the<components::Player>();
    auto& inventory = $level.world->get<inventory::Model>(player.entity);

    if(inventory.add(slot_name, world_entity)) {
      $level.world->make_constant(world_entity);
      update();
      return true;
    }  else {
      dbc::log("there's something there already");
      return false;
    }
  }

  bool StatusUI::drop_item(DinkyECS::Entity item_id) {
    bool dropped = System::drop_item($level, item_id);
    if(dropped) update();
    return dropped;
  }

  // NOTE: do I need this or how does it relate to drop_item?
  void StatusUI::remove_slot(guecs::Entity slot_id) {
    // NOTE: really the System should coordinate either dropping on the
    // ground or moving from one container or another, so when loot_ui
    // moves to use an ECS id to a container I can have the System
    // do it.
    auto& slot_name = $gui.name_for(slot_id);

    auto player = $level.world->get_the<components::Player>();
    auto& inventory = $level.world->get<inventory::Model>(player.entity);

    auto world_entity = inventory.get(slot_name);
    inventory.remove(world_entity);

    $gui.remove<guecs::GrabSource>(slot_id);
    $gui.remove<guecs::Icon>(slot_id);
  }

  void StatusUI::swap(guecs::Entity gui_a, guecs::Entity gui_b) {
    if(gui_a != gui_b) {
      auto& a_name = $gui.name_for(gui_a);
      auto& b_name = $gui.name_for(gui_b);
      auto player = $level.world->get_the<components::Player>();
      System::inventory_swap($level, player.entity, a_name, b_name);
    }

    update();
  }

  bool StatusUI::occupied(guecs::Entity slot) {
    auto player = $level.world->get_the<components::Player>();
    return System::inventory_occupied($level, player.entity, $gui.name_for(slot));
  }
}
