#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>

#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0

//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
   * restliche Felder mit zufälligen Buchstaben füllen */

// Creates the word salad by placing words randomly and filling empty spaces
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
    if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) {
        return 0;
    }

    // Start with empty field
    for (unsigned int r = 0; r < searchFieldLen; ++r) {
        for (unsigned int c = 0; c < searchFieldLen; ++c) {
            salad[r][c] = EMPTY_CHAR;
        }
    }

    // Platzieren der Wörter
    for (unsigned int w = 0; w < wordCount; ++w) {
        const char *word = words[w];
        size_t wordLen = strlen(word);
        if (wordLen == 0 || wordLen > searchFieldLen) {
            continue;
        }

        int placed = 0;
        for (int attempts = 0; attempts < MAX_RAND_TRIES_PER_WORD && !placed; ++attempts) {
            int vertical = rand() % 2; // 0=horizontal, 1=vertical
            unsigned int row, col;

            if (vertical) {
                row = rand() % (searchFieldLen - wordLen + 1);
                col = rand() % searchFieldLen;
            } else {
                row = rand() % searchFieldLen;
                col = rand() % (searchFieldLen - wordLen + 1);
            }

            // Check if placeable
            int ok = 1;
            for (size_t i = 0; i < wordLen; ++i) {
                unsigned int r = row + (vertical ? i : 0);
                unsigned int c = col + (vertical ? 0 : i);
                char current = salad[r][c];
                if (current != EMPTY_CHAR && current != word[i]) {
                    ok = 0;
                    break;
                }
            }
            if (!ok) {
                continue;
            }

            // Set letters
            for (size_t i = 0; i < wordLen; ++i) {
                unsigned int r = row + (vertical ? i : 0);
                unsigned int c = col + (vertical ? 0 : i);
                salad[r][c] = word[i];
            }
            placed = 1;
        }

        if (!placed) {
            // Ein Wort konnte nicht platziert werden, Abbruch möglich
            return 0;
        }
    }

    // Rest mit zufälligen Buchstaben füllen
    for (unsigned int r = 0; r < searchFieldLen; ++r) {
        for (unsigned int c = 0; c < searchFieldLen; ++c) {
            if (salad[r][c] == EMPTY_CHAR) {
                salad[r][c] = 'A' + (rand() % 26);
            }
        }
    }

    return 1;
}

// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
    for (unsigned int r = 0; r < searchFieldLen; ++r) {
        for (unsigned int c = 0; c < searchFieldLen; ++c) {
            char ch = salad[r][c];
            if (ch == EMPTY_CHAR) {
                ch = '.';
            }
            putchar(ch);
            if (c + 1 < searchFieldLen) {
                putchar(' ');
            }
        }
        putchar('\n');
    }
}

