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

// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.

// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
    int wordCount = 0;
    char line[MAX_WORD_LEN];

    while (wordCount < maxWordCount && fgets(line, MAX_LINE_LEN, file) != NULL){

        line[strcspn(line, "\n")] ='\0';

        /*strncpy(words[wordCount], tempBuffer, MAX_WORD_LEN -1);
        words[wordCount][MAX_WORD_LEN - 1] = '\0';*/
        const char *teiler = ";,\n ";
        char *token;

        token = strtok(line, teiler);

        while (token != NULL){
            strncpy(words[wordCount], token, MAX_WORD_LEN);
            words[wordCount][MAX_WORD_LEN-1] = '\0';
            for (int i = 0; words[wordCount][i] != '0'; i++){
                char currentLetter = words[wordCount][i];
                if (currentLetter >= 'a' && currentLetter <= 'z'){
                    words[wordCount][i] = currentLetter - ('a' - 'A');
                }
            }
            token = strtok(NULL, teiler);
            wordCount++;
        }

        /*for (int i = 0; words[wordCount][i] != 0 && i < MAX_WORD_LEN; i++) {

             char currentLetter = words[wordCount][i];
             if (currentLetter >= 'a' && currentLetter <= 'z'){
                 words[wordCount][i] = currentLetter - ('a'-'A');
             }
        }*/
    }

    return wordCount;
}