/*
 * Name: Owen Sullivan
 * Date Submitted: 10/10/22
 * Lab Section: 006
 * Assignment Name: Lab 5: Spell Checker Using a Hash Table
*/
 
#include "stringset.h"
#include <iostream>
#include <fstream>

void testStringset(Stringset &words);
void loadStringset(Stringset &words, string filename);
vector<string> spellcheck(const Stringset &words, string word);

// int main() {
    
//     Stringset wordlist;
    
//     testStringset(wordlist);
    
//     return 0;

// }

void testStringset(Stringset &words) {
    
    string choice;
    string word;
    
    do {
        
        cout << "I: insert word" << endl;
        cout << "F: find word" << endl;
        cout << "R: remove word" << endl;
        cout << "P: print words in stringset" << endl;
        cout << "Q: quit" << endl;
        cin >> choice;
        
        switch (choice[0]) {
            
            case 'I':
            case 'i':
                cout << "Enter word to insert: ";
                cin >> word;
                words.insert(word);
                break;
            
            case 'F':
            case 'f':
                cout << "Enter word to find: ";
                cin >> word;
                if (words.find(word)) {
                    
                    cout << word << " in stringset" << endl;
                
                } else {
                    
                    cout << word << " not in stringset" << endl;
                
                }
                break;
            
            case 'R':
            case 'r':
                cout << "Enter word to remove: ";
                cin >> word;
                words.remove(word);
                break;
            
            case 'P':
            case 'p':
                vector<list<string>> t = words.getTable();
                int numWords = words.getNumElems();
                int tSize = words.getSize();
                for (int i=0; i<tSize; ++i) {
                    
                    list<string>::iterator pos;
                    for (pos = t[i].begin(); pos != t[i].end(); ++pos) {
                        
                        cout << *pos << endl;
                    
                    }
                
                }
                cout << "Words: " << numWords << endl;
        
        }
    } while (choice[0] != 'Q' && choice[0] != 'q');

}

void loadStringset(Stringset &words, string filename) {
    
    // Open file using file name passed as function argument
    ifstream inFile(filename);

    // Temporary variable to hold contents of line being read
    string word;

    // Loop until getline returns false, which means the end of
    // the file has been reached
    while (getline(inFile, word)) {

        // Call the Stringset insert function to insert
        // the word that was just read
        words.insert(word);

    }

}

vector<string> spellcheck(const Stringset &words, string word) {
    
    // Create a vector to hold values that are one letter
    // away from the input string
    vector<string> oneLetterAway;

    // Loop through each of the linked lists in the hash table
    for (list list : words.getTable()) {

        // Loop through each string in the current linked list
        for (string value : list) {

            int numLettersAway = 0;
            
            // If the current string is the same length as the input string
            if (value.length() == word.length()) {

                // Loop through the characters in the string
                for (int pos = 0; pos < value.length(); pos++) {

                    // If the current character is not equal to the
                    // character at the same index in the input string
                    if (value.at(pos) != word.at(pos)) {

                        // Increment numLettersAway
                        numLettersAway++;

                    }

                }

            }

            // If there is exactly one letter off
            if (numLettersAway == 1) {

                // Add value to vector
                oneLetterAway.push_back(value);

            }

        }

    }

    return oneLetterAway;

}