#include "tetris-struct.h"

Grid gridInit(){
    Grid g;
    g.width = GRID_WIDTH;
    g.height = GRID_HEIGHT;
    
    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.size = PIECE_SIZE;
    p.squares = calloc(p.size, sizeof(Square));

    return p;
}

Square squareInit(){
    Square sq = malloc(sizeof(square_s));
    
    sq->color = RAYWHITE;
    sq->posX = 0;
    sq->posY = 0;
    

    return sq;
}

void setSquareColor(Square sq, int colorIndex){
    Color colorList[7] = {SKYBLUE, YELLOW, PURPLE, ORANGE, BLUE, RED, GREEN};

    sq->color = colorList[colorIndex];
}

void setColor(Piece p, int colorIndex){
    for(int i = 0; i < p.size; i++){
        setSquareColor(p.squares[i], colorIndex);
    }
}

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;
        }
    }
    return height;
}

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

    while(height > 0 && column[size-height] == NULL){
        height--;
    }
    return height;
}

bool canMove(Piece piece, Grid grid){

    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;

    char text3[5];
    itoa(y, text3, 50);
    DrawText(text3, 400, 20, 20, RED);

    if(y < g.height-1){
        if(y < g.height-heightColumn(g.grid[x], g.height)-1){
            return true;
        }
        return false;
    }
    return false;
}

void rotateSquare(Square sq, Square ref, bool rotation){
    int refX = ref->posX;
    int refY = ref->posY;

    // Keep memory
    int posX = sq->posX;
    int posY = sq->posY;

    if(rotation){
        sq->posX = refX - posY + refY;
        sq->posY = refY + posX - refX;
    }
    else{
        sq->posX = refX + posY - refY;
        sq->posY = refY - posX + refX; 
    }
}

void rotatePiece(Piece p, bool rotation){
    for(int i = 0; i < p.size; i++){
        if(i != 1){
            rotateSquare(p.squares[i], p.squares[1], rotation);
        }
    }

    
}

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){
    Square newSq = copySquare(sq);
    
    g.grid[newSq->posX][newSq->posY] = newSq;
}

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(char* fileName){

    FILE* dataFile = NULL;
    unsigned int numPieces;

    
    dataFile = fopen(fileName, "r");
    if(dataFile == NULL){
        exit(EXIT_FAILURE);
    }

    // Initialize number of pieces
    fscanf(dataFile, "%u\n", &numPieces);

    // Initialize piecesList
    char* line = calloc(30, sizeof(char));
    Piece* pieceList = calloc(numPieces, sizeof(Piece));
    for(int i = 0; i < numPieces; i++){
        pieceList[i] = pieceInit();
    }

    for(int i = 0; i < numPieces; i++){
        fgets(line, 30, dataFile);
        pieceList[i] = getPieceFromLine(line);
        setColor(pieceList[i], i);
    }
    free(line);

    return pieceList;
}

Square copySquare(Square sq){
    Square newSq = squareInit();

    newSq->color = sq->color;
    newSq->posX = sq->posX;
    newSq->posY = sq->posY;

    return newSq;
}

Piece generePiece(Piece* pieceList){
    int numPiece = randInt(0, 6);

    Piece pieceFromList = pieceList[numPiece];
    Piece newPiece = pieceInit();
    
    for(int i = 0; i < newPiece.size; i++){
        newPiece.squares[i] = copySquare(pieceFromList.squares[i]);
    }
    
    return newPiece;
}

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);
    }
}

void moveSquareVert(Square sq, int shift){
    sq->posY += shift;
}

void movePieceVert(Piece piece, int shift){
    for(int i = 0; i < piece.size; i++){
        moveSquareVert(piece.squares[i], shift);
    }
    char text3[5];
        itoa(shift, text3, 20);
        DrawText(text3, 450, 20, 20, RED);
}

bool canMoveHztl(Grid g, Piece p, int moveNum){
    for(int i = 0; i < p.size; i++){
        int x = p.squares[i]->posX + moveNum;
        int y = p.squares[i]->posY;

        if(x < 0 || x >= g.width || g.grid[x][y] != NULL){
            return false;
        }
    }
    return true;
}

void moveSquareHztl(Square sq, int shift){
    sq->posX += shift;
}

void movePieceHztl(Piece p, int shift){
    for(int i = 0; i < p.size; i++){
        moveSquareHztl(p.squares[i], shift);
    }
}

void removeLinesIfCompleted(Grid grid){
    bool isLineCompleted;

    int y = 0;
    while(y < grid.height){
        isLineCompleted = true;
        for(int x = 0; x < grid.width; x++){
            if(grid.grid[x][y] == NULL){
                isLineCompleted = false;
            }
        }

        if(isLineCompleted){
            int lineToRemove = y;

            //free
            for(int x = 0; x < grid.width; x++){
                free(grid.grid[x][lineToRemove]);
            }
            //shift
            for(int x = 0; x < grid.width; x++){
                for(int i = lineToRemove; i > 0; i--){
                    grid.grid[x][i] = grid.grid[x][i-1];
                }   
            }
            //initTop
            for(int x = 0; x < grid.width; x++){
                grid.grid[x][0] = malloc(sizeof(square_s));
                grid.grid[x][0] = NULL;
            }
        }
        else{
            y++;
        }
    }
}

void displayColumn(Square* line, int index, int height){
    for(int y = 0; y < height; y++){
        if(line[y] != NULL){
            DrawText("1", 500 + 30*index, 20 + 30*y, 20, RAYWHITE);
        }
    }
}

void displayGrid(Grid g){
    for(int x = 0; x < g.width; x++){
        displayColumn(g.grid[x], x, g.height);
    }
}