#include "map_view.hpp"
#include <functional>
#include <string>
#include "systems.hpp"

namespace gui {
  using namespace components;
  using ftxui::Color;

  MapViewUI::MapViewUI(GameLevel &level) :
    Panel(0, 0, 0, 0, true),
    $level(level)
  {
  }

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

  void MapViewUI::draw_map() {
    const auto& debug = $level.world->get_the<Debug>();
    const auto& player = $level.world->get_the<Player>();
    const auto& player_position = $level.world->get<Position>(player.entity);
    Point start = $level.map->center_camera(player_position.location, width, height);
    auto &tiles = $level.map->tiles();
    auto &paths = $level.map->paths();
    auto &lighting = $level.lights->lighting();

    // WARN: this is exploiting that -1 in size_t becomes largest
    size_t end_x = std::min(size_t(width), $level.map->width() - start.x);
    size_t end_y = std::min(size_t(height), $level.map->height() - start.y);

    for(size_t y = 0; y < end_y; ++y) {
      for(size_t x = 0; x < end_x; ++x)
      {
        const Tile& tile = tiles.at(start.x+x, start.y+y);
        // light value is an integer that's a percent
        float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT;
        int dnum = debug.PATHS ? paths[start.y+y][start.x+x] : WALL_PATH_LIMIT;

        if(debug.PATHS && dnum != WALL_PATH_LIMIT) {
          string num = dnum > 15 ? "*" : fmt::format("{:x}", dnum);

          $canvas.DrawText(x * 2, y * 4, num, [dnum, tile](auto &pixel) {
              pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
              pixel.background_color = Color::HSV(30, 20, tile.foreground[2] * 50 * PERCENT);
              });
        } else {
          $canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) {
              pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value);
              pixel.background_color = Color::HSV(tile.background[0], tile.background[1], tile.background[2] * light_value);
              });
        }
      }
    }

    System::draw_entities(*$level.world, *$level.map, lighting, $canvas, start, width, height);
  }

  void MapViewUI::create_render() {
    set_renderer(Renderer([&] {
          draw_map();
          return canvas($canvas);
          }));
  }

  void MapViewUI::resize_canvas() {
    // set canvas to best size
    $canvas = Canvas(width * 2, height * 4);
  }
}