/// G. Hagopian -- making a three letter square

#include "std_lib_facilities.h"

/// Create file containing list of three-letter words.
void makeFile(istream& is, ostream& os) {
    string word;
    char ch{};
    while(is>>word) {
        while(is.get(ch))
        {
            if (ch == '\n')  // If the file has been opened in
            {    break;        // text mode then it will correctly decode the
            }                  // platform specific EOL marker into '\n'
        }
        os << word << " ";
    }
}

class letterPair{
public:
    char letter;
    double frequency;
    letterPair(char ch, double f) : letter(ch), frequency(f) {}
    friend ostream& operator<<(ostream& os, letterPair& lp) {
        os << lp.letter << ',' << lp.frequency;
    }
};

/// Create vector of letterPairs
void frequ(istream& is, vector<letterPair>& lf)
{   string word;
    char letter;
    double freq;
    while(!is.eof()) {
        is >> word;
        is >> word;
        is >> letter;
        is >> freq;
        lf.push_back(letterPair(letter, freq));
    }
}

void showWordSqu(vector<char> ws) {
    for(int i = 0; i < ws.size(); ++i) {
        cout << ws.at(i);
        if((i+1)%3==0) cout << endl;
    }
    cout << endl;
}

void showWordSquare(ostream& os, vector<string> vs) {
    for(int i = 0; i < vs.size(); ++i) {
        os << vs.at(i) << endl;
    }
    os << endl;
}

bool good2(ifstream& is, vector<string> vs) {
    is.close();
    vector<string> vs2(3);
    string word;
    for(int i = 0 ; i < vs.size(); ++i) {
        //cout << "\ni = " << i;
        for(int j = 0; j < vs.size(); ++j) {
            //cout << "\nvs[" <<i <<"][" << j << "]=" <<vs[i][j];
            vs2[i] += vs[j][i];
            //cout << "\nvs2[" << i << "]=" << vs2[i];
        }
    }
    //cout << "\nhaha\n" << vs2[0] << endl << vs2[1] << endl << vs2[2] <<endl;
    //showWordSquare(vs2);
    is.open("threeLetterWords.txt");
    for(int i = 0; i < vs.size(); ++i) {
        while(is>>word) {
            if(word==vs2[i]) break;
        }
        if(is.eof()) return false;
        else {
            is.clear();
            is.seekg(0, is.beg);
        }
    }
    return true;
}


bool good(ifstream& is, vector<char> vc) {
    vector<string> vs(6);
    string word;
    for(int i = 0; i<vc.size(); ++i) {
        vs[i/3] += vc[i];
        vs[3+i/3] += vc[(i/3)+3*(i%3)];
    }
    for(int i = 0; i< vs.size(); i++)
        cout << vs[i]  <<endl;
    for(int i = 0; i < vs.size(); ++i) {
        is.open("threeLetterWords.txt");
        while(is>>word) {
            if(word==vs[i]) break;
            if(is.eof()) return false;
            else is.close();
        }
    }
    return true;
}

double score(vector<letterPair> vlp, vector<string> wordSquare) {
    int counter{};
    double score{};
    for(int i = 0; i < wordSquare.size(); ++i) {
        for(int j = 0; j < wordSquare[i].size(); ++j) {
            ///look up frequency
            counter = 0;
            while(vlp[counter].letter != wordSquare[i][j])
                ++counter;
            score += 1/vlp[counter].frequency;
        }
    }
    return score;
}

void initializeFiles(vector<letterPair>& vlp) {
    ifstream ifs("threeLetterWordsDef.txt");
    ofstream ofs("threeLetterWords.txt");
    makeFile(ifs,ofs);
    ifs.close();
    ofs.close();
    /**ifs.open("threeLetterWords.txt");
    while(ifs >> word)
        cout << word << " ";
    ifs.close();*/
    //vector<letterPair> vlp;
    ifs.open("letterFrequ.txt");
    frequ(ifs, vlp);
    ifs.close();
    /**for(int i = 0; i < vlp.size(); ++i)
        cout << vlp.at(i) << endl;*/
}

int main() {
    ofstream squares("squares.txt");
    double points{0};
    string word;
    vector<letterPair> vlp;
    initializeFiles(vlp); //Create threeLetterWords.txt from threeLetterWordsDef.txt
    cout << endl;

    /// object for containing word square
    ///vector<char> wordSqu(9, 'A');
    ///showWordSqu(wordSqu);
    ///vector<char> vc{'A','B','C','D','E','F','G','H','I'};
    ifstream ifs("threeLetterWords.txt");
    //read file of 3-letter words into a vector
    vector<string> words, wordSquare(3);
    ///copy file into vector of words
    while(ifs>>word)
        words.push_back(word);
    //ifs.close();
    //ifs.open("threeLetterWords.txt");  ///mebbe just seekg(0)
    ifs.seekg(0,ifs.beg);
    for(int i = 0; i < words.size()-2;++i) {
        //wordSquare.clear();
        wordSquare[0] = words[i];
        for(int j = 0; j < words.size()-1;++j) {
            wordSquare[1]= words[j];
            for(int k = 0; k < words.size();++k) {
                wordSquare[2] = words[k];
                //showWordSquare(wordSquare);
                //cout << "\nha\n";
                //cin.get();
                if(good2(ifs, wordSquare)) {  //score(vlp,wordSquare)>points &&
                    points = score(vlp,wordSquare);
                    //cout << "\nGood one!\n";
                    showWordSquare(squares, wordSquare);
                    squares << "\nhas a score " << points << endl;
                    ///cin.get();
                }
            }
        }
    }
}
