#include "gui/main_ui.hpp"
#include "components.hpp"
#include "easings.hpp"
#include <fmt/xchar.h>
#include "constants.hpp"

namespace gui {
  using namespace components;

  MainUI::MainUI(sf::RenderWindow& window) :
    $window(window),
    $rayview(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)
  {
    $window.setVerticalSyncEnabled(VSYNC);
    $window.setFramerateLimit(FRAME_LIMIT);
  }

  void MainUI::dirty() {
    $needs_render = true;
  }

  void MainUI::init() {
    auto& player_position = $level.world->get<Position>($level.player);
    auto player = player_position.location;

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

    $overlay_ui.init();
  }

  DinkyECS::Entity MainUI::camera_aim() {
    // what happens if there's two things at that spot
    if($level.collision->something_there($rayview.aiming_at)) {
      return $level.collision->get($rayview.aiming_at);
    } else {
      return 0;
    }
  }

  void MainUI::render() {
    if($needs_render) $rayview.render();
    $rayview.draw($window);

    $overlay_ui.render($window);
  }

  lel::Cell MainUI::overlay_cell(const std::string& name) {
    return $overlay_ui.$gui.cell_for(name);
  }

  std::optional<Point> MainUI::play_rotate() {
    if($rayview.play_rotate()) {
      $needs_render = false;
      return std::make_optional<Point>($rayview.aiming_at);
    } else {
      $needs_render = true;
      return std::nullopt;
    }
  }

  std::optional<components::Position> MainUI::play_move() {
    if($rayview.play_move()) {
      $needs_render = false;
      return std::make_optional<Position>(
          $rayview.camera_target(),
          $rayview.aiming_at);
    } else {
      $needs_render = true;
      return std::nullopt;
    }
  }

  void MainUI::plan_rotate(int dir, float amount) {
    // -1 is left, 1 is right
    int extra = (amount == 0.5) * dir;
    $compass_dir = ($compass_dir + dir + extra) % COMPASS.size();
    $rayview.plan_rotate(dir, amount);
  }

  Point MainUI::plan_move(int dir, bool strafe) {
    return $rayview.plan_move(dir, strafe);
  }

  void MainUI::abort_plan() {
    $rayview.abort_plan();
  }

  void MainUI::dead_entity(DinkyECS::Entity entity) {
    // BUG: this is kind of weird, but I think when I switch to dead bodies for things
    // (see System::distribute_loot) then this can be fixed or improved
    if($level.world->has<components::Sprite>(entity)) {
      auto &sprite = $level.world->get<components::Sprite>(entity);
      $rayview.update_sprite(entity, sprite);
    }
  }

  void MainUI::update_level(GameLevel level) {
    $level = level;
    auto& player_position = $level.world->get<Position>($level.player);
    auto player = player_position.location;

    $rayview.update_level($level);
    $rayview.position_camera(player.x + 0.5, player.y + 0.5);

    player_position.aiming_at = $rayview.aiming_at;

    $compass_dir = 0;

    $overlay_ui.update_level(level);
    dirty();
  }

  void MainUI::mouse(int x, int y, bool hover) {
    $overlay_ui.$gui.mouse(x, y, hover);
  }
}
