#include "gui.hpp"
#include <iostream>
#include <chrono>
#include <numeric>
#include <functional>
#include "components.hpp"
#include <numbers>
#include "systems.hpp"

using namespace components;

namespace gui {
  using ftxui::Color;

  MapViewUI::MapViewUI(GameLevel &level) :
    Panel(RAY_VIEW_X, 0, 0, 0, true),
    $level(level)
  {}

  void MapViewUI::update_level(GameLevel &level) {
    $level = level;
  }

  void MapViewUI::draw_map() {
    const auto& debug = $level.world->get_the<Debug>();
    const auto& player = $level.world->get_the<Player>();
    const auto& player_position = $level.world->get<Position>(player.entity);
    Point start = $level.map->center_camera(player_position.location, width, height);
    auto &tiles = $level.map->tiles();
    auto &paths = $level.map->paths();
    auto &lighting = $level.lights->lighting();

    // WARN: this is exploiting that -1 in size_t becomes largest
    size_t end_x = std::min(size_t(width), $level.map->width() - start.x);
    size_t end_y = std::min(size_t(height), $level.map->height() - start.y);

    for(size_t y = 0; y < end_y; ++y) {
      for(size_t x = 0; x < end_x; ++x)
      {
        const Tile& tile = tiles.at(start.x+x, start.y+y);
        // light value is an integer that's a percent
        float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT;
        int dnum = debug.PATHS ? paths[start.y+y][start.x+x] : WALL_PATH_LIMIT;

        if(debug.PATHS && dnum != WALL_PATH_LIMIT) {
          string num = dnum > 15 ? "*" : fmt::format("{:x}", dnum);

          $canvas.DrawText(x * 2, y * 4, num, [dnum, tile, light_value](auto &pixel) {
              pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
              pixel.background_color = Color::HSV(30, 20, tile.foreground[2] * 50 * PERCENT);
              });
        } else {
          $canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) {
              pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value);
              pixel.background_color = Color::HSV(tile.background[0], tile.background[1], tile.background[2] * light_value);
              });
        }
      }
    }

    System::draw_entities(*$level.world, *$level.map, lighting, $canvas, start, width, height);
  }

  void MapViewUI::create_render() {
    set_renderer(Renderer([&] {
      draw_map();
      return canvas($canvas);
    }));
  }

  void MapViewUI::resize_canvas() {
    // set canvas to best size
    $canvas = Canvas(width * 2, height * 4);
  }

  FSM::FSM() :
    $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"),
    $renderer($window),
    $level($levels.current()),
    $map_view($level),
    $font{FONT_FILE_NAME},
    $text{$font},
    $rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)
    {
      $window.setVerticalSyncEnabled(VSYNC);
      $window.setFramerateLimit(FRAME_LIMIT);
      $text.setPosition({10,10});
      $text.setFillColor({255,255,255});
      $textures.load_tiles();
      $textures.load_sprites();
    }

  void FSM::event(Event ev) {
    switch($state) {
      FSM_STATE(State, START, ev);
      FSM_STATE(State, MOVING, ev);
      FSM_STATE(State, ATTACKING, ev);
      FSM_STATE(State, MAPPING, ev);
      FSM_STATE(State, ROTATING, ev);
      FSM_STATE(State, IDLE, ev);
      FSM_STATE(State, END, ev);
    }
  }

  void FSM::START(Event ) {
    generate_map();
    $level.world->set_the<Debug>({});
    $rayview.init_shaders();
    $rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
    $rayview.position_camera($player.x + 0.5, $player.y + 0.5);

    $renderer.init_terminal();
    $map_view.create_render();

    $renderer.resize_grid(MAX_FONT_SIZE, $map_view);
    $map_view.resize_canvas();

    state(State::IDLE);
  }

  void FSM::MAPPING(Event ev) {
    // BUG: can't close window when in mapping
    switch(ev) {
      case Event::MAP_OPEN:
        state(State::IDLE);
        break;
      case Event::CLOSE:
        state(State::IDLE);
        break;
      case Event::TICK:
        break;
      default:
        dbc::log("invalid event sent to MAPPING");
    }
  }

  void FSM::MOVING(Event ) {
    if($camera.play_move($rayview)) {
      System::plan_motion(*$level.world, {size_t($camera.target_x), size_t($camera.target_y)});
      run_systems();
      state(State::IDLE);
    }
  }

  void FSM::ATTACKING(Event ev) {
    fmt::println("ATTACKING! {}", $rotation);
    switch(ev) {
      case Event::ATTACK:
        run_systems();
        $rotation = $rotation < 0.0f ? 0.0f : -33.0f;
        break;
      default:
        state(State::IDLE);
    }
  }

  void FSM::ROTATING(Event ) {
    if($camera.play_rotate($rayview)) {
      state(State::IDLE);
    }
  }

  void FSM::IDLE(Event ev) {
    using FU = Event;
    $rotation = -10.0f;

    switch(ev) {
      case FU::QUIT:
        $window.close();
        state(State::END);
        return; // done
      case FU::MOVE_FORWARD:
        try_move(1, false);
        break;
      case FU::MOVE_BACK:
        try_move(-1, false);
        break;
      case FU::MOVE_LEFT:
        try_move(1, true);
        break;
      case FU::MOVE_RIGHT:
        try_move(-1, true);
        break;
      case FU::ROTATE_LEFT:
        $camera.plan_rotate($rayview, 1);
        state(State::ROTATING);
        break;
      case FU::ROTATE_RIGHT:
        $camera.plan_rotate($rayview, -1);
        state(State::ROTATING);
        break;
      case FU::MAP_OPEN:
        state(State::MAPPING);
        break;
      case FU::ATTACK:
        fmt::println("ATTACK IN IDLE!");
        draw_weapon();
        state(State::ATTACKING);
        break;
      case FU::CLOSE:
        dbc::log("Nothing to close.");
        break;
      default:
        dbc::sentinel("unhandled event in IDLE");
    }
  }

  void FSM::try_move(int dir, bool strafe) {
    // prevent moving into occupied space
    Point move_to = $camera.plan_move($rayview, dir, strafe);

    if($level.map->can_move(move_to) && !$level.collision->occupied(move_to)) {
      state(State::MOVING);
    } else {
      state(State::IDLE);
      $camera.abort_plan($rayview);
    }
  }

  void FSM::END(Event ev) {
    fmt::println("END: received event after done: {}", int(ev));
  }

  void FSM::keyboard() {
    while(const auto keyev = $window.pollEvent()) {
      if(keyev->is<sf::Event::Closed>()) {
        event(Event::QUIT);
      }

      if(const auto* key = keyev->getIf<sf::Event::KeyPressed>()) {
        using KEY = sf::Keyboard::Scan;
        switch(key->scancode) {
          case KEY::W:
            event(Event::MOVE_FORWARD);
            break;
          case KEY::S:
            event(Event::MOVE_BACK);
            break;
          case KEY::Q:
            event(Event::ROTATE_LEFT);
            break;
          case KEY::E:
            event(Event::ROTATE_RIGHT);
            break;
          case KEY::D:
            event(Event::MOVE_RIGHT);
            break;
          case KEY::A:
            event(Event::MOVE_LEFT);
            break;
          case KEY::Space:
            $rotation = 0;
            event(Event::ATTACK);
            break;
          case KEY::R:
            $stats.reset();
            break;
          case KEY::M:
            event(Event::MAP_OPEN);
            break;
          case KEY::Escape:
            event(Event::CLOSE);
            break;
          default:
            break; // ignored
        }
      }
    }
  }

  void FSM::draw_weapon() {
    auto weapon = $rayview.$textures.sword.sprite;
    weapon->setPosition({SCREEN_WIDTH/2, SCREEN_HEIGHT/2});
    weapon->setRotation(sf::degrees($rotation));
    $window.draw(*weapon);
  }

  void FSM::draw_gui() {
    sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT});
    rect.setPosition({0,0});
    rect.setFillColor({50, 50, 50});
    $window.draw(rect);

    auto player = $level.world->get_the<Player>();
    auto player_combat = $level.world->get<Combat>(player.entity);

    $text.setString(
        fmt::format("FPS\n"
          "HP: {}\n"
          "mean:{:>8.5}\n"
          "sdev: {:>8.5}\n"
          "min: {:>8.5}\n"
          "max: {:>8.5}\n"
          "count:{:<10}\n\n"
          "VSync? {}\n"
          "FR Limit: {}\n"
          "Debug? {}\n\n"
          "dir: {:>2.02},{:>2.02}\n"
          "pos: {:>2.02},{:>2.02}\n\n",
          player_combat.hp, $stats.mean(), $stats.stddev(), $stats.min,
          $stats.max, $stats.n, VSYNC,
          FRAME_LIMIT, DEBUG_BUILD, $rayview.$dir_x,
          $rayview.$dir_y, $rayview.$pos_x, $rayview.$pos_y));

    $window.draw($text);
  }

  void FSM::render() {
    auto start = std::chrono::high_resolution_clock::now();

    if(in_state(State::MAPPING)) {
      $window.clear();
      $map_view.render();
      $renderer.draw($map_view);
    } else {
      $rayview.draw($window);
    }

    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration<double>(end - start);
    $stats.sample(1/elapsed.count());

    draw_gui();
    draw_weapon();
    $window.display();
  }

  void FSM::mouse() {
  }

  void FSM::generate_map() {
    // ZED: this should eventually go away now that level manager is in play
    auto& player = $level.world->get_the<Player>();
    auto& player_position = $level.world->get<Position>(player.entity);
    $player = player_position.location;
    $rayview.set_level($level);
  }

  void FSM::run_systems() {
    System::enemy_pathing($level);
    System::collision($level);
    System::motion($level);
    System::lighting($level);
    System::death($level);
  }

  bool FSM::active() {
    return !in_state(State::END);
  }
}
