#include "textures.hpp"
#include <SFML/Graphics/Image.hpp>
#include "dbc.hpp"
#include <fmt/core.h>
#include "config.hpp"
#include "constants.hpp"
#include <memory>

namespace textures {
  using std::shared_ptr, std::make_shared;

  static TextureManager TMGR;
  static bool initialized = false;

  void load_sprites() {
    Config assets("assets/config.json");

    for(auto& [name, settings] : assets["sprites"].items()) {
      auto texture = make_shared<sf::Texture>(settings["path"]);

      texture->setSmooth(assets["graphics"]["smooth_textures"]);
      auto sprite = make_shared<sf::Sprite>(*texture);

      int width = settings["frame_width"];
      int height = settings["frame_height"];
      sprite->setTextureRect({{0,0}, {width, height}});

      TMGR.sprite_textures.try_emplace(name, sprite, texture);
    }
  }

  inline void resize_shit(size_t size) {
    TMGR.surfaces.resize(size);
    TMGR.ceilings.resize(size);
    TMGR.map_tile_set.resize(size);
    TMGR.ambient_light.resize(size);
  }

  void load_tiles() {
    Config assets("assets/tiles.json");
    auto &tiles = assets.json();

    resize_shit(tiles.size());

    for(auto &el : tiles.items()) {
      auto &config = el.value();
      const std::string& texture_fname = config["texture"];
      size_t surface_i = config["id"];
      TMGR.name_to_id.insert_or_assign(el.key(), surface_i);

      if(surface_i >= tiles.size()) {
        resize_shit(surface_i + 1);
      }

      TMGR.map_tile_set[surface_i] = config["display"];
      TMGR.ambient_light[surface_i] = config["light"];
      TMGR.surfaces[surface_i] = load_image(texture_fname);

      // NOTE: ceilings defaults to 0 which is floor texture so only need to update
      if(config.contains("ceiling")) {
        const std::string& name = config["ceiling"];

        dbc::check(tiles.contains(name), fmt::format("invalid ceiling name {} in tile config {}", name, (std::string)el.key()));

        auto& ceiling = tiles[name];
        TMGR.ceilings[surface_i] = ceiling["id"];
      }
    }
  }

  void init() {
    if(!initialized) {
      load_tiles();
      load_sprites();
      initialized = true;
    }
  }

  SpriteTexture get(const std::string& name) {
    dbc::check(initialized, "you forgot to call textures::init()");
    dbc::check(TMGR.sprite_textures.contains(name),
        fmt::format("!!!!! texture pack does not contain {} sprite", name));

    auto result = TMGR.sprite_textures.at(name);

    dbc::check(result.sprite != nullptr,
        fmt::format("bad sprite from textures::get named {}", name));
    dbc::check(result.texture != nullptr,
        fmt::format("bad texture from textures::get named {}", name));

    return result;
  }

  sf::Image load_image(const std::string& filename) {
    sf::Image texture;
    bool good = texture.loadFromFile(filename);
    dbc::check(good, fmt::format("failed to load {}", filename));
    return texture;
  }

  std::vector<int>& get_ambient_light() {
    return TMGR.ambient_light;
  }

  std::vector<wchar_t>& get_map_tile_set() {
    return TMGR.map_tile_set;
  }

  const uint32_t* get_surface(size_t num) {
    return (const uint32_t *)TMGR.surfaces[num].getPixelsPtr();
  }

  const uint32_t* get_ceiling(size_t num) {
    size_t ceiling_num = TMGR.ceilings[num];
    return (const uint32_t *)TMGR.surfaces[ceiling_num].getPixelsPtr();
  }

  size_t get_id(const std::string& name) {
    dbc::check(TMGR.name_to_id.contains(name),
        fmt::format("there is no texture named {} in tiles.json", name));
    return TMGR.name_to_id.at(name);
  }
};