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

#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0

// einmalige Initialisierung des RNG
static void initRandom(void)
{
    static int seeded = 0;
    if (!seeded)
    {
        srand((unsigned int)time(NULL));
        seeded = 1;
    }
}

static int placeWordHorizontal(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
                               unsigned int fieldLen,
                               const char *word)
{
    unsigned int wordLen = (unsigned int)strlen(word);
    if (wordLen > fieldLen)
        return 0;

    // zufällige Zeile, passende Startspalte
    unsigned int row = (unsigned int)(rand() % fieldLen);
    unsigned int maxStartCol = fieldLen - wordLen;
    unsigned int col = (unsigned int)(rand() % (maxStartCol + 1));

    // Kollisionen prüfen
    for (unsigned int i = 0; i < wordLen; i++)
    {
        if (salad[row][col + i] != EMPTY_CHAR)
            return 0;
    }

    // setzen
    for (unsigned int i = 0; i < wordLen; i++)
        salad[row][col + i] = word[i];

    return 1;
}

static int placeWordVertical(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
                             unsigned int fieldLen,
                             const char *word)
{
    unsigned int wordLen = (unsigned int)strlen(word);
    if (wordLen > fieldLen)
        return 0;

    // zufällige Spalte, passende Startzeile
    unsigned int col = (unsigned int)(rand() % fieldLen);
    unsigned int maxStartRow = fieldLen - wordLen;
    unsigned int row = (unsigned int)(rand() % (maxStartRow + 1));

    // Kollisionen prüfen
    for (unsigned int i = 0; i < wordLen; i++)
    {
        if (salad[row + i][col] != EMPTY_CHAR)
            return 0;
    }

    // setzen
    for (unsigned int i = 0; i < wordLen; i++)
        salad[row + i][col] = word[i];

    return 1;
}

int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
                    unsigned int searchFieldLen,
                    const char words[][MAX_WORD_LEN],
                    unsigned int wordCount)
{
    initRandom();

    // 1) Feld leeren
    for (unsigned int r = 0; r < searchFieldLen; r++)
        for (unsigned int c = 0; c < searchFieldLen; c++)
            salad[r][c] = EMPTY_CHAR;

    unsigned int placedWords = 0;

    // 2) Wörter platzieren (horizontal oder vertikal, max. 10 Versuche/Wort)
    for (unsigned int w = 0; w < wordCount; w++)
    {
        const char *word = words[w];
        int placed = 0;

        for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
        {
            int dir = rand() % 2; // 0: horizontal, 1: vertikal
            if (dir == 0)
                placed = placeWordHorizontal(salad, searchFieldLen, word);
            else
                placed = placeWordVertical(salad, searchFieldLen, word);
        }

        if (placed)
            placedWords++;
    }

    // 3) restliche Felder mit A–Z 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] = (char)('A' + (rand() % 26));

    return (int)placedWords;
}

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++)
        {
            putchar(salad[r][c]);
            putchar(' ');
        }
        putchar('\n');
    }
}