#pragma once
#include "guecs/lel.hpp"
#include <string>
#include <memory>
#include <functional>
#include <any>
#include <queue>
#include <typeindex>
#include <unordered_map>
#include "guecs/theme.hpp"
#include "guecs/sfml/components.hpp"
#include <cassert>

namespace guecs {
  using std::shared_ptr, std::wstring, std::string;

  using Entity = unsigned long;

  using EntityMap = std::unordered_map<Entity, size_t>;

  template <typename T>
    struct ComponentStorage {
      std::vector<T> data;
      std::queue<size_t> free_indices;
    };

  struct Clickable {
    /* This is actually called by UI::mouse and passed the entity ID of the
     * button pressed so you can interact with it in the event handler.
     */
    std::function<void(Entity ent, std::any data)> action;
  };

  struct ActionData {
    std::any data;
  };

  struct CellName {
    string name;
  };


  class UI {
    public:
      Entity MAIN = 0;
      unsigned long entity_count = 1;
      std::unordered_map<std::type_index, EntityMap> $components;
      std::unordered_map<std::type_index, std::any> $component_storages;
      std::unordered_map<string, Entity> $name_ents;
      shared_ptr<sf::Font> $font = nullptr;
      lel::Parser $parser;
      string $grid = "";

      UI();

      void position(int x, int y, int width, int height);
      sf::Vector2f get_position();
      sf::Vector2f get_size();
      void layout(const string& grid);
      Entity init_entity(const string& name);
      Entity entity(const string& name);
      Entity entity(const string& name, int id);

      inline lel::CellMap& cells() {
        return $parser.cells;
      }

      void init();
      void render(sf::RenderWindow& window);
      bool mouse(float x, float y, bool hover);
      void click_on(const string& name, bool required=false);
      void click_on(Entity slot_id);
      void debug_layout(sf::RenderWindow& window);

      Entity entity() { return ++entity_count; }

      template <typename Comp>
        size_t make_component() {
          auto &storage = component_storage_for<Comp>();
          size_t index;

          if(!storage.free_indices.empty()) {
            index = storage.free_indices.front();
            storage.free_indices.pop();
          } else {
            storage.data.emplace_back();
            index = storage.data.size() - 1;
          }

          return index;
        }

      template <typename Comp>
        ComponentStorage<Comp> &component_storage_for() {
          auto type_index = std::type_index(typeid(Comp));
          $component_storages.try_emplace(type_index, ComponentStorage<Comp>{});
          return std::any_cast<ComponentStorage<Comp> &>(
              $component_storages.at(type_index));
        }

      template <typename Comp>
        EntityMap &entity_map_for() {
          return $components[std::type_index(typeid(Comp))];
        }

      template <typename Comp>
        void set(Entity ent, Comp val) {
          EntityMap &map = entity_map_for<Comp>();

          if(has<Comp>(ent)) {
            get<Comp>(ent) = val;
            return;
          }

          map.insert_or_assign(ent, make_component<Comp>());
          get<Comp>(ent) = val;
        }

      template <typename Comp>
        Comp& get(Entity ent) {
          EntityMap &map = entity_map_for<Comp>();
          auto &storage = component_storage_for<Comp>();
          auto index = map.at(ent);
          return storage.data[index];
        }

      template <typename Comp>
        Comp* get_if(Entity entity) {
          EntityMap &map = entity_map_for<Comp>();
          auto &storage = component_storage_for<Comp>();
          if(map.contains(entity)) {
            auto index = map.at(entity);
            return &storage.data[index];
          } else {
            return nullptr;
          }
        }

      template <typename Comp>
        bool has(Entity ent) {
          EntityMap &map = entity_map_for<Comp>();
          return map.contains(ent);
        }

      template <typename Comp>
        void remove(Entity ent) {
          EntityMap &map = entity_map_for<Comp>();

          if(map.contains(ent)) {
            size_t index = map.at(ent);
            component_storage_for<Comp>().free_indices.push(index);
          }

          map.erase(ent);
        }

      template <typename Comp>
        void query(std::function<void(Entity, Comp &)> cb) {
          EntityMap &map = entity_map_for<Comp>();

          for(auto &[entity, index] : map) {
            cb(entity, get<Comp>(entity));
          }
        }

      template <typename CompA, typename CompB>
        void query(std::function<void(Entity, CompA &, CompB &)> cb) {
          EntityMap &map_a = entity_map_for<CompA>();
          EntityMap &map_b = entity_map_for<CompB>();

          for(auto &[entity, index_a] : map_a) {
            if(map_b.contains(entity)) {
              cb(entity, get<CompA>(entity), get<CompB>(entity));
            }
          }
        }

      template <typename Comp>
        void set_init(Entity ent, Comp val) {
          assert(has<lel::Cell>(ent) && "WRONG! slot is missing its cell?!");
          auto& cell = get<lel::Cell>(ent);
          val.init(cell);
          set<Comp>(ent, val);
        }

      template <typename Comp>
        void do_if(Entity ent, std::function<void(Comp &)> cb) {
          if(has<Comp>(ent)) {
            cb(get<Comp>(ent));
          }
        }

      lel::Cell& cell_for(Entity ent) {
        return get<lel::Cell>(ent);
      }

      lel::Cell& cell_for(const string& name) {
        Entity ent = entity(name);
        return get<lel::Cell>(ent);
      }

      // BUG: close could just be remove with overload
      template <typename Comp>
        void close(string region) {
          auto ent = entity(region);
          if(has<Comp>(ent)) {
            remove<Comp>(ent);
          }
        }

      template<typename T>
      void render_helper(sf::RenderWindow& window, T& target, sf::Shader *shader_ptr) {
        window.draw(*target, shader_ptr);
      }

      sf::Shader* find_shader(Entity ent, bool is_shape);

      void show_sprite(const string& region, const string& sprite_name);
      void show_text(const string& region, const wstring& content);
      void show_label(const string& region, const wstring& content);
  };


  wstring to_wstring(const string& str);

}
