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

#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20

int main(int argc, char *argv[])
{
    int exitCode = EXIT_SUCCESS;

    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;
        unsigned int placedWords = 0; // <-- Hierher verschoben
        char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];

        FILE *file = fopen(argv[1], "r");

        if(file != NULL)
        {
            wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
            fclose(file);

            placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
        }
        else
        {
            fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
            exitCode = EXIT_FAILURE;
        }

        if (placedWords == wordCount)
        {
            startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
        }
        else
        {
            fprintf(stderr, "Nur %u von %u Wörtern konnten im Buchstabensalat platziert werden.\n", placedWords, wordCount);
            exitCode = EXIT_FAILURE;
        }
    }

    return exitCode;
}