#pragma once

#include <fmt/core.h>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Image.hpp>
#include <numbers>
#include <algorithm>
#include <cmath>
#include "matrix.hpp"
#include <cstdlib>
#include <array>
#include "dbc.hpp"
#include "amt/pixel.hpp"
#include "amt/texture.hpp"
#include <memory>
#include "thread.hpp"

using Matrix = amt::Matrix<int>;

struct Raycaster {
  int PITCH=0;

  TexturePack textures;
  double posX = 0;
  double posY = 0;

  // initial direction vector
  double dirX = -1;
  double dirY = 0;

  // the 2d raycaster version of camera plane
  double planeX = 0;
  double planeY = 0.66;
  sf::Texture view_texture;
  sf::Sprite view_sprite;

  //ZED: USE smart pointer for this

  int $width;
  int $height;
  amt::PixelBuf pixels;
  sf::RenderWindow& $window;
  Matrix& $map;
  std::vector<int> spriteOrder;
  std::vector<double> spriteDistance;
  std::vector<double> ZBuffer; // width
  float $radius; // std::min($height, $width) / 2;
  float $r_sq; // = radius * radius;
  amt::thread_pool_t pool;

  Raycaster(sf::RenderWindow& window, Matrix &map, unsigned width, unsigned height);

  void draw_pixel_buffer();
  void clear();
  void cast_rays();
  void draw_ceiling_floor();
  void sprite_casting();
  void sort_sprites(std::vector<int>& order, std::vector<double>& dist, int amount);
  void render();

  bool empty_space(int new_x, int new_y);

  void run(double speed, int dir);
  void rotate(double speed, int dir);
  void position_camera(float player_x, float player_y);
  float get_distance_from_center(int x, int y) const noexcept;

  void set_position(int x, int y);
  inline size_t pixcoord(int x, int y) {
    return ((y) * $width) + (x);
  }

};