#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "input.h"
#include "game.h"
#include "graphicalGame.h"

#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_WIDTH 800

int main(int argc, char *argv[])
{
    int exitCode = EXIT_SUCCESS;
    srand(time(NULL));
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
        exitCode = EXIT_FAILURE;
    } else {
        char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
        unsigned int wordCount = 0;

        FILE *file = fopen(argv[1], "r");
        if (file != NULL) {
            unsigned int placedWords = 0;
            char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];

            wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
            fclose(file);
            placedWords = createWordSalad(wordSalad, SALAD_SIZE, (const char(*)[MAX_WORD_LEN])words, wordCount);
            if (placedWords == wordCount) {
                printf("Info: %d Wörter geladen und platziert. Starte speil...\n", placedWords);
                showWordSalad(wordSalad, SALAD_SIZE);
                startGame(wordSalad, SALAD_SIZE, words, wordCount,WINDOW_WIDTH);
                printf("Spielbeendet.\n");
            } else {
                fprintf(stderr, "Fehler: Es konnten nur %d von %d Wörtern platziert werden.\n", placedWords, wordCount, wordCount);
                fprintf(stderr, "Das Spiel ist evtl. zu klein.\n");
                exitCode = EXIT_FAILURE;
            }

        } else {
            fprintf(stderr, "Datei konnte nicht geöffnet werden.\n");
            exitCode = EXIT_FAILURE;
        }
    }

    return exitCode;
}