#ifndef MAIN_C

#define MAIN_C

#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 <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 17000000

#define FALL 58
#define BLINK 12

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;



void init_field();

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

piece init_piece(tetromino new_piece);

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



extern tetromino playfield[FULL_HEIGHT][FULL_WIDTH];


#endif
