#include "raycaster.hpp"
#include <iostream>
#include <chrono>
#include <numeric>
#include <functional>
#include "constants.hpp"
#include "stats.hpp"
#include "levelmanager.hpp"
#include "components.hpp"
#include "camera.hpp"
#include <numbers>

using namespace components;

void draw_gui(sf::RenderWindow &window, Raycaster &rayview, sf::Text &text, Stats &stats) {
  sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT});

  rect.setPosition({0,0});
  rect.setFillColor({50, 50, 50});
  window.draw(rect);

  text.setString(
    fmt::format("FPS\nmean:{:>8.5}\nsdev: {:>8.5}\nmin: {:>8.5}\nmax: {:>8.5}\ncount:{:<10}\n\nVSync? {}\nFR Limit: {}\nDebug? {}\n\nHit R to reset.\n\ndir: {:>2.2},{:>2.2}\npos: {:>2.2},{:>2.2}",
      stats.mean(), stats.stddev(), stats.min,
      stats.max, stats.n, VSYNC,
      FRAME_LIMIT, DEBUG_BUILD, rayview.$dirX,
      rayview.$dirY, rayview.$posX, rayview.$posY));
  window.draw(text);
}

Matrix generate_map(TexturePack &textures, GameLevel &level, Point &player_out) {
  auto &tiles = level.map->tiles();
  auto &player = level.world->get_the<Player>();
  auto &player_position = level.world->get<Position>(player.entity);
  player_out = player_position.location;

  return textures.convert_char_to_texture(tiles.$tile_ids);
}

void draw_weapon(sf::RenderWindow &window, sf::Sprite &weapon, float rotation) {
  weapon.setPosition({SCREEN_WIDTH/2,SCREEN_HEIGHT/2});
  weapon.setRotation(sf::degrees(rotation));
  window.draw(weapon);
}

enum MoveState {
  MOVE,
  ROTATE,
  STRAFE,
  IDLE
};

inline void handle_window_events(sf::RenderWindow &window, Raycaster &rayview,
    MoveState &state, CameraLOL &camera)
{
  while(const auto event = window.pollEvent()) {
    if(event->is<sf::Event::Closed>()) {
      window.close();
    }

    if(const auto* key = event->getIf<sf::Event::KeyPressed>()) {
      if(key->scancode == sf::Keyboard::Scan::W) {
        camera.plan_run(rayview, 1);
        state = MOVE;
      } else if(key->scancode == sf::Keyboard::Scan::S) {
        camera.plan_run(rayview, -1);
        state = MOVE;
      }

      if(key->scancode == sf::Keyboard::Scan::Q) {
        camera.plan_rotate(rayview, 1);
        state = ROTATE;
      } else if(key->scancode == sf::Keyboard::Scan::E) {
        camera.plan_rotate(rayview, -1);
        state = ROTATE;
      }

      if(key->scancode == sf::Keyboard::Scan::D) {
        camera.plan_strafe(rayview, -1);
        state = STRAFE;
      } else if(key->scancode == sf::Keyboard::Scan::A) {
        camera.plan_strafe(rayview, 1);
        state = STRAFE;
      }
    }
  }

  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::P)) {
    if(rayview.$active_shader == nullptr) {
      rayview.$active_shader = &rayview.$paused;
    } else {
      rayview.$active_shader = nullptr;
    }
  }
}

int main() {
  sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Ray Caster Game Thing");

  sf::Font font{"./assets/text.otf"};
  sf::Text text{font};
  text.setFillColor({255,255,255});
  text.setPosition({10,10});

  Point player{0, 0};
  LevelManager levels;
  GameLevel &cur_level = levels.current();

  TexturePack textures;
  textures.load_tiles();
  textures.load_sprites();

  auto map = generate_map(textures, cur_level, player);

  Point evil_eye_1, evil_eye_2;
  dbc::check(cur_level.map->place_entity(1, evil_eye_1), "failed to place enemy 1");
  dbc::check(cur_level.map->place_entity(2, evil_eye_2), "failed to place enemy 2");

  Raycaster rayview(window, textures, map, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
  rayview.init_shaders();

  rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
  rayview.position_camera(player.x + 0.5, player.y + 0.5);

  rayview.position_sprite(evil_eye_1, "evil_eye");
  rayview.position_sprite(evil_eye_2, "evil_eye");

  float rotation = -30.0f;
  Stats stats;

  window.setVerticalSyncEnabled(VSYNC);
  window.setFramerateLimit(FRAME_LIMIT);

  MoveState state = IDLE;
  CameraLOL camera;

  while(window.isOpen()) {
    auto start = std::chrono::high_resolution_clock::now();
    rayview.render();
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration<double>(end - start);
    stats.sample(1/elapsed.count());

    auto weapon_sprite_ptr = rayview.$textures.sword.sprite;
    draw_gui(window, rayview, text, stats);
    draw_weapon(window, *weapon_sprite_ptr, rotation);
    window.display();

    if(state == IDLE) {
      handle_window_events(window, rayview, state, camera);
    } else if(state == MOVE) {
      if(camera.play_run(rayview)) {
        state = IDLE;
      }
    } else if(state == ROTATE) {
      if(camera.play_rotate(rayview)) {
        state = IDLE;
      }
    } else if(state == STRAFE) {
      if(camera.play_strafe(rayview)) {
        state = IDLE;
      }
    } else {
      dbc::sentinel("invalid move state.");
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) {
      rayview.position_camera(player.x + 0.5, player.y + 0.5);
      stats.reset();
    }

    if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
      rayview.$anim.play(false);
      rotation = -30.0f;
    } else {
      rotation = -10.0f;
    }
  }

  return 0;
}
