#include "map.hpp"
#include "dbc.hpp"
#include "rand.hpp"
#include <vector>
#include <array>
#include <fmt/core.h>
#include <utility>
#include "matrix.hpp"

using std::vector, std::pair;
using namespace fmt;

Map::Map(size_t width, size_t height) :
  $width(width),
  $height(height),
  $walls(height, matrix::Row(width, SPACE_VALUE)),
  $paths(width, height)
{}

Map::Map(Matrix &walls, Pathing &paths) :
  $walls(walls),
  $paths(paths)
{
  $width = matrix::width(walls);
  $height = matrix::height(walls);
}

void Map::make_paths() {
  INVARIANT();
  $paths.compute_paths($walls);
}

bool Map::inmap(size_t x, size_t y) {
  return x < $width && y < $height;
}

void Map::set_target(const Point &at, int value) {
  $paths.set_target(at, value);
}

void Map::clear_target(const Point &at) {
  $paths.clear_target(at);
}

bool Map::place_entity(size_t room_index, Point &out) {
  dbc::check($dead_ends.size() != 0, "no dead ends?!");

  if(room_index < $rooms.size()) {
    Room &start = $rooms.at(room_index);

    for(matrix::rando_rect it{$walls, start.x, start.y, start.width, start.height}; it.next();) {
      if(!iswall(it.x, it.y)) {
        out.x = it.x;
        out.y = it.y;
        return true;
      }
    }
  }

  out = $dead_ends.at(room_index % $dead_ends.size());
  return true;
}

bool Map::iswall(size_t x, size_t y) {
  return $walls[y][x] == WALL_VALUE;
}

void Map::dump(int show_x, int show_y) {
  matrix::dump("WALLS", walls(), show_x, show_y);
  matrix::dump("PATHS", paths(), show_x, show_y);
}

bool Map::can_move(Point move_to) {
  return inmap(move_to.x, move_to.y) &&
    !iswall(move_to.x, move_to.y);
}

Point Map::map_to_camera(const Point &loc, const Point &cam_orig) {
  return {loc.x - cam_orig.x, loc.y - cam_orig.y};
}

Point Map::center_camera(const Point &around, size_t view_x, size_t view_y) {
  int high_x = int(width() - view_x);
  int high_y = int(height() - view_y);
  int center_x = int(around.x - view_x / 2);
  int center_y = int(around.y - view_y / 2);

  size_t start_x = high_x > 0 ? std::clamp(center_x, 0, high_x) : 0;
  size_t start_y = high_y > 0 ? std::clamp(center_y, 0, high_y) : 0;

  return {start_x, start_y};
}

/*
 * Finds the next optimal neighbor in the path
 * using either a direct or random method.
 *
 * Both modes will pick a random direction to start
 * looking for the next path, then it goes clock-wise
 * from there.
 *
 * In the direct method it will attempt to find
 * a path that goes 1 lower in the dijkstra map
 * path, and if it can't find that it will go to
 * a 0 path (same number).
 *
 * In random mode it will pick either the next lower
 * or the same level depending on what it finds first.
 * Since the starting direction is random this will
 * give it a semi-random walk that eventually gets to
 * the target.
 *
 * In map generation this makes random paths and carves
 * up the space to make rooms more irregular.
 *
 * When applied to an enemy they will either go straight
 * to the player (random=false) or they'll wander around
 * drunkenly gradually reaching the player, and dodging
 * in and out.
 */
bool Map::neighbors(Point &out, bool random, int direction) {
  return $paths.random_walk(out, random, direction);
}

bool Map::INVARIANT() {
  using dbc::check;

  check($walls.size() == height(), "walls wrong height");
  check($walls[0].size() == width(), "walls wrong width");
  check($paths.$width == width(), "in Map paths width don't match map width");
  check($paths.$height == height(), "in Map paths height don't match map height");

  for(auto room : $rooms) {
    check(int(room.x) >= 0 && int(room.y) >= 0,
        format("room invalid position {},{}",
          room.x, room.y));
    check(int(room.width) > 0 && int(room.height) > 0,
        format("room has invalid dims {},{}",
          room.width, room.height));
  }

  return true;
}

void Map::init_tiles() {
  $tiles = $walls;
}

void Map::enclose() {
  // wraps the outside edge with solid walls
  std::array<Point, 4> starts{{
    {0,0}, {$width-1, 0}, {$width-1, $height-1}, {0, $height-1}
  }};

  std::array<Point, 4> ends{{
    {$width-1, 0}, {$width-1, $height-1}, {0, $height-1}, {0,0},
  }};

  for(size_t i = 0; i < starts.size(); i++) {
    for(matrix::line it{starts[i], ends[i]}; it.next();) {
      $walls[it.y][it.x] = 1;
    }
  }
}

void Map::add_room(Room &room) {
  $rooms.push_back(room);
}

void Map::invert_space() {
  for(matrix::each_cell it{$walls}; it.next();) {
    int is_wall = !$walls[it.y][it.x];
    $walls[it.y][it.x] = is_wall;
  }
}