#include "game.h"
#include "print.h"
#include "keydriver.h"
#include "strings.h"
#include "stdint.h"

// Define constants for the game
#define ROOM_COUNT 5
#define INVENTORY_SIZE 5

// Global variable to hold the game state
static GameState game_state;

// Room descriptions and connected rooms
typedef struct {
    const char* description;
    int north;
    int south;
    int east;
    int west;
} Room;

static const Room rooms[ROOM_COUNT] = {
    { "You are in a dark forest. There are paths to the north and east.", 1, -1, 2, -1 },
    { "You are on a beach. There is a cave to the west.", -1, 0, -1, 3 },
    { "You are inside a cave. It’s damp and dark.", -1, -1, -1, 0 },
    { "You are in a small village. There's a shop to the north and a forest to the east.", 4, -1, -1, -1 },
    { "You are in a shop filled with various items. The village is to the south.", -1, 3, -1, -1 }
};

// Player's inventory
static const char* inventory[INVENTORY_SIZE];
static int inventory_count = 0;

// Command buffer
static char commandBuffer[MAX_COMMAND_LENGTH];
static size_t commandLength = 0;

bool keydown = false;

// Function to handle user input commands
void handle_command(const char* command) {
    
    
    game_clear();
    
    
    if (strcmp(command, "look") == 0) {
        printf(rooms[game_state.current_room].description);
        printf("\n");
    } else if (strcmp(command, "north") == 0) {
        if (rooms[game_state.current_room].north != -1) {
            game_state.current_room = rooms[game_state.current_room].north;
            printf("You walk north.\n");
            printf("%s\n", rooms[game_state.current_room].description);
        } else {
            printf("You can't go that way.\n");
        }
    } else if (strcmp(command, "south") == 0) {
        if (rooms[game_state.current_room].south != -1) {
            game_state.current_room = rooms[game_state.current_room].south;
            printf("You walk south.\n");
            printf("%s\n", rooms[game_state.current_room].description);
        } else {
            printf("You can't go that way.\n");
        }
    } else if (strcmp(command, "east") == 0) {
        if (rooms[game_state.current_room].east != -1) {
            game_state.current_room = rooms[game_state.current_room].east;
            printf("You walk east.\n");
            printf("%s\n", rooms[game_state.current_room].description);
        } else {
            printf("You can't go that way.\n");
        }
    } else if (strcmp(command, "west") == 0) {
        if (rooms[game_state.current_room].west != -1) {
            game_state.current_room = rooms[game_state.current_room].west;
            printf("You walk west.\n");
            printf("%s\n", rooms[game_state.current_room].description);
        } else {
            printf("You can't go that way.\n");
        }
    } else if (strcmp(command, "inventory") == 0) {
        printf("You are carrying:\n");
        for (int i = 0; i < inventory_count; ++i) {
            printf("- %s\n", inventory[i]);
        }
        if (inventory_count == 0) {
            printf("Nothing.\n");
        }
    } else if (strcmp(command, "take key") == 0) {
        if (game_state.current_room == 1) {
            if (inventory_count < INVENTORY_SIZE) {
                inventory[inventory_count++] = "key";
                printf("You picked up a key.\n");
            } else {
                printf("Your inventory is full.\n");
            }
        } else {
            printf("There is no key here.\n");
        }
    } else if (strcmp(command, "use key") == 0) {
        bool has_key = false;
        for (int i = 0; i < inventory_count; ++i) {
            if (strcmp(inventory[i], "key") == 0) {
                has_key = true;
                break;
            }
        }
        if (has_key && game_state.current_room == 2) {
            printf("You unlock the door in the cave and find a treasure!\n");
            game_state.game_active = false;  // End the game
        } else {
            printf("You can't use the key here.\n");
        }
    } else if (strcmp(command, "quit") == 0) {
        printf("You quit the game.\n");
        game_state.game_active = false;
    } else {
        printf("Unknown command.\n");
    }
}

// Function to print the prompt for the player
void print_prompt() {
    printf(" > ");
}

const void game_clear()
{
    print_set_color(PRINT_COLOR_WHITE, PRINT_COLOR_BLACK); // Set text color

    move_print_cursor(0, 0);
    move_cursor(0, 0);
    print_clear();
}

// Function to initialize and run the game
void create_game() {
    // Initialize the game state
    game_state.current_room = 0;
    game_state.game_active = true;

    printf("Welcome to the text adventure game!\n");
    printf("Type 'look' to see where you are, 'north', 'south', 'east', 'west' to move, or 'quit' to exit.\n");
    printf("You can 'take key' or 'use key' in the appropriate places.\n");

    print_prompt();

    // Main game loop
    while (game_state.game_active) {
        char key = read_key();  // Get key input
        
        if (key != 0) {
            if (!keydown) {
                if (key == 10) {  // Enter key
                    printf("\n");
                    commandBuffer[commandLength] = '\0';  // Null-terminate the command
                    handle_command(commandBuffer);  // Process the command
                    commandLength = 0;  // Reset command buffer
                    if (game_state.game_active) {
                        print_prompt();  // Print prompt for the next command
                    }
                } else if (key == 8) {  // Backspace key
                    if (commandLength > 0) {
                        commandLength--;
                        offset_print_cursor(-1, 0);
                        print_char(' ');  // Overwrite the character
                        offset_print_cursor(-1, 0);
                    }
                } else if (key >= 32 && key <= 126) {  // Printable characters
                    if (commandLength < MAX_COMMAND_LENGTH - 1) {
                        commandBuffer[commandLength++] = key;
                        print_char(key);  // Display the key on the screen
                    }
                }
                keydown = true;
            }
        } else {
            keydown = false;
        }
    }

    printf("Thanks for playing!\n");
}
