#include "gui/ritual_ui.hpp"
#include "components.hpp"
#include <guecs/ui.hpp>
#include "rand.hpp"
#include "animation.hpp"
#include "rand.hpp"
#include "sound.hpp"
#include "events.hpp"

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

    UI::UI(GameLevel level) :
      $level(level),
      $blanket($level.world->get_the<::ritual::Blanket>())
    {
      $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
      $gui.layout(
          "[_]"
          "[inv_slot0 | inv_slot1 | inv_slot2| inv_slot3]"
          "[inv_slot4 | inv_slot5 | inv_slot6| inv_slot7]"
          "[inv_slot8 | inv_slot9 | inv_slot10| inv_slot11]"
          "[inv_slot12 | inv_slot13 | inv_slot14| inv_slot15]"
          "[inv_slot16 | inv_slot17 | inv_slot18| inv_slot19]"
          "[_                      |=*%(200,400)result_text|_]"
          "[*%(100,200)result_image|_                     |_]"
          "[_|_|_]"
          "[_|_|_]"
          "[_]"
          "[ ritual_ui ]");
    }

    void UI::event(Event ev, std::any data) {
      switch($state) {
        FSM_STATE(State, START, ev);
        FSM_STATE(State, OPENED, ev, data);
        FSM_STATE(State, CRAFTING, ev, data);
        FSM_STATE(State, CLOSED, ev);
        FSM_STATE(State, OPENING, ev);
        FSM_STATE(State, CLOSING, ev);
      }
    }

    void UI::START(Event) {
      $ritual_ui = textures::get_sprite("ritual_crafting_area");
      $ritual_ui.sprite->setPosition($gui.get_position());
      $ritual_ui.sprite->setTextureRect($ritual_closed_rect);
      $ritual_anim = animation::load("ritual_blanket");

      auto open_close_toggle = $gui.entity("ritual_ui");
      $gui.set<Clickable>(open_close_toggle, {
        [&](auto, auto){ event(Event::TOGGLE); }
      });


      $craft_state = $ritual_engine.start();
      $gui.init();

      state(State::CLOSED);
    }

    void UI::OPENED(Event ev, std::any data) {
      if(ev == Event::TOGGLE) {
        clear_blanket();
        state(State::CLOSING);
      } else if(ev == Event::SELECT) {
        // do this before transitioning
        state(State::CRAFTING);
        UI::CRAFTING(ev, data);
      }
    }

    void UI::CRAFTING(Event ev, std::any data) {
      if(ev == Event::TOGGLE) {
        clear_blanket();
        state(State::CLOSING);
      } else if(ev == Event::COMBINE) {
        complete_combine();
      } else if(ev == Event::SELECT) {
        dbc::check(data.has_value(), "OPENED state given SELECT with no data");
        auto pair = std::any_cast<SelectedItem>(data);
        select_item(pair);
        update_selection_state();
      }
    }


    void UI::CLOSED(Event ev) {
      if(ev == Event::TOGGLE) {
        $ritual_anim.play();
        load_blanket();
        state(State::OPENING);
      }
    }

    void UI::OPENING(Event ev) {
      if(ev == Event::TICK) {
        if(!animation::apply($ritual_anim, $ritual_ui)) {
          state(State::OPENED);
        }
      }
    }

    void UI::CLOSING(Event ev) {
      if(ev == Event::TICK) {
        $ritual_ui.sprite->setTextureRect($ritual_closed_rect);
        state(State::CLOSED);
      }
    }

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

    bool UI::is_open() {
      return !in_state(State::CLOSED);
    }

    void UI::render(sf::RenderWindow &window) {
      event(Event::TICK);

      window.draw(*$ritual_ui.sprite);

      if(in_state(State::OPENED) || in_state(State::CRAFTING)) {
        $gui.render(window);
        // $gui.debug_layout(window);
      }
    }

    void UI::clear_blanket() {
      for(int i = 0; i < INV_SLOTS; i++) {
        auto slot_id = $gui.entity("inv_slot", i);

        if($gui.has<Sprite>(slot_id)) {
          $gui.remove<Sprite>(slot_id);
          $gui.remove<Clickable>(slot_id);
        }
      }

      $blanket.reset();
    }

    void UI::select_item(SelectedItem pair) {
      auto& sprite = $gui.get<Sprite>(pair.slot_id);

      if($blanket.is_selected(pair.item_id)) {
        $blanket.deselect(pair.item_id);
        sprite.sprite->setColor({255, 255, 255, 255});
      } else {
        $blanket.select(pair.item_id);
        sprite.sprite->setColor({255, 200, 200, 200});
      }
    }

    void UI::update_selection_state() {
      if($blanket.no_selections()) {
        clear_craft_result();
        state(State::OPENED);
      } else {
        run_crafting_engine();
        show_craft_result();
      }
    }

    void UI::load_blanket() {
      // update the list of available items
      int i = 0;
      for(auto& [item_id, item] : $blanket.contents) {
        auto slot_id = $gui.entity("inv_slot", i++);

        $gui.set_init<Sprite>(slot_id, {item});
        $gui.set<Clickable>(slot_id, {
          [&, slot_id, item_id](auto, auto) {
            auto data = std::make_any<SelectedItem>(slot_id, item_id);
            event(Event::SELECT, data);
          }
        });
      }

      for(; i < INV_SLOTS; i++) {
        auto slot_id = $gui.entity("inv_slot", i);
        $gui.remove<Sprite>(slot_id);
        $gui.remove<Clickable>(slot_id);
      }
    }

    void UI::complete_combine() {
      if($craft_state.is_combined()) {
        auto ritual = $ritual_engine.finalize($craft_state);
        auto& belt = $level.world->get_the<::ritual::Belt>();
        belt.equip(belt.next(), ritual);
        $level.world->send<Events::GUI>(Events::GUI::NEW_RITUAL, $level.player, {});
        $blanket.consume_crafting();
        clear_craft_result();

        load_blanket();
        state(State::OPENED);
      }
    }

    void UI::run_crafting_engine() {
      $craft_state.reset();

      for(auto [item_id, setting] : $blanket.selected) {
        auto& item = $blanket.get(item_id);
        $ritual_engine.load_junk($craft_state, item);
      }

      $ritual_engine.plan($craft_state);
    }

    void UI::show_craft_result() {
      using enum ::ritual::Element;
      auto ritual = $ritual_engine.finalize($craft_state);
      auto combine = $gui.entity("result_image");

      if($craft_state.is_combined()) {
        $gui.show_text("result_text", L"This might work...");

        switch(ritual.element) {
          case FIRE:
            $gui.show_sprite("result_image", "broken_yoyo");
            break;
          case LIGHTNING:
            $gui.show_sprite("result_image", "pocket_watch");
            break;
          default:
            $gui.show_sprite("result_image", "severed_finger");
        }

        $gui.set<Clickable>(combine, {
          [&](auto, auto){ event(Event::COMBINE); }
        });
      } else {
        $gui.show_text("result_text", L"That won't work.");
        $gui.show_sprite("result_image", "dubious_combination");
        $gui.remove<Clickable>(combine);
        return;
      }
    }

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

    void UI::clear_craft_result() {
      $blanket.reset();
      $gui.close<Text>("result_text");
      $gui.close<Sprite>("result_image");
    }
  }
}
