#include "ritual_ui.hpp"
#include "components.hpp"
#include "guecs.hpp"
#include "rand.hpp"
#include "animation.hpp"
#include "rand.hpp"

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

  RitualUI::RitualUI(GameLevel level) :
    $level(level)
  {
    $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
    $gui.layout(
        "[_]"
        "[inv_slot9 | inv_slot10 | inv_slot11| inv_slot12]"
        "[inv_slot13 | inv_slot14 | inv_slot15| inv_slot16]"
        "[inv_slot17 | inv_slot18 | inv_slot19| inv_slot20]"
        "[inv_slot21 | inv_slot22 | inv_slot23| inv_slot24]"
        "[*%(100,600)circle_area]"
        "[_]"
        "[_]"
        "[_]"
        "[_]"
        "[_]"
        "[ ritual_ui ]");
  }

  void RitualUI::init() {
    std::vector<std::string> junk_list{
      {"chess_pawn"},
      {"dirty_kerchief"},
      {"mushroom"},
      {"pocket_watch"},
      {"rusty_nails"},
      {"severed_finger"}
    };

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

      if(name == "circle_area") {
        $gui.set<Sprite>(button, {"the_ritual_circle"});
        $gui.set<Clickable>(button, {
            [&](auto ent, auto){ ritual_circle_clicked(ent); }
        });
      } else if(name.starts_with("inv_slot")) {
        $gui.set<Sprite>(button, {
            fmt::format("{}-64", junk_list[button % junk_list.size()])});
        $gui.set<Clickable>(button, {
            [&](auto ent, auto){ inv_slot_clicked(ent); }
        });
      } else if(name == "ritual_ui") {
        $gui.set<Clickable>(button, {
            [&](auto, auto){ toggle(); }
        });
      }
    }

    $ritual_ui = textures::get("ritual_crafting_area");
    $ritual_ui.sprite->setPosition({0,0});
    $ritual_ui.sprite->setTextureRect($ritual_closed_rect);
    $ritual_state = RitualUIState::CLOSED;
    $ritual_anim = animation::load("ritual_blanket");

    $gui.init();

  }

  bool RitualUI::is_open() {
    return $ritual_state != RitualUIState::CLOSED;
  }

  void RitualUI::inv_slot_clicked(DinkyECS::Entity ent) {
    if($gui.has<Sprite>(ent)) {
      auto& bs = $gui.get<Sprite>(ent);
      auto ritual_circle = $gui.entity("circle_area");
      auto& ritual_cell = $gui.cell_for(ritual_circle);
      dbc::log(fmt::format("inv_slot clicked {}", bs.name));

      int inner_x = ritual_cell.x + ritual_cell.x / 2;
      int inner_y = ritual_cell.y + ritual_cell.y / 2;

      float x = Random::uniform(inner_x, inner_x + ritual_cell.w / 2);
      float y = Random::uniform(inner_y, inner_y + ritual_cell.h / 2);
      bs.sprite->setPosition({float(x), float(y)});
    }
  }

  void RitualUI::reset_inv_positions() {
    auto ritual_circle = $gui.entity("circle_area");

    $gui.world().query<lel::Cell, Sprite>(
      [&](const auto ent, auto &cell, auto &bs) {
        if(ent == ritual_circle) {
          bs.sprite->setColor({255,255,255,255});
          bs.sprite->setRotation(sf::degrees(0.0));
        } else {
          bs.sprite->setPosition({(float)cell.x, (float)cell.y});
        }
    });
  }

  void RitualUI::ritual_circle_clicked(DinkyECS::Entity ent) {
    auto cell = $gui.cell_for(ent);
    auto& bs = $gui.get<Sprite>(ent);
    bs.sprite->setColor({200, 0, 0});
    animation::center(*bs.sprite, {(float)cell.x, (float)cell.y});
    animation::rotate(*bs.sprite, 20.0);
  }

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

  void RitualUI::toggle() {
    using enum RitualUIState;

    if($ritual_state == OPEN) {
      $ritual_state = CLOSING;
    } else if($ritual_state == CLOSED) {
      $ritual_state = OPENING;
      $ritual_anim.play();
    }
  }

  /* WARNING: This is really not the greatest way to do this.
   * look in status_ui.update_level()
   * */
  void RitualUI::update() {
    dbc::log("RITUAL UPDATE NOT IMPLEMENTED");
  }

  void RitualUI::render(sf::RenderWindow &window) {
    using enum RitualUIState;

    if($ritual_state == OPENING) {
      if(!animation::apply($ritual_anim, $ritual_ui)) {
        $ritual_state = OPEN;
      }
    } else if($ritual_state == CLOSING) {
      reset_inv_positions();
      $ritual_ui.sprite->setTextureRect($ritual_closed_rect);
      $ritual_state = CLOSED;
    }

    window.draw(*$ritual_ui.sprite);
    if($ritual_state == OPEN) $gui.render(window);
  }
}