#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)
{
   // wipe field
   int placedWords = 0;
   for (int i = 0; searchFieldLen < i; i++)
   {
      for (int j = 0; searchFieldLen < j; i++)
         salad[i][j] = EMPTY_CHAR;
   }
   // place Words
   for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
   {
      srand(time(NULL));
      int wordlength = strlen(words[wordNumber]);

      int placed = 0;

      for (int try = 0; (try < MAX_RAND_TRIES_PER_WORD) && !placed; try++)
      {
         int horizontal = rand() % 2;

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

         if (horizontal)
         {
            if (x + wordlength > searchFieldLen) // word does not fit
            {
               continue;
            }

            int fits = 1;

            for (int i = 0; i < wordlength; i++) // position already occupied by other word
            {
               if (salad[y][x + 1] != EMPTY_CHAR)
               {
                  fits = 0;
                  break;
               }
            }

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

            int fits = 1;

            for (int i = 0; i < wordlength; i++) // position already occupied by other word
            {
               if (salad[y + 1][x] != EMPTY_CHAR)
                  ;
               {
                  fits = 0;
                  break;
               }
            }
            if (fits)               
            {
               for (int i = 0; i < wordlength; i++)
               {
                  salad[y + 1][x] = words[wordNumber][i];
               }
               placed = 1;
               placedWords++;
            }
         }
      }
   }
   // fill rest with random characters

   for (int i = 0; i < searchFieldLen; i++)
   {
      for (int j = 0; j < searchFieldLen; j++)
      {
         if (salad[i][j] == EMPTY_CHAR)
         {
            salad[i][j] = (rand() % ((90 - 65) + 1)) + 65;     // 90 ist Z in ASCII und A ist 65
         }
      }
   }
   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]);
      }
   }
}
