#pragma once
#include "levelmanager.hpp"
#include "constants.hpp"
#include <deque>
#include "textures.hpp"
#include <guecs/ui.hpp>
#include "rituals.hpp"
#include "simplefsm.hpp"

namespace gui {
  namespace ritual {
    enum class State {
      START=0,
      OPENED=1,
      CLOSED=2,
      OPENING=3,
      CLOSING=4,
      CRAFTING=5
    };

    enum class Event {
      STARTED=0,
      TOGGLE=1,
      TICK=2,
      SELECT=3,
      COMBINE=4
    };

    struct SelectedItem {
      guecs::Entity slot_id;
      ::ritual::Entity item_id;
    };

    class UI : public DeadSimpleFSM<State, Event> {
      public:
        sf::IntRect $ritual_closed_rect{{0,0},{380,720}};
        sf::IntRect $ritual_open_rect{{380 * 2,0},{380,720}};
        components::Animation $ritual_anim;
        guecs::UI $gui;
        GameLevel $level;
        textures::SpriteTexture $ritual_ui;
        ::ritual::Blanket& $blanket;
        ::ritual::Engine $ritual_engine;
        ::ritual::CraftingState $craft_state;

        UI(GameLevel level);

        void event(Event ev, std::any data={});
        void START(Event);
        void OPENED(Event, std::any data={});
        void CRAFTING(Event, std::any data={});
        void CLOSED(Event);
        void OPENING(Event);
        void CLOSING(Event);

        bool mouse(float x, float y, bool hover);
        void render(sf::RenderWindow &window);
        bool is_open();
        void load_blanket();
        void clear_blanket();
        void select_item(SelectedItem pair);
        void show_craft_result();
        void clear_craft_result();
        void run_crafting_engine();
        void complete_combine();
        void update_selection_state();
        void update_level(GameLevel &level);
    };
  }
}
