#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)
{
   int direction = 0;
   int step = 0;
   int added = 0;
   int addedWords = 0;
   int positionX = 0;
   int positionY = 0;

   srand(time(NULL));

   for(int i = 0; i < searchFieldLen; i++)
   {
     for(int j = 0; j < searchFieldLen; j++)
      {
          salad[i][j] = EMPTY_CHAR;
      }
   }
   while(step <= wordCount)
   {
       int tries = 0;
       int wordLength = 0;
       direction = rand() % 1;

       for (int i = 0; i < MAX_WORD_LEN; i++)
       {
           if (words[step][i] == '\0')
               break;
           wordLength++;
       }
       if(direction == 0)
       {
         while(added <= 0 && tries < MAX_RAND_TRIES_PER_WORD){
         positionY = rand() % searchFieldLen-1;
             positionX = rand() % searchFieldLen-1;
         if(searchFieldLen - positionX >= wordLength)
           {

               for(int i = 0; i < searchFieldLen; i++)
               {
                 if(salad[positionY][positionX+i] != EMPTY_CHAR)
                   {
                       tries++;
                       break;
                   }
                for (int j = 0; j < searchFieldLen; j++)
                {
                       salad[positionY][positionX+j] = words[step][j];
                       added = 1;
                }
               }

           }
            else
            {
                tries++;
            }
          }
           step++;
           tries = 0;
           added = 0;
       }
       if(direction == 1)
       {
           while(added <= 0 && tries < MAX_RAND_TRIES_PER_WORD){
               positionY = rand() % searchFieldLen-1;
               positionX = rand() % searchFieldLen-1;
               if(searchFieldLen - positionY >= wordLength)
               {

                   for(int i = 0; i < searchFieldLen; i++)
                   {
                       if(salad[positionY+i][positionX] != EMPTY_CHAR)
                       {
                           tries++;
                           break;
                       }
                       for (int j = 0; j < searchFieldLen; j++)
                       {
                           salad[positionY+j][positionX] = words[step][j];
                           added = 1;
                       }
                   }

               }
               else {
                   tries++;
               }
               step++;
               tries = 0;
               added = 0;
           }

           }

   return addedWords;
}

// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{

}
