#pragma once

#include "character/CharacterActionTypes.h"
#include <frostbite2D/event/joystick_event.h>
#include <frostbite2D/event/key_event.h>
#include <cstdint>
#include <string>
#include <vector>

namespace frostbite2D {

/// 将 SDL 键盘/手柄事件统一转换为角色命令，避免状态机直接依赖设备细节。
class CharacterInputRouter {
public:
  bool OnKeyDown(const KeyEvent& event);
  bool OnKeyUp(const KeyUpEvent& event);
  bool OnJoystickAxis(const JoystickAxisEvent& event);
  bool OnJoystickButtonDown(const JoystickButtonDownEvent& event);
  bool OnJoystickButtonUp(const JoystickButtonUpEvent& event);

  void EmitCommands(CharacterCommandBuffer& commandBuffer);
  void ClearFrameState();

private:
  enum class KeyboardMoveDirection {
    None,
    Left,
    Right,
    Up,
    Down,
  };

  void QueueAction(const std::string& actionId);
  bool IsDirectionPressed(KeyboardMoveDirection direction) const;
  bool HasDirectionalInput() const;
  void RefreshKeyboardMoveMode();

  bool left_ = false;
  bool right_ = false;
  bool up_ = false;
  bool down_ = false;
  bool jumpPressed_ = false;
  std::vector<std::string> actionRequests_;
  CharacterMoveMode keyboardMoveMode_ = CharacterMoveMode::None;
  KeyboardMoveDirection runDirection_ = KeyboardMoveDirection::None;
  std::uint32_t leftLastTapTimestamp_ = 0;
  std::uint32_t rightLastTapTimestamp_ = 0;
  std::uint32_t upLastTapTimestamp_ = 0;
  std::uint32_t downLastTapTimestamp_ = 0;
  float leftStickX_ = 0.0f;
  float leftStickY_ = 0.0f;
  float joystickDeadZone_ = 0.2f;
};

} // namespace frostbite2D
