#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct word_pair
{
	int pair_number;
	char *wordA, *wordB;
};
int *shuffledeck(int deck_size)
{
	int *deck;
	int unique;
	int i, j;

	deck = malloc(deck_size * sizeof(int));
	srandom((unsigned int) time(NULL));
	i = 0;
	while (i<deck_size)
	{
		deck[i] = random() % deck_size;
		unique = 1;
		for (j=0; j<i; j++)
		{
			if (deck[j] == deck[i])
			{
				unique = 0;
				break;
			}
		}
		if (0 != unique)
		{
			i++;
		}
	}
	return deck;
}
int *choosefromdeck(int deck_size, int repeat_limit, int selection_size, int selection_offset)
{
	int *selection;
	int unique;
	int i, j;

	/* check if this won't break for large values of selection_offset*/
	if (selection_size > ((long long) deck_size * repeat_limit))
	{
		return NULL;
	}
	selection = malloc(selection_size * sizeof(int));
	srandom((unsigned int) time(NULL));
	i = 0;
	while (i<selection_size)
	{
		selection[i] = (random() % deck_size) + selection_offset;
		unique = repeat_limit;
		for (j=0; j<i; j++)
		{
			if (selection[j] == selection[i])
			{
				unique--;
				if(0 == unique)
				{
					break;
				}
			}
		}
		if (unique <= 0)
		{
			i++;
		}
	}
	return selection;
}
void maingame(struct word_pair **main_list, int total_pairs, int number_of_answers)
{
//	struct word_pair *temp_pair;
	int *order, *answer_choices;
	int finished = 0;
	int i;
	char input;

	order = shuffledeck(total_pairs);
	while (0 == finished)
	{
		answer_choices = choosefromdeck(total_pairs, 1, number_of_answers, 0);

		input = getchar();
		if ('q' == input)
		{
			finished = 1;
			break;
		}
		for (i=0; i<total_pairs; i++)
		{
			if (order[i] < 0)
			{
				finished = 1;
			}
		}
	}
	return;
}
int main()
{
	struct word_pair **word_pair_list, *current_pair, **temp;
	int total_word_pairs;
	FILE *file_pointer;
	char filename[] = "example.txt";
	char buffer[100];
	char *current_character;
//	int error_check_int, *error_check_pointer;
//	int finished = 0;

	printf("\nCommand line vocabulary flash cards v5\n\n");
	file_pointer = fopen(filename, "r");
	if (NULL == file_pointer)
	{
	//	finished = 1;
		printf("Could not open file %s\n", filename);
		return 1;
	}

	int loop_count=0;

	total_word_pairs = 0;
	word_pair_list = NULL;
	while((current_character = fgets(buffer, 100, file_pointer)))
	{
		loop_count++;
		printf("Reading line %d\n", loop_count);
		fflush(stdout);
		if ('#' != buffer[0])
		{
			if ((current_character = strchr(buffer, '\r')))
			{
				*current_character = '\0';
			}
			if ((current_character = strchr(buffer, '\n')))
			{
				*current_character = '\0';
			}
			if ((current_character = strchr(buffer, '|')))
			{
				*current_character = '\0';
				current_character++;
				total_word_pairs++;
				temp = realloc(word_pair_list, total_word_pairs*sizeof(struct word_pair **));
				current_pair = malloc(sizeof(struct word_pair));
				if ((NULL == temp) || (NULL == current_pair))
				{
					printf("Could not allocate more memory for pair %d", total_word_pairs);
					total_word_pairs--;
				}
				else
				{
					word_pair_list = temp;
					word_pair_list[total_word_pairs - 1] = current_pair;
					current_pair->pair_number = total_word_pairs;
					if (0 == (strlen(buffer)))
					{
						asprintf(&current_pair->wordA, "Word %dA is blank", total_word_pairs);
						printf("%s\n", current_pair->wordA);
					}
					else
					{
						current_pair->wordA = strdup(buffer);
					}
					if (0 == (strlen(current_character)))
					{
						asprintf(&current_pair->wordB, "Word %dB is blank", total_word_pairs);
						printf("%s\n", current_pair->wordB);
					}
					else
					{
						current_pair->wordB = strdup(current_character);
					}
				}
			}
		}
	}
	printf("Reached End Of File\n");
	fflush(stdout);
	fclose(file_pointer);

	maingame(word_pair_list, total_word_pairs, 8);

	printf("Cleaning up\n");

	int i;
	
	for(i=0; i<total_word_pairs; i++)
	{
		printf("No. %-4d A: %-17s B: %s\n",
				word_pair_list[i]->pair_number, word_pair_list[i]->wordA, word_pair_list[i]->wordB);
		free(word_pair_list[i]->wordA);
		free(word_pair_list[i]->wordB);
		free(word_pair_list[i]);
		fflush(stdout);
	}
	free(word_pair_list);
	printf("Exiting\n");
	return 0;
}
