#include "controllers.h"

Action dummy_shooter_controller(const Info& info) {
  return Action{.jump = false, .move_to_x = 10.0f};
}

Action simple_prediction_controller(const Info& info) {
  const float predict_ball_x = [&] {
    constexpr float GRAVITY = 9.81f;
    constexpr float TARGET_HEIGHT = 1.0f;

    constexpr float a = -0.5f * GRAVITY;
    const float b = info.ball_vel_y;
    const float c = info.ball_y - TARGET_HEIGHT;

    const float discriminant = b * b - 4 * a * c;
    if (discriminant < 0) {
      return info.ball_x;
    }

    float t = (-b + std::sqrt(discriminant)) / (2 * a);
    if (t < 0) {
      t = (-b - std::sqrt(discriminant)) / (2 * a);
    }

    if (t < 0) {
      return info.ball_x;
    }

    return info.ball_x + info.ball_vel_x * t;
  }();

  const auto dist_to_ball = static_cast<float>(
      std::sqrt(std::pow(info.ball_x - info.player_x, 2) + std::pow(info.ball_y - info.player_y, 2)));

  if (info.ball_x < 0.0f && predict_ball_x < 0.0f) {
    return Action{.jump = false, .move_to_x = 5.0f};
  }

  if (predict_ball_x > 22.0f) {
    return Action{.jump = false, .move_to_x = 15.0f};
  }

  return Action{.jump = dist_to_ball < 2.0f, .move_to_x = predict_ball_x + 0.5f};
}
