#ifndef SNAKE_H
#define SNAKE_H


#define _POSIX_C_SOURCE 199309L

#include <termios.h>
#include <unistd.h>

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

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

#include <errno.h>

#include <time.h>

#include <ctype.h>
#include <string.h>

#define RIGHT_CODE 'C'
#define LEFT_CODE 'D'
#define UP_CODE 'A'
#define DOWN_CODE 'B'

#define WIDTH 20
#define HEIGHT 20

#define MIN_SIZE 10
#define MAX_SIZE 100

#define STRING_LEN 20

#define SCORE_LEN 10
#define SCORE_NUMBER 10

#define SPEED 50 /* in ms */

#define MAX_LENGTH 1000
#define STARTING_LENGTH 2

#define SETTINGS_FILE "settings"
#define SCORE_FILE "score"
#define SETTINGS_FOLDER "snake_c"

typedef enum { NEW_GAME, SETTINGS, EXIT, MAIN_MENU } main_menu;

typedef enum {HEIGHT_S, WIDTH_S, SPEED_S, BACK_S, SETTINGS_MENU} settings;

typedef enum {V_FAST, FAST, AVERAGE, SLOW, V_SLOW, SPEED_L} speed;

typedef enum {
  UP, LEFT, DOWN, RIGHT, DIRECTIONS,
  ENTER, P_KEY, Q_KEY, ESCAPE
} direction;

typedef enum {
  AIR, FRUIT, HEAD_1, HEAD_2, HORIZONTAL, VERTICAL, 
  TOP_RIGHT, TOP_LEFT, BOTTOM_RIGHT, BOTTOM_LEFT
} board_state;

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

typedef struct snake_part {
  selected coords;
  direction directions[2];
} snake_part;

typedef struct snake {
  snake_part * position;
  uint16_t length;
} snake;

typedef struct score {
  char name[SCORE_LEN];
  uint16_t points;
} score;

uint8_t update_snake (selected game_size, snake *my_snake, direction shift);
void update_field(snake my_snake, selected game_size, 
  board_state **board, bool init);
selected new_fruit(selected game_size, board_state **board);

bool start_animation();

bool draw_menu(main_menu menu, struct winsize window_size);
void draw_settings(settings selected_setting, speed game_speed, selected game_size);

bool draw_board(selected game_size, board_state **board,
  struct winsize window_size, bool redraw, bool pause, uint16_t score);

void draw_pause(struct winsize window_size, 
  uint8_t field_width,uint8_t field_height);

char *give_state(board_state square);

direction read_direction(char *string, bool wasd_enabled);

uint8_t make_folder(char *path);

int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number);

#endif
