#include "tetris-struct.h"

Grid gridInit(){
    Grid g;
    g.grid = calloc(g.width, sizeof(Square*));

    for(int i = 0; i < g.width; i++){
        g.grid[i] = calloc(g.height, sizeof(Square));
    }
    
    return g;
}

Piece pieceInit(){
    Piece p;
    p.squares = calloc(p.size, sizeof(Square));
    p.size = PIECE_SIZE;

    return p;
}

Square squareInit(){
    Square sq = malloc(sizeof(Square));
    sq->color = RAYWHITE;

    return sq;
}

bool gameOver(Grid grid){
    return height(grid) == grid.height;
}

int height(Grid g){
    int height = 0;
    int height_p;

    for(int i = 0; i < g.width; i++){
        height_p = heightColumn(g.grid[i], g.height);

        if(height_p > height){
            height = height_p;
        }
    }
}

int heightColumn(Square* column, int size){
    int height = 0;
    int y;

    for(int i = 0; i < size; i++){
        if(column[i] != NULL){
            y = column[i]->posY; 
        
            if(y+1 > height){
                height = y+1;
            }
        }
        
    }

    return height;
}

bool canMove(Piece piece, Grid grid){
    int x, y;

    for(int i = 0; i < piece.size; i++){
        if(!canMoveSquare(piece.squares[i], grid)){
            return false;
        }
    }
    return true;
}

bool canMoveSquare(Square sq, Grid g){
    int x = sq->posX;
    int y = sq->posY;
    
    return (y != 0 || y == heightColumn(g.grid[x], g.height));
}

void move(Piece piece){
    for(int i = 0; i < piece.size; i++){
        moveSquare(piece.squares[i]);
    }
}

void moveSquare(Square sq){
    sq->posY --;
}

void addPieceToGrid(Piece piece, Grid grid){
    for(int i = 0; i < piece.size; i++){
        addSquareToGrid(piece.squares[i], grid);
    }
}

void addSquareToGrid(Square sq, Grid g){
    int x = sq->posX;
    int y = sq->posY;
    
    g.grid[x][y] = sq;
}

int randInt(int lower, int upper){
    srand(time(0));
    int num = (rand() % (upper-lower+1)) + lower;

    return num;
}

Square getSquare(char* line, int index){
    Square sq = squareInit();
    int k = 0;      // Compteur de '('
    int shift = 0;  // Décalage d'indice

    while(k != index){
        if(line[shift] == '('){
            k++;
        }
        shift+=5;
    }
    shift++;        // On obtient l'indice du premier chiffre
    
    int x = line[shift] - '0';
    int y = line[shift+2] - '0';
    
    sq->posX = x;
    sq->posY = y;

    return sq;
}

Piece getPieceFromLine(char* line){
    Piece p = pieceInit();
    Square sq;
    DrawText(line, 50, 50, 20, RAYWHITE);
    
    for(int i = 0; i < p.size; i++){
        sq = getSquare(line, i);
        p.squares[i] = sq;
        
        char text0[5];
        char text1[5];
            
        itoa(sq->posX, text0, 10);
        itoa(sq->posY, text1, 10);
            
        DrawText(text0, 50, 100+50*i, 20, RAYWHITE);
        DrawText(text1, 100, 100+50*i, 20, RAYWHITE);
    }
    return p;
}

Piece* getPiecesData(){
    Piece* pieceList;

    char* fileName = "tetris.data";
    FILE* dataFile;
    dataFile = fopen(fileName, "r");

    unsigned int pieceNumber;
    fscanf(dataFile, "%u\n", &pieceNumber);

    char* line = calloc(22, sizeof(char));
    pieceList = calloc(pieceNumber, sizeof(Piece));
    for(int i = 0; i < pieceNumber; i++){
        pieceList[i] = pieceInit();
        fgets(line, 22, dataFile);
        pieceList[i] = getPieceFromLine(line);
    }
    free(line);

    return pieceList;
}

Piece generePiece(Piece* pieceList){
    int numPiece = randInt(0, 6);
    DrawText("test", 20, 20, 20, RED);
    Piece p = pieceList[numPiece];
    
    return p;
}

void gameInstructions(Grid grid, Piece* pList){
    Piece piece = generePiece(pList);

    drawPiece(piece);

    while(canMove(piece, grid)){
        move(piece);
    }

    addPieceToGrid(piece, grid);
}

void displaySquare(Square sq, int pIndex, int pListIndex){
    char text0[5];
    char text1[5];
            
    itoa(sq->posX, text0, 10);
    itoa(sq->posY, text1, 10);
            
    DrawText(text0, 20 + 80*pIndex, 20 + 80*pListIndex, 20, RAYWHITE);
    DrawText(text1, 60 + 80*pIndex, 20 + 80*pListIndex, 20, RAYWHITE);
}

void displayPiece(Piece p, int pListIndex){
    for(int i = 0; i < p.size; i ++){
        displaySquare(p.squares[i], i, pListIndex);
    }
}

void displayPieceList(Piece* pList, int size){
    for(int i = 0; i < size; i++){
        displayPiece(pList[i], i);
    }
}