#include <fmt/core.h>
#include <SFML/Graphics.hpp>
#include <numbers>
#include <cmath>
#include "matrix.hpp"
#include <cstdlib>

using matrix::Matrix;
using namespace fmt;

Matrix MAP{
  {1,1,1,1,1,1,1,1,1},
  {1,0,1,0,0,0,0,0,1},
  {1,0,1,0,0,1,1,0,1},
  {1,0,0,0,0,0,0,0,1},
  {1,1,0,0,0,0,0,0,1},
  {1,0,0,1,1,1,0,0,1},
  {1,0,0,0,1,0,0,0,1},
  {1,0,0,0,0,0,1,1,1},
  {1,1,1,1,1,1,1,1,1}
};

const int SCREEN_HEIGHT=480;
const int SCREEN_WIDTH=SCREEN_HEIGHT * 2;
const int MAP_SIZE=matrix::width(MAP);
const int TILE_SIZE=(SCREEN_WIDTH/2) / MAP_SIZE;
const float FOV = std::numbers::pi / 3.0;
const float HALF_FOV = FOV / 2;
const int CASTED_RAYS=30;
const float STEP_ANGLE = FOV / CASTED_RAYS;
const int MAX_DEPTH = MAP_SIZE * TILE_SIZE;
const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS;

float player_x = SCREEN_WIDTH / 4;
float player_y = SCREEN_WIDTH / 4;
float player_angle = std::numbers::pi;

void draw_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) {
  sf::RectangleShape rect(size);
  rect.setFillColor({color, color, color});
  rect.setPosition(pos);
  window.draw(rect);
}

void draw_map_rect(sf::RenderWindow &window, int x, int y, uint8_t color) {
  draw_rect(window,
      {float(x * TILE_SIZE), float(y * TILE_SIZE)},
      {float(TILE_SIZE-1), float(TILE_SIZE-1)},
      color);
}

void draw_map(sf::RenderWindow &window, Matrix &map) {
  uint8_t light_grey = 191;
  uint8_t dark_grey = 65;

  for(size_t y = 0; y < matrix::height(map); y++) {
    for(size_t x = 0; x < matrix::width(map); x++) {
      draw_map_rect(window, x, y, map[y][x] == 1 ? light_grey : dark_grey);
    }
  }
}

void draw_line(sf::RenderWindow &window, sf::Vector2f start, sf::Vector2f end) {
  sf::Vertex line[] = {
      sf::Vertex(start),
      sf::Vertex(end)
  };

  window.draw(line, 2, sf::Lines);
}

void draw_map_rays(sf::RenderWindow &window, int col, int row, sf::Vector2f target) {
  draw_map_rect(window, col, row, 100);
  draw_line(window, {player_x, player_y}, target);
}

void draw_3d_view(sf::RenderWindow &window, int depth, float start_angle, int ray) {
  uint8_t color = 255 / (1 + depth * depth * 0.0001);

  float fixed_depth = depth * std::cos(player_angle - start_angle);

  float wall_height = 21000 / fixed_depth;

  if(wall_height > SCREEN_HEIGHT){
    wall_height = SCREEN_HEIGHT;
  }

  draw_rect(window,
      {SCREEN_HEIGHT + ray * SCALE, (SCREEN_HEIGHT / 2) - wall_height / 2},
      {SCALE, wall_height},
      color);
}

void ray_casting(sf::RenderWindow &window, Matrix& map) {
  float start_angle = player_angle - HALF_FOV;

  for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE)
  {
    for(int depth = 1; depth < MAX_DEPTH; depth++) {
      float target_x = player_x - std::sin(start_angle) * depth;
      float target_y = player_y + std::cos(start_angle) * depth;

      int col = int(target_x / TILE_SIZE);
      int row = int(target_y / TILE_SIZE);

      if(map[row][col] == 1) {
        draw_map_rays(window, col, row, {target_x, target_y});
        draw_3d_view(window, depth, start_angle, ray);
        break;
      }
    }
  }
}

void draw_ceiling_floor(sf::RenderWindow &window) {
  draw_rect(window,
      {SCREEN_HEIGHT, SCREEN_HEIGHT /2},
      {SCREEN_HEIGHT, SCREEN_HEIGHT},
      100);
  draw_rect(window,
      {SCREEN_HEIGHT, (SCREEN_HEIGHT * -1) / 2},
      {SCREEN_HEIGHT, SCREEN_HEIGHT},
      200);
}

void draw_everything(sf::RenderWindow &window) {
  draw_map(window, MAP);
  draw_ceiling_floor(window);
  ray_casting(window, MAP);
  window.display();
}

bool collision(float x, float y) {
  int col = int(x / TILE_SIZE);
  int row = int(y / TILE_SIZE);

  return MAP[row][col] == 1;
}

int main() {
  using KB = sf::Keyboard;
  sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Raycaster");
  window.setVerticalSyncEnabled(true);

  while(window.isOpen()) {
    draw_everything(window);

    float x = player_x;
    float y = player_y;

    if(KB::isKeyPressed(KB::Q)) {
      player_angle -= 0.1;
    } else if(KB::isKeyPressed(KB::E)) {
      player_angle += 0.1;
    }

    if(KB::isKeyPressed(KB::W)) {
      x += -1 * std::sin(player_angle) * 5;
      y += std::cos(player_angle) * 5;
    } else if(KB::isKeyPressed(KB::S)) {
      x -= -1 * std::sin(player_angle) * 5;
      y -= std::cos(player_angle) * 5;
    }

    if(KB::isKeyPressed(KB::D)) {
      x += -1 * std::sin(player_angle + std::numbers::pi * 0.5) * 5;
      y += std::cos(player_angle + std::numbers::pi * 0.5) * 5;
    } else if(KB::isKeyPressed(KB::A)) {
      x -= -1 * std::sin(player_angle + std::numbers::pi * 0.5) * 5;
      y -= std::cos(player_angle + std::numbers::pi * 0.5) * 5;
    }

    if(!collision(x, y)) {
      player_x = x;
      player_y = y;
    }

    sf::Event event;
    while(window.pollEvent(event)) {
      if(event.type == sf::Event::Closed) {
        window.close();
      }
    }
  }

  return 0;
}
