#pragma once
#include <SDL3/SDL.h>
#include <vector>
#include <string>
#include "../../gameplay/core/Game.h"
#include "../../gameplay/coop/CoopGame.h"

// Forward declarations
class FontAtlas;
class LineEffect;

/**
 * GameRenderer - Utility class for rendering the Tetris game board and HUD.
 * 
 * This class encapsulates all the game-specific rendering logic that was
 * previously in main.cpp, making it reusable across different contexts.
 */
class GameRenderer {
public:
    // Render the complete playing state including game board, HUD, and effects
    static void renderPlayingState(
        SDL_Renderer* renderer,
        Game* game,
        FontAtlas* pixelFont,
        LineEffect* lineEffect,
        SDL_Texture* blocksTex,
        SDL_Texture* asteroidsTex,
        SDL_Texture* statisticsPanelTex,
        SDL_Texture* scorePanelTex,
        SDL_Texture* nextPanelTex,
        SDL_Texture* holdPanelTex,
        bool countdownActive,
        float logicalW,
        float logicalH,
        float logicalScale,
        float winW,
        float winH,
        bool challengeClearFxActive = false,
        const std::vector<int>* challengeClearFxOrder = nullptr,
        double challengeClearFxElapsedMs = 0.0,
        double challengeClearFxDurationMs = 0.0,
        const std::string* challengeStoryText = nullptr,
        float challengeStoryAlpha = 0.0f
    );

    // Render the pause overlay (full screen)
    static void renderPauseOverlay(
        SDL_Renderer* renderer,
        FontAtlas* pixelFont,
        float winW,
        float winH,
        float logicalScale
    );

    // Render the exit confirmation popup
    static void renderExitPopup(
        SDL_Renderer* renderer,
        FontAtlas* pixelFont,
        float winW,
        float winH,
        float logicalScale,
        int selectedButton
    );

    static void renderCoopPlayingState(
        SDL_Renderer* renderer,
        CoopGame* game,
        FontAtlas* pixelFont,
        LineEffect* lineEffect,
        SDL_Texture* blocksTex,
        SDL_Texture* statisticsPanelTex,
        SDL_Texture* scorePanelTex,
        SDL_Texture* nextPanelTex,
        SDL_Texture* holdPanelTex,
        bool paused,
        float logicalW,
        float logicalH,
        float logicalScale,
        float winW,
        float winH
    );

    // Public wrapper that forwards to the private tile-drawing helper. Use this if
    // calling from non-member helper functions (e.g. visual effects) that cannot
    // access private class members.
    static void drawBlockTexturePublic(SDL_Renderer* renderer, SDL_Texture* blocksTex, float x, float y, float size, int blockType);
    // Transport/teleport visual effect API (public): start a sci-fi "transport" animation
    // moving a visual copy of `piece` from screen pixel origin (startX,startY) to
    // target pixel origin (targetX,targetY). `tileSize` should be the same cell size
    // used for the grid. Duration is seconds.
    static void startTransportEffect(const Game::Piece& piece, float startX, float startY, float targetX, float targetY, float tileSize, float durationSeconds = 0.6f);
    // Convenience: compute the preview & grid positions using the same layout math
    // used by `renderPlayingState` and start the transport effect for the current
    // `game` using renderer layout parameters.
    static void startTransportEffectForGame(Game* game, SDL_Texture* blocksTex, float logicalW, float logicalH, float logicalScale, float winW, float winH, float durationSeconds = 0.6f);
    static bool isTransportActive();

private:
    // Helper functions for drawing game elements
    static void drawBlockTexture(SDL_Renderer* renderer, SDL_Texture* blocksTex, float x, float y, float size, int blockType);
    static void drawPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, const Game::Piece& piece, float ox, float oy, float tileSize, bool isGhost = false, float pixelOffsetX = 0.0f, float pixelOffsetY = 0.0f);
    static void drawSmallPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, PieceType pieceType, float x, float y, float tileSize);
    static void renderNextPanel(SDL_Renderer* renderer, FontAtlas* pixelFont, SDL_Texture* blocksTex, SDL_Texture* nextPanelTex, const Game::Piece& nextPiece, float panelX, float panelY, float panelW, float panelH, float tileSize);
    
    // Helper function for drawing rectangles
    static void drawRect(SDL_Renderer* renderer, float x, float y, float w, float h, SDL_Color c);
};
