#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 THREED_VIEW_WIDTH=480;
const int THREED_VIEW_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=120;
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;

struct RGBA {
  uint8_t r;
  uint8_t g;
  uint8_t b;
  uint8_t a;
};

RGBA pixels[SCREEN_HEIGHT * SCREEN_HEIGHT] = {{0,0,0,0}};
sf::Texture view_texture;
sf::Sprite view_sprite;

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_pixel_buffer(sf::RenderWindow &window) {
  view_texture.update((uint8_t *)pixels, SCREEN_HEIGHT, SCREEN_HEIGHT, 0, 0);
  view_sprite.setTexture(view_texture);
  view_sprite.setPosition(SCREEN_HEIGHT, 0);
  window.draw(view_sprite);
}

void draw_pixel_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) {
  size_t x_start = size_t(pos.x - SCREEN_HEIGHT);
  size_t y_start = size_t(pos.y);
  size_t width = size_t(size.x);
  size_t height = size_t(size.y);

  for(size_t y = y_start; y < y_start + height; y++) {
    for(size_t x = x_start; x < x_start + width; x++) {
      size_t pixel_index = (y * SCREEN_HEIGHT) + x;
        pixels[pixel_index] = {color, color, color, 255};
    }
  }
}

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_pixel_rect(window,
      {SCREEN_HEIGHT + ray * SCALE, (SCREEN_HEIGHT / 2) - wall_height / 2},
      {SCALE, wall_height},
      color);
}

void clear_pixel_buffer() {
  std::fill_n(pixels, SCREEN_HEIGHT * SCREEN_HEIGHT, RGBA{});
}

void ray_casting(sf::RenderWindow &window, Matrix& map) {
  clear_pixel_buffer();
  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);
  draw_pixel_buffer(window);
  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);
  view_texture.create(SCREEN_HEIGHT, SCREEN_HEIGHT);

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