// ============================================================================
// INPUT MANAGER HEADER
// ============================================================================
// Handles all input processing and debouncing for touch and button inputs
// Converts hardware events into InputEvent objects for games to consume

#ifndef INPUT_MANAGER_H
#define INPUT_MANAGER_H

#include <stdint.h>
#include "input_event.h"
#include "display/low_level_touch.h"

// Forward declaration - avoid pulling in full basic1.cpp
struct GameConfig;

/**
 * @brief Input Manager - Processes touch and button inputs
 * 
 * Responsibilities:
 * - Reading touch controller data
 * - Debouncing touch and button events
 * - Converting raw inputs to InputEvent objects
 * - Gesture recognition
 * 
 * Does NOT handle:
 * - Button GPIO initialization (stays in basic1.cpp)
 * - Interrupt handler registration (stays in basic1.cpp)
 */
class InputManager {
public:
    /**
     * @brief Construct InputManager with hardware references
     * @param touch Pointer to touch controller interface
     * @param config Pointer to game configuration
     */
    InputManager(LowLevelTouch* touch, const GameConfig* config);
    
    /**
     * @brief Process touch input from controller
     * @param last_time Pointer to last touch timestamp for debouncing
     * @return InputEvent (valid=false if no valid input)
     */
    InputEvent process_touch_input(uint32_t* last_time);
    
    /**
     * @brief Process button input from GPIO flags
     * @return InputEvent (valid=false if no valid input)
     */
    InputEvent process_button_input();

    /**
     * @brief Get virtual button regions for drawing
     * @param a_rect Pointer to rectangle for Button A [x, y, w, h]
     * @param b_rect Pointer to rectangle for Button B [x, y, w, h]
     */
    void get_virtual_button_regions(int* a_rect, int* b_rect) const;

    /**
     * @brief Set virtual button regions
     */
    void set_virtual_button_regions(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh);

    /**
     * @brief Clear virtual button regions (disables detection)
     */
    void clear_virtual_button_regions();

    /**
     * @brief Check if a touch event hits a virtual button
     * @param x Touch X coordinate
     * @param y Touch Y coordinate
     * @param out_type Output parameter for the button type if hit
     * @return true if a virtual button was hit
     */
    bool check_virtual_buttons(int16_t x, int16_t y, InputType& out_type) const;
    
    /**
     * @brief Get human-readable gesture name
     * @param gesture_code Gesture code from touch controller
     * @return Constant string with gesture name
     */
    static const char* get_gesture_name(uint8_t gesture_code);
    
    /**
     * @brief Check if touch input is available
     * @return true if touch controller is present, false otherwise
     */
    bool has_touch() const;
    
    /**
     * @brief Check if hardware buttons are available
     * @return true if physical buttons are present, false otherwise
     */
    bool has_buttons() const;

private:
    LowLevelTouch* touch;
    const GameConfig* config;
    
    // Virtual button regions
    int v_button_a[4] = {0, 0, 0, 0}; // [x, y, w, h]
    int v_button_b[4] = {0, 0, 0, 0};
    bool v_buttons_active = false;
};

#endif // INPUT_MANAGER_H
