#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "raylib.h"

#define PIECE_SIZE 4
#define GRID_WIDTH 10
#define GRID_HEIGHT 20

typedef struct square_s{
    int posX;
    int posY;

    Color color;

} square_s;
typedef square_s* Square;

typedef struct Grid{
    Square** grid;

    int width;
    int height;
} Grid;

typedef struct Piece{
    Square* squares;
    int size;
} Piece;

Grid gridInit();
Grid initSquares(Grid g);
Piece pieceInit();

Square copySquare(Square sq);

bool gameOver(Grid grid);
int height(Grid g);
int heightColumn(Square* column, int size);

bool canMove(Piece piece, Grid grid);
bool canMoveSquare(Square sq, Grid g);

void rotateSquare(Square sq, Square ref, bool rotation);
void rotatePiece(Grid g, Piece p, bool rotation);       // False makes a counterclockwise rotation and true makes a clockwise rotation

void addPieceToGrid(Piece piece, Grid grid);
void addSquareToGrid(Square sq, Grid g);

int randInt(int lower, int upper);
Square getSquare(char* line, int index);
Piece getPieceFromLine(char* line);
Piece* getPiecesData(char* fileName);
Piece generePiece(Piece* pieceList);

void displaySquare(Square sq, int pIndex, int pListIndex);
void displayPiece(Piece p, int pListIndex);
void displayPieceList(Piece* pList, int size);

void moveSquareVert(Square sq, int shift);
void movePieceVert(Piece piece, int shift);

bool canMoveHztl(Grid g, Piece p, int moveNum);
void moveSquareHztl(Square sq, int shift);
void movePieceHztl(Piece p, int shift);

int removeLinesIfCompleted(Grid grid);

void displayColumn(Square* line, int index, int height);
void displayGrid(Grid g);

bool rotateBorderLeft(Piece p, bool rotation);
bool rotateBorderRight(Piece p, bool rotation);
bool rotateCollideGrid(Grid g, Piece p, bool rotation);