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

#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0

void emptyArray();
int placeWord();
void fillEmptySpots();

//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)
{
   srand(time(NULL));
   emptyArray(salad,MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
   placeWord(salad,words);
   fillEmptySpots(salad, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
}

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

void emptyArray(char array[][], int arrayLength)
{
   char* element = (char*) &array;
   for(int i = 0; i < arrayLength; i++ )
   {
      *element = EMPTY_CHAR;
      element++;
   }
}

int placeWord(char intoArray[][], char insertedWord[][])
{
   int numberWord;
   int tries;
   while(tries <= MAX_RAND_TRIES_PER_WORD)
   {
      int xCord = rand() % MAX_SEARCH_FIELD_LEN;
      int yCord = rand() % MAX_SEARCH_FIELD_LEN;
      int isHorizontal = rand() % 2;
      unsigned int wordLength = strlen(insertedWord[numberWord][0]);
      if(isHorizontal)
      {
         if(xCord + sizeof(insertedWord[numberWord][0]) <= MAX_SEARCH_FIELD_LEN)
         {
            
         }
      
      }
   }
}

void fillEmptySpots(char array[][], int arrayLength)
{
   char* element = (char*) &array;
   for(int i = 0; i < arrayLength; i++ )
   {
      if(*element == EMPTY_CHAR)
      {
         *element == rand() % ('Z' - 'A' + 1) + 'A';
      }
      element++;
   }
  
}