#include "combat_ui.hpp"
#include "constants.hpp"
#include "color.hpp"
#include "rituals.hpp"
#include <fmt/xchar.h>

namespace gui {
  using namespace guecs;

  CombatUI::CombatUI(GameLevel level) :
      $level(level)
  {
    $gui.position(COMBAT_UI_X, COMBAT_UI_Y, COMBAT_UI_WIDTH, COMBAT_UI_HEIGHT);
    $gui.layout(
        "[*%(100,150)button_0 | *%(100,150)button_1 | *%(100,150)button_2 | *%(100,150)button_3]");
  }

  void CombatUI::make_button(std::string name, std::wstring label, Events::GUI event, int action) {
    auto button = $gui.entity(name);
    $gui.set<Sprite>(button, {"leather_pouch-128"});
    $gui.set<Rectangle>(button, {});
    $gui.set<Sound>(button, {"ui_click"});
    $gui.set<Label>(button, {label});
    $gui.set<Effect>(button, {.duration=0.1f});
    $gui.set<Clickable>(button,
        guecs::make_action(*$level.world, event, {action}));
  }

  void CombatUI::init() {
    $gui.world().set_the<Background>({$gui.$parser, ColorValue::DARK_MID});
    auto& the_belt = $level.world->get<combat::RitualBelt>($level.player);

    for(int slot = 0; slot < 4; slot++) {
      if(the_belt.has(slot)) {
        std::string name = fmt::format("button_{}", slot);
        std::wstring label = fmt::format(L"Attack {}", slot+1);
        make_button(name, label, Events::GUI::ATTACK, slot);
      }
    }

    $gui.init();
  }

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

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

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