#include "tetris-graph.h"

int main(void){
    
    InitGame();
    
    Piece* pieceList = getPiecesData("tetris.data");
    Grid grid = gridInit();
    Piece p = pieceInit();
    bool isFixed = true;

    int score = 0;
    int level = 1;

    int frameCounter = 0;
    
    while(!WindowShouldClose() && !gameOver(grid)){

        BeginDrawing();
        InitDisplay();
        displayScore(score);
        displayLevel(level);
        
        // Nouvelles pièces
        if(isFixed){
            p = spawnPiece(pieceList);
            movePieceHztl(p, 3);
            isFixed = false;
        }
        
        // Déplacements saisis au clavier
        if(IsKeyPressed(KEY_LEFT) && canMoveHztl(grid, p, -1)){
            movePieceHztl(p, -1);
        }
        if(IsKeyPressed(KEY_RIGHT) && canMoveHztl(grid, p, 1)){
            movePieceHztl(p, 1);
        }
        if(IsKeyDown(KEY_DOWN) && canMoveVert(grid, p, 1)){
            movePieceVert(p, 1);
        }

        if(IsKeyPressed(KEY_Q)){    // A en AZERTY
            while(rotateCollideLeft(p, false)){
                movePieceHztl(p, 1);
            }
            while(rotateCollideRight(p, false)){
                movePieceHztl(p, -1);
            }
            while(rotateCollideTop(p, false)){
                movePieceVert(p, 1);
            }
            while(rotateCollideBottom(p, false)){
                movePieceVert(p, -1);
            }
            while(rotateCollideGrid(grid, p, false)){
                movePieceVert(p, -1);
            }
            
            rotatePiece(grid, p, false);
        } 
        
        if(IsKeyPressed(KEY_W)){    // Z en AZERTY
            while(rotateCollideLeft(p, true)){
                movePieceHztl(p, 1);
            }
            while(rotateCollideRight(p, true)){
                movePieceHztl(p, -1);
            }
            while(rotateCollideTop(p, true)){
                movePieceVert(p, 1);
            }
            while(rotateCollideBottom(p, true)){
                movePieceVert(p, -1);
            }
            while(rotateCollideGrid(grid, p, true)){
                movePieceVert(p, -1);
            }

            rotatePiece(grid, p, true);
        }
    
        int score_prec = score;
        // Déplacer pièce toutes les 1s
        if(frameCounter%(60/level) == 0){
            if(canMoveVert(grid, p, 1)){
                movePieceVert(p, 1);
            }
            else{
                addPieceToGrid(p, grid);
                isFixed = true;

                score += removeLinesIfCompleted(grid);
                
            }
        }

        // Gestion des niveaux
        if(score_prec%10 > score%10 && level < 10){
            level++;
        }

        drawPiece(p);
        drawGrid(grid);

        frameCounter++;
        
        EndDrawing();
    }

    for(int x = 0; x < grid.width; x++){
        for(int y = 0; y < grid.height; y++){
            free(grid.grid[x][y]);
        }
        free(grid.grid[x]);
    }
    free(grid.grid);

    for(int i = 0; i < p.size; i++){
        free(p.squares[i]);
    }
    free(p.squares);

    CloseWindow();
    
    return EXIT_SUCCESS;
}