#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)
{
    // Zufall initialisieren
    srand(time(NULL));

    // 1. Spielfeld leeren
    for (unsigned int i = 0; i < searchFieldLen; i++)
    {
        for (unsigned int j = 0; j < searchFieldLen; j++)
        {
            salad[i][j] = EMPTY_CHAR;
        }
    }

    // 2. Wörter platzieren
    for (unsigned int w = 0; w < wordCount; w++)
    {
        const char *word = words[w];
        unsigned int wordLen = strlen(word);

        for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++)
        {
            // zufällige Startposition
            int row = rand() % searchFieldLen;
            int col = rand() % searchFieldLen;

            // zufällige Richtung: 0 = horizontal, 1 = vertikal
            int direction = rand() % 2;

            int canPlace = 1;

            // Nur prüfen, ob Buchstaben passen (gleiche erlaubt, leer erlaubt)
            for (unsigned int k = 0; k < wordLen; k++)
            {
                int r = row + (direction == 1 ? k : 0);
                int c = col + (direction == 0 ? k : 0);

                // Wenn außerhalb des Feldes -> abbrechen
                if (r >= (int)searchFieldLen || c >= (int)searchFieldLen)
                {
                    canPlace = 0;
                    break;
                }

                // Wenn belegt mit anderem Buchstaben -> abbrechen
                if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[k])
                {
                    canPlace = 0;
                    break;
                }
            }

            // Wenn möglich, das Wort setzen
            if (canPlace)
            {
                for (unsigned int k = 0; k < wordLen; k++)
                {
                    int r = row + (direction == 1 ? k : 0);
                    int c = col + (direction == 0 ? k : 0);
                    if (r < (int)searchFieldLen && c < (int)searchFieldLen)
                        salad[r][c] = word[k];
                }
                break; // Wort erfolgreich platziert
            }
        }
    }

    // 3. Leere Felder mit Zufallsbuchstaben füllen
    for (unsigned int i = 0; i < searchFieldLen; i++)
    {
        for (unsigned int j = 0; j < searchFieldLen; j++)
        {
            if (salad[i][j] == EMPTY_CHAR)
            {
                salad[i][j] = 'A' + (rand() % 26);
            }
        }
    }

    return 0;
}

// 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 i = 0; i < searchFieldLen; i++)
    {
        for (unsigned int j = 0; j < searchFieldLen; j++)
        {
            printf("%c ", salad[i][j]);
        }
        printf("\n");
    }
}

