#include <fmt/core.h>
#include "dbc.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "constants.hpp"
#include "config.hpp"
#include <filesystem>
#include "shiterator.hpp"
#include <functional>
#include <iostream>
#include "textures.hpp"
#include "palette.hpp"

namespace fs = std::filesystem;
constexpr const int TILE_COUNT=10;
constexpr const sf::Color DEFAULT_COLOR{255, 255, 255, 255};
using namespace nlohmann;

using namespace shiterator;

using MapRow = BaseRow<wchar_t>;
using MapGrid = Base<wchar_t>;

using BoolRow = BaseRow<bool>;
using BoolGrid = Base<bool>;

struct MapConfig {
  MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT);
  BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
  std::unordered_map<wchar_t, sf::Color> colors;
  std::unordered_map<wchar_t, sf::Color> backgrounds;
  std::unordered_map<wchar_t, std::string> names;
  each_row_t<MapGrid> it{map};
};

struct MapTileBuilder {
  unsigned int $font_size = 20;
  sf::Glyph $glyph;
  sf::Font $font{FONT_FILE_NAME};
  std::shared_ptr<sf::RenderTexture> $render = nullptr;
  sf::Vector2i $size;
  sf::Vector2i $image_size;
  sf::RenderTexture $temp_render;

  MapTileBuilder(size_t x, size_t y) :
    $size(x, y),
    $image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT),
    $temp_render({(unsigned int)$size.x, (unsigned int)$size.y})
  {
    $font.setSmooth(false);
  }

  void best_size(wchar_t for_char, bool centered) {
    float factor = centered ? 0.8f : 1.0f;
    sf::Vector2i adjusted_size = {int($size.x * factor), int($size.y * factor)};
    $font_size = 20; // reset the size
    // fit the glyph in our box height
    auto temp = $font.getGlyph(for_char, $font_size, false);
    auto temp_size = $font_size;

    while(temp.textureRect.size.y <= adjusted_size.y
        && temp.textureRect.size.x <= adjusted_size.x)
    {
      $glyph = temp;
      $font_size = temp_size;

      temp_size++;
      temp = $font.getGlyph(for_char, temp_size, false);
    }
  }

  void save_image(std::string icon_path) {
    dbc::check($render != nullptr, "You have to call run() first.");
    fs::path out_path{icon_path};

    if(fs::exists(out_path)) {
      fs::remove(out_path);
    }

    sf::Image out_img = $render->getTexture().copyToImage();

    bool worked = out_img.saveToFile(out_path);
    dbc::check(worked, "Failed to write screenshot.png");
  }

  void run_real_textures(MapConfig &config) {
    textures::init();
    sf::Vector2u crop{$size.x * (unsigned int)config.it.width, ($size.y) * ((unsigned int)config.it.y + 1)};
    $render = std::make_shared<sf::RenderTexture>(crop);
    $render->clear({0,0,0,0});

    $render->setSmooth(false);
    sf::Vector2f cell_pos{0.0f,0.0f};

    for(each_row_t<MapGrid> it{config.map}; it.next();) {
      wchar_t display_char = config.map[it.y][it.x];
      // stop when there's no more cells set
      if(display_char == 0) break;

      cell_pos.x = it.x * $size.x;
      cell_pos.y = it.y * $size.y;

      auto& name = config.names.at(display_char);
      auto id = textures::get_id(name);
      auto& img = textures::get_surface_img(id);
      auto img_size = img.getSize();

      sf::Texture surface{img};

      sf::Vector2f scale{float($size.x) / float(img_size.x),
        float($size.y) / float(img_size.y)};

      sf::Sprite sprite{surface};
      sprite.setScale(scale);
      sprite.setPosition(cell_pos);
      $render->draw(sprite);
      $render->display();
    }
  }

  void run(MapConfig& config) {
    sf::Vector2u crop{$size.x * (unsigned int)config.it.width, $size.y * ((unsigned int)config.it.y+1)};
    $render = std::make_shared<sf::RenderTexture>(crop);
    $render->clear({0,0,0,0});

    $render->setSmooth(false);
    sf::Vector2f cell_pos{0.0f,0.0f};
    sf::RectangleShape background({(float)$size.x, (float)$size.y});

    for(each_row_t<MapGrid> it{config.map}; it.next();) {
      // a 0 slot means we're done
      if(config.map[it.y][it.x] == 0) break;

      cell_pos.x = it.x * $size.x;
      cell_pos.y = it.y * $size.y;
      bool is_centered = config.centered[it.y][it.x];

      wchar_t display_char = config.map[it.y][it.x];
      std::wstring content{display_char};
      auto bg = config.backgrounds.at(display_char);
      auto fg = config.colors.at(display_char);

      best_size(display_char, is_centered);

      sf::Text icon{$font, content, $font_size};
      icon.setFillColor({255, 255, 255, 255});
      $temp_render.draw(icon);
      $temp_render.clear({0,0,0,0});

      auto& font_texture = $font.getTexture($font_size);
      sf::Sprite sprite{font_texture, $glyph.textureRect};
      auto t_size = $glyph.textureRect.size;

      dbc::check($size.x - t_size.x >= 0, "font too big on x");
      dbc::check($size.y - t_size.y >= 0, "font too big on y");

      // draw the background first
      background.setFillColor(bg);

      if(is_centered) {
        sf::Vector2f center{
          float(($size.x - t_size.x) / 2),
          float(($size.y - t_size.y) / 2)};

        sprite.setScale({1.0f, 1.0f});
        sprite.setPosition({cell_pos.x + center.x, cell_pos.y + center.y});
      } else {
        sf::Vector2f scale{float($size.x) / float(t_size.x), float($size.y) / float(t_size.y)};
        sprite.setScale(scale);
        sprite.setPosition(cell_pos);
        background.setPosition(cell_pos);
      }

      sprite.setColor(fg);

      $render->draw(background);
      $render->draw(sprite);
      $render->display();
    }
  }

  void save_config(MapConfig& config, const std::string &path) {
    (void)path;
    json result = json::array();

    for(each_row_t<MapGrid> it{config.map}; it.next();) {
      if(config.map[it.y][it.x] == 0) break;

      json val;

      val["x"] = $size.x * it.x;
      val["y"] = $size.y * it.y;
      val["display"] = (int)config.map[it.y][it.x];
      val["centered"] = config.centered[it.y][it.x];

      result.push_back(val);
    }

    std::ofstream o(path, std::ios::out | std::ios::binary);
    o << std::setw(4) << result << std::endl;
  }
};

