#include "raylib.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600

#define MATRIX_WIDTH  10
#define MATRIX_HEIGHT 40

#define BLOCK_SIZE 25

#define FALL_TICK 0.5

enum Phases {
    GENERATION,
    FALL,
    LOCK,
    PATTERN,
    MARK,
    ITERATE,
    ANIMATE,
    ELIMINATE,
    COMPLETE
} game_phase;

#include "tetromino.h"

struct {
    int pos_x;
    int pos_y;
    int rotation;
    int type;
} current_piece;

struct Color matrix[MATRIX_WIDTH][MATRIX_HEIGHT];
int piece_queue[2][TETROMINO_COUNT] = {
    {0, 1, 2, 3, 4, 5, 6},
    {0, 1, 2, 3, 4, 5, 6}
};
int current_bag = 0;
int current_piece_index = 0;

void draw_block(int x, int y, int width, int height, struct Color color)
{
    DrawRectangle(x, -y + SCREEN_HEIGHT - height, width, height, color);
}

void render_matrix(void)
{
    int matrix_origin_x = (SCREEN_WIDTH/2 - (MATRIX_WIDTH * BLOCK_SIZE))/2;
    int matrix_origin_y = (SCREEN_HEIGHT - MATRIX_HEIGHT/2 * BLOCK_SIZE)/2;

    /* draw matrix background */
    draw_block(
        matrix_origin_x - 1
        ,matrix_origin_y - 1
        ,MATRIX_WIDTH * BLOCK_SIZE + 2
        ,MATRIX_HEIGHT/2 * BLOCK_SIZE + 2
        ,GRAY
    );

    /* draw matrix */
    for (int i = 0; i < MATRIX_WIDTH; i++) {
        for (int j = 0; j < MATRIX_HEIGHT; j++) {
            draw_block(
                matrix_origin_x + i * BLOCK_SIZE + 1
                ,matrix_origin_y + j * BLOCK_SIZE + 1
                ,BLOCK_SIZE - 2
                ,BLOCK_SIZE - 2
                ,matrix[i][j]
            );
        }
    }

    /* draw current piece */
    for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
        for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
            if (tetrominos[current_piece.type].directions[current_piece.rotation][j][i] == 1) {
                draw_block(
                    (matrix_origin_x + ((current_piece.pos_x + i - 1) * BLOCK_SIZE)) + 1
                    ,(matrix_origin_y + ((current_piece.pos_y - j + 1) * BLOCK_SIZE)) + 1
                    ,BLOCK_SIZE - 2
                    ,BLOCK_SIZE - 2
                    ,tetrominos[current_piece.type].color
                );
            }
        }
    }
}

void shuffle_bag(int *bag)
{
    for (int i = 0; i < TETROMINO_COUNT; i++) {
        int r = i + rand() / (RAND_MAX / (TETROMINO_COUNT - i) + 1);
        int t = bag[r];
        bag[r] = bag[i];
        bag[i] = t;
    }
}

void generation_phase(void)
{
    // bag generation
    if (current_piece_index > TETROMINO_COUNT) {
        current_piece_index = 0;
        shuffle_bag(piece_queue[current_bag]);
        current_bag = !current_bag;
    }

    // set current piece
    current_piece.type = piece_queue[current_bag][current_piece_index++];
    current_piece.rotation = NORTH;
    current_piece.pos_x = 4;
    current_piece.pos_y = 20;
}

int main(void)
{
    //srand(time(NULL));
    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "~turnipGod's Tetris");

    // set all blocks to white
    for (int y = 0; y < 40; y++) {
        for (int x = 0; x < 10; x++) {
            matrix[x][y] = WHITE;
        }
    }

    shuffle_bag(piece_queue[0]);
    generation_phase();
    while (!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(WHITE);
        render_matrix();
        EndDrawing();
    }

    CloseWindow();

    return 0;
}