#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, INV_WALL)),
  $paths(width, height)
{}

Map::Map(Matrix &walls, Pathing &paths) :
  $walls(walls),
  $paths(paths)
{
  $width = walls[0].size();
  $height = walls.size();
}

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);
}

Point Map::place_entity(size_t room_index) {
  dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");

  Room &start = $rooms[room_index];
  // BUG: this can place someone in a wall on accident, move them if they're stuck
  return {start.x+1, start.y+1};
}

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) {
  Matrix &paths = $paths.$paths;
  bool zero_found = false;

  // just make a list of the four directions
  std::array<Point, 4> dirs{{
      {out.x,out.y-1}, // north
      {out.x+1,out.y}, // east
      {out.x,out.y+1}, // south
      {out.x-1,out.y} // west
  }};

  // get the current dijkstra number
  int cur = paths[out.y][out.x];

  // pick a random start of directions
  // BUG: is uniform inclusive of the dir.size()?
  int rand_start = Random::uniform<int>(0, dirs.size());

  // go through all possible directions
  for(size_t i = 0; i < dirs.size(); i++) {
    // but start at the random start, effectively randomizing
    // which valid direction to go
    // BUG: this might be wrong given the above ranom from 0-size
    Point dir = dirs[(i + rand_start) % dirs.size()];
    if(!inmap(dir.x, dir.y)) continue; //skip unpathable stuff
    int weight = cur - paths[dir.y][dir.x];

    if(weight == 1) {
      // no matter what we follow direct paths
      out = dir;
      return true;
    } else if(random && weight == 0) {
      // if random is selected and it's a 0 path take it
      out = dir;
      return true;
    } else if(weight == 0) {
      // otherwise keep the last zero path for after
      out = dir;
      zero_found = true;
    }
  }

  // if we reach this then either zero was found and
  // zero_found is set true, or it wasn't and nothing found
  return zero_found;
}

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 depth={} has invalid position {},{}",
          room.depth, room.x, room.y));
    check(int(room.width) > 0 && int(room.height) > 0,
        format("room depth={} has invalid dims {},{}",
          room.depth, room.width, room.height));
  }

  return true;
}