#include "gui/loot_ui.hpp"
#include "constants.hpp"
#include <fmt/xchar.h>
#include "systems.hpp"

namespace gui {
  using namespace guecs;

  LootUI::LootUI(GameLevel level) :
      $level(level),
      $temp_loot($level.world->entity()),
      $target($temp_loot)
  {
    $gui.position(RAY_VIEW_X+RAY_VIEW_WIDTH/2-200,
        RAY_VIEW_Y+RAY_VIEW_HEIGHT/2-200, 400, 400);

    $gui.layout(
        "[=item_0 | =item_1 |=item_2 |=item_3 ]"
        "[=item_4 | =item_5 |=item_6 |=item_7 ]"
        "[=item_8 | =item_9 |=item_10|=item_11]"
        "[=item_12| =item_13|=item_14|=item_15 ]"
        "[ =take_all | =close| =destroy]");

    $level.world->set<inventory::Model>($temp_loot, {});
    $level.world->make_constant($temp_loot);
  }

  void LootUI::make_button(const std::string &name, const std::wstring& label, Events::GUI event) {

    auto button = $gui.entity(name);
    $gui.set<guecs::Rectangle>(button, {});
    $gui.set<guecs::Text>(button, {label});
    $gui.set<guecs::Clickable>(button,
        guecs::make_action($level, event));
  }

  void LootUI::init() {
    using guecs::THEME;
    auto bg_color = THEME.DARK_LIGHT;
    bg_color.a = 140;
    $gui.set<Background>($gui.MAIN, {$gui.$parser, bg_color});

    make_button("close", L"CLOSE", Events::GUI::LOOT_CLOSE);
    make_button("take_all", L"TAKE ALL", Events::GUI::LOOT_CLOSE);
    make_button("destroy", L"DESTROY", Events::GUI::LOOT_CLOSE);

    for(int i = 0; i < INV_SLOTS; i++) {
      auto name = fmt::format("item_{}", i);
      auto id = $gui.entity(name);

      $gui.set<guecs::Rectangle>(id, {THEME.PADDING,
           THEME.TRANSPARENT, THEME.LIGHT_MID });
      $gui.set<guecs::Effect>(id, {0.4f, "ui_shader"});
      $gui.set<guecs::Clickable>(id, {
          guecs::make_action($level, Events::GUI::LOOT_SELECT, {id})
      });
    }

    $gui.init();
    update();
  }

  void LootUI::update() {
    dbc::check($level.world->has<inventory::Model>($target),
        "update called but $target isn't in world");

    auto& contents = $level.world->get<inventory::Model>($target);

    for(size_t i = 0; i < INV_SLOTS; i++) {
      auto id = $gui.entity("item_", int(i));
      auto& slot_name = $gui.name_for(id);

      if(contents.has(slot_name)) {
        auto item = contents.get(slot_name);
        dbc::check($level.world->has<components::Sprite>(item),
            "item in inventory UI doesn't exist in world. New level?");
        auto& sprite = $level.world->get<components::Sprite>(item);
        $gui.set_init<guecs::Icon>(id, {sprite.name});

        guecs::GrabSource grabber{
            item, [&, id]() { return remove_slot(id); }};
        grabber.setSprite($gui, id);
        $gui.set<guecs::GrabSource>(id, grabber);
      } else {
        // BUG: fix remove so it's safe to call on empty
        if($gui.has<guecs::GrabSource>(id)) {
          $gui.remove<guecs::Icon>(id);
          $gui.remove<guecs::GrabSource>(id);
        }

        $gui.set<guecs::DropTarget>(id, {
           [&, id](DinkyECS::Entity world_entity) -> bool { return place_slot(id, world_entity); }
        });
      }
    }
  }

  void LootUI::remove_slot(guecs::Entity slot_id) {
    auto& name = $gui.name_for(slot_id);
    fmt::println("LootUI remove slot inv::Model id={} slot={}", $target, name);
    System::remove_from_container(*$level.world, $target, name);
    update();
  }

  bool LootUI::place_slot(guecs::Entity id, DinkyECS::Entity world_entity) {
    fmt::println("LootUI target={} placing world entity {} in slot id {}",
        $target, id, world_entity);
    auto& name = $gui.name_for(id);

    bool worked = System::place_in_container(*$level.world, $target, name, world_entity);
    if(worked) update();
    return worked;
  }

  void LootUI::render(sf::RenderWindow& window) {
    $gui.render(window);
  }

  void LootUI::update_level(GameLevel &level) {
    $level = level;
    init();
  }

  void LootUI::add_loose_item(DinkyECS::Entity entity) {
    System::place_in_container(*$level.world, $temp_loot, "item_0", entity);
    set_target($temp_loot);
    update();
  }

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

  bool LootUI::mouse(float x, float y, bool hover) {
    return $gui.mouse(x, y, hover);
  }

  bool LootUI::occupied(guecs::Entity slot) {
    return System::inventory_occupied($level, $target, $gui.name_for(slot));
  }

  void LootUI::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);
      System::inventory_swap($level, $target, a_name, b_name);
    }

    update();
  }
}
