#pragma once

#include <box2d/box2d.h>
#include <random>
#include <raylib.h>

class Court {
  static constexpr float BALL_START_Y = 12.0f;
  static constexpr float BALL_START_VELOCITY = 4.0f;
  static constexpr int ALLOWED_TOUCHES = 3;
  static constexpr double BALL_RESET_SECS = 1.5f;

  bool left_start;
  b2BodyId ball_id;
  b2ShapeId ball_shape_id;
  b2BodyId ground_id;
  b2ShapeId ground_shape_id;
  b2WorldId world_id;
  b2ShapeId player_left;
  b2ShapeId player_right;
  b2BodyId net_id;

  std::mt19937 rng;

  int left_points;
  int right_points;

  int allowed_touches_left;
  int allowed_touches_right;

  double ball_reset_time;
  bool in_play;

  Texture2D ball_texture;
  Texture2D flag_texture;
  Texture2D juice_texture;
  Texture2D net_texture;
  Texture2D ground_texture;
  Texture2D background_texture;
  Texture2D tower_texture;

  b2BodyId flag_ids[40];

  void draw_background() const;
  void draw_ground() const;
  void draw_towers() const;
  void draw_ball() const;
  static void draw_flag(b2Transform transform, const Texture2D& texture);
  void draw_net() const;

public:
  explicit Court(b2WorldId world_id);
  ~Court();
  static bool compare_shape_ids(const b2ShapeId& a, const b2ShapeId& b);
  void update();
  void draw() const;
  void draw_ui(int screen_width, int screen_height) const;
  void reset_court();
  [[nodiscard]] b2BodyId get_ball_id() const;
  void set_left_shape_id(b2ShapeId shape_id);
  void set_right_shape_id(b2ShapeId shape_id);
  Vector2 get_ball_position() const;
};
