// main.cpp
// The code in main is not as compact as I like it to be.
// I couldn't figure out how to pass a templated list as
// a parameter.  Consequently, I had to implemented some
// of the functions in main.  I couldn't use a function
// to accomplish certain task because I couldn't pass 
// wordList as a parameter; therefore I had to write
// parts of the code more than once.  

#include "Word.h"
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "ListAr.cpp"

class Word;
class ListAr;
float ran2(long *idum);

const int TEMP_LENGTH = 20;

void wordMixer( Word &w )
{
	Word scrabbleWord( w );
	long wordLength = w.length();
	long seed = -100000;

	int *usedIndex =  new int[wordLength];
	for ( int u = 0; u < wordLength; u++ )
		usedIndex[u] = 1000; // an impossible num for wordLength
	
	for ( int i = 0; i < wordLength; i++ )
	{
		int ranNum = int ( ran2( &seed ) * wordLength );
		for ( int b = 0; b < wordLength; b++ )
		{
			if ( ranNum == usedIndex[b] )
			{
				int nextAvail;
				for ( nextAvail = wordLength-1; nextAvail >= 0; nextAvail-- )
				{
					try
					{
						usedIndex[nextAvail];
					}
					catch( int badIndex )
					{
						cerr << badIndex << "is out of range." << endl;
					}
					if ( usedIndex[nextAvail] == 1000 )
						ranNum = nextAvail;
				}
			}
		}
		usedIndex[ranNum] = ranNum;
		scrabbleWord[i] = w[ranNum];
	}
	cout << endl << scrabbleWord << endl;
}

char getChoiceIsPlayMenu()
{
	char c;
	cout << "\tEnter *skip* to skip to the next word.\n";
	cout << "\t***************************************\n"
		 << "\t*  Choose a word category             *\n"
		 << "\t*  Computer                         c *\n"
		 << "\t*  Math                             m *\n"
		 << "\t*  Physics                          p *\n"
		 << "\t*  Quit                             q *\n"
		 << "\t***************************************\n";
	cout << "Enter choice: ";
	cin >> c;
	return c;
}

int main()
{
	char choice = getChoiceIsPlayMenu();
	char tempWordHolder[TEMP_LENGTH];
	List<Word> wordList;
	char guessTemp[TEMP_LENGTH];
	Word skip( "skip" );

	switch ( choice )
	{
	case 'c':
		{
			ifstream infile( "Computer.txt", ios::nocreate|ios::in );
			while ( infile >> tempWordHolder )
			{
				Word newWord( tempWordHolder );
				wordList.add( newWord );
			}
			int iteration = wordList.sizeOf(); 
			for ( int listlen = 0; listlen < iteration; listlen++ )
			{
				Word frontWord( wordList.getFront() );
				wordMixer( frontWord );
				for ( int g = 0; g < 10; g++ )
				{
					long int beginTime = time( 0 );	
					cout << "enter your guess: ";
					cin >> guessTemp;
					Word guess( guessTemp );
					if ( guess == skip ) 
						break;
					if ( guessTemp == frontWord )
					{
						long int elapsedTime = time( 0 ) - beginTime;
						cout << "Correct!  Solving this word took " 
							 << elapsedTime << " seconds." << endl;
						break;
					}
					else cout << "Try again!" << endl;
				}
				cout << "The correct permutation is: " << frontWord << endl;
				wordList.remove();
				cout << "Get ready for the next word!" << endl;
			}
		break;
		}
	case 'm':
		{
			ifstream infile( "Math.txt", ios::nocreate|ios::in );
			while ( infile >> tempWordHolder )
			{
				Word newWord( tempWordHolder );
				wordList.add( newWord );
			}
			int iteration = wordList.sizeOf(); 
			for ( int listlen = 0; listlen < iteration; listlen++ )
			{
				Word frontWord( wordList.getFront() );
				wordMixer( frontWord );
				for ( int g = 0; g < 10; g++ )
				{
					long int beginTime = time( 0 );	
					cout << "enter your guess: ";
					cin >> guessTemp;
					Word guess( guessTemp );
					if ( guess == skip ) 
						break;
					if ( guessTemp == frontWord )
					{
						long int elapsedTime = time( 0 ) - beginTime;
						cout << "Correct!  Solving this word took " 
							 << elapsedTime << " seconds." << endl;
						break;
					}
					else cout << "Try again!" << endl;
				}
				cout << "The correct permutation is: " << frontWord << endl;
				wordList.remove();
				cout << "Get ready for the next word!" << endl;
			}
		break;
		}
	case 'p':
		{
			ifstream infile( "Physics.txt", ios::nocreate|ios::in );
			while ( infile >> tempWordHolder )
			{
				Word newWord( tempWordHolder );
				wordList.add( newWord );
			}
			int iteration = wordList.sizeOf(); 
			for ( int listlen = 0; listlen < iteration; listlen++ )
			{
				Word frontWord( wordList.getFront() );
				wordMixer( frontWord );
				for ( int g = 0; g < 10; g++ )
				{
					long int beginTime = time( 0 );	
					cout << "enter your guess: ";
					cin >> guessTemp;
					Word guess( guessTemp );
					if ( guess == skip ) 
						break;
					if ( guessTemp == frontWord )
					{
						long int elapsedTime = time( 0 ) - beginTime;
						cout << "Correct!  Solving this word took " 
							 << elapsedTime << " seconds." << endl;
						break;
					}
					else cout << "Try again!" << endl;
				}
				cout << "The correct permutation is: " << frontWord << endl;
				wordList.remove();
				cout << "Get ready for the next word!" << endl;
			}
		break;
		}

	case 'q': 
		exit(1);
	default:
		cout << "Invalid choice." << endl;
	}

	return 0;
}