#ifndef MAIN_H

#define MAIN_H

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

#include <termios.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <time.h>
#include <unistd.h>
#include <poll.h>

#include <ctype.h>

#include <errno.h>


#define FIELD_WIDTH 10
#define FIELD_HEIGHT 21

#define BORDER_WIDTH 2
#define TOP_GAP 1

#define FULL_WIDTH (FIELD_WIDTH + BORDER_WIDTH * 2)
#define FULL_HEIGHT (FIELD_HEIGHT + BORDER_WIDTH)

#define FRAME 16742006

#define FALL 53
#define BLINK 12
#define SPIN 8
#define FALL_PROTECTION 50

#define LINES_LEVEL 10
#define MAX_LEVEL 20


typedef enum {
  EMPTY, I_PIECE, J_PIECE, L_PIECE, O_PIECE, S_PIECE, T_PIECE, Z_PIECE, TETRIS
} tetromino;

typedef enum {
  TOP, RIGHT, BOTTOM, LEFT
} direction;

typedef struct {
  tetromino name;
  bool piece[4][4];
  int8_t x;
  int8_t y;
  uint8_t size;
  direction rotation;
  uint8_t positions;
} piece;

typedef struct {
  int8_t x;
  int8_t y;
} coord;

typedef struct {
  uint8_t r;
  uint8_t g;
  uint8_t b;
} color;


void init_field();

void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piece);
void draw_pause();


piece init_piece(tetromino new_piece);

coord find_direction(piece current_piece, uint8_t x, uint8_t y);
  
bool valid_position(piece current_piece);

uint8_t level_delay();


extern tetromino playfield[FULL_HEIGHT][FULL_WIDTH];
extern uint8_t level;

#endif