void load_config(MapConfig& config, bool is_centered, std::string path, std::function<json&(json&)> finder)
{
  Config tiles(path);

  for(auto [key, val] : tiles.json().items()) {
    config.it.next();
    auto data = finder(val);
    wchar_t display = data["display"];
    config.map[config.it.y][config.it.x] = display;
    config.centered[config.it.y][config.it.x] = is_centered;
    config.names.insert_or_assign(display, key);

    dbc::check(!config.colors.contains(display),
        fmt::format("duplicate color for display={} key={}",
          (int)display, (std::string)key));

    dbc::check(data.contains("foreground"),
        fmt::format("{} has no foreground", std::string(key)));

    auto fg = palette::get(data["foreground"]);
    config.colors.insert_or_assign(display, fg);

    dbc::check(data.contains("background"),
      fmt::format("{} has no background", std::string(key)));

    auto bg = palette::get(data["background"]);
    config.backgrounds.insert_or_assign(display, bg);
  }
}

json& component_display(json& val)  {
  auto& components = val["components"];

  for(auto& comp : components) {
    if(comp["_type"] == "Tile") {
      return comp;
    }
  }

  dbc::log("BAD CHAR");
  return val;
}

int main() {
  palette::init();
  MapConfig config;

  load_config(config, false, "./assets/tiles.json", [](json& val) -> json& {
      return val;
  });

  load_config(config, true, "./assets/items.json", component_display);
  load_config(config, true, "./assets/devices.json", component_display);
  load_config(config, true, "./assets/enemies.json", component_display);

  fmt::println("-----------------------------------------");
  MapTileBuilder builder(ICONGEN_MAP_TILE_DIM, ICONGEN_MAP_TILE_DIM);
  builder.run(config);

  builder.save_image("./assets/map_tiles.png");
  builder.save_config(config, "./assets/map_tiles.json");
  return 0;
}
