#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 */

void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
    for (int i = 0; i < searchFieldLen; i++)
    {
        for (int j = 0; j < searchFieldLen; j++)
        {
            if (salad[i][j] == EMPTY_CHAR) {
                salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
            }
        }
    }
}

// Creates the word salad by placing words randomly and filling empty spaces
//   - 2D array salad -> fertiger Wortsalat
//   - searchFieldLen -> Dimension der Salatschüssel
//   - 2D array words -> gegebene Wörter
//   - wordCount      -> anzahl an Wörtern
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
    srand(time(NULL));
    int placedWords = 0;

    //Feld komplettmit EMPTY_CHAR füllen
    for (int i = 0; i < searchFieldLen; i++)
    {
        for (int j = 0; j < searchFieldLen; j++)
        {
            salad[i][j] = EMPTY_CHAR;
        }
    }

    //Wörter plazieren
    for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
    {
        int wordLength = strlen(words[wordNumber]);
        int placed = 0;

        for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
        {
            int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal

            int x = rand() % searchFieldLen;
            int y = rand() % searchFieldLen;

            if (horizontal)
            {
                if (x + wordLength > searchFieldLen) continue; // passt nicht

                //Prüfen ob kein anderes Wort im Weg
                int fits = 1;
                for (int i = 0; i < wordLength; i++)
                {
                    if (salad[y][x + i] != EMPTY_CHAR)
                    {
                        fits = 0;
                        break;
                    }
                }

                //Wort plazieren
                if (fits)
                {
                    for (int i = 0; i < wordLength; i++)
                    {
                        salad[y][x + i] = words[wordNumber][i];
                    }
                    placed = 1;
                    placedWords++;
                }
            }
            else //Wort vertikal plazieren
            {
                if (y + wordLength > searchFieldLen) continue;

                //Prüfen ob kein anderes Wort im Weg
                int fits = 1;
                for (int i = 0; i < wordLength; i++)
                {
                    if (salad[y + i][x] != EMPTY_CHAR)
                    {
                        fits = 0;
                        break;
                    }
                }

                //Wort plazieren
                if (fits)
                {
                    for (int i = 0; i < wordLength; i++)
                    {
                        salad[y + i][x] = words[wordNumber][i];
                    }
                    placed = 1;
                    placedWords++;
                }
            }
        }
    }
    fillRestWithRandom(salad, searchFieldLen);
    return placedWords;
}

// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
   for( int i = 0; i < searchFieldLen; i++ )
   {
      for( int j = 0; j < searchFieldLen; j++ )
      {
         printf("%c", salad[i][j]);
      }
   }
}
