// G. Hagopian woodshedding on the ChemWordsGrid problem

// These "include" code from the C++ library and SFML too
//#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <vector>
using std::vector;
#include <string>;
using std::string;
#include <random>  // for mt_rand() to generate random
using std::mt19937;
#include <iostream>
using std::cout;
#include <fstream>
using std::fstream;

mt19937 mt_rand(time(0));  //seed the random number

// initialize vector of sf::Text objects
void setup(vector<sf::Text>& CS, sf::Font& font) {

	char chems[][3] =
	{ "ac", "ag", "al", "am", "ar", "as", "at", "au", "b",
	  "ba", "be", "bh", "bi", "bk", "br", "c", "ca", "cd",
	  "ce", "cf", "cl", "cm", "cn", "co", "cr", "cs", "cu",
	  "db", "ds", "dy", "er", "es", "eu", "f", "fe", "fl",
	  "fm", "fr", "ga", "gd", "ge", "h", "he", "hf", "hg",
	  "ho", "hs", "i", "in", "ir", "k", "kr", "la", "li",
	  "lr", "lu", "lv", "mc", "md", "mg", "mn", "mo", "mt",
	  "n", "na", "nb", "nd", "ne", "nh", "ni", "no", "np",
	  "o", "og", "os", "p", "pa", "pb", "pd", "pm", "po",
	  "pr", "pt", "pu", "ra", "rb", "re", "rf", "rg", "rh",
	  "rn", "ru", "s", "sb", "sc", "se", "sg", "si", "sm",
	  "sn", "sr", "ta", "tb", "tc", "te", "th", "ti", "tl",
	  "tm", "ts", "u", "v", "w", "xe", "y", "yb", "zn",
	  "zr" };

	//Load and format chem strings into sf::Text objects
	for (unsigned int i = 0; i < 118; ++i) {
		CS[i].setFont(font);
		CS[i].setString(chems[i]);
		cout << chems[i] << " ";
		CS[i].setCharacterSize(100);
		CS[i].setFillColor(sf::Color::Color(3, 13, 2019));

	}
}

//------------------------------------------------------
bool isGood(vector<sf::Text>& words) {
	string word, word1, word2, word3, fword;
	//first row
	//vector<string> vs;
	for (int i = 0; i < 3; ++i) {
		word1 = words[3 * i + 0].getString();
		word2 = words[3 * i + 1].getString();
		word3 = words[3 * i + 2].getString();
		word = word1 + word2 + word3;
		switch (word.size()) {
		case 3: {
			fstream fs("threeLetterWords.txt", std::ios_base::in);
			if (!fs) cout << "failed to open threeLetterWords.txt";
			while (fs >> fword) if (word == fword) break;
			return false;
		}
		case 4: {
			fstream fs("fourLetterWords.txt", std::ios_base::in);
			if (!fs) cout << "failed to open fourLetterWords.txt";
			while (fs >> fword) if (word == fword) break;
			return false;
		}
		case 5: {
			fstream fs("fiveLetterWords.txt", std::ios_base::in);
			if (!fs) cout << "failed to open fiveLetterWords.txt";
			while (fs >> fword) if (word == fword) break;
			return false;
		}
		case 6: {
			fstream fs("sixLetterWords.txt", std::ios_base::in);
			if (!fs) cout << "failed to open sixLetterWords.txt";
			while (fs >> fword) if (word == fword) break;
			return false;
		}
		}
		return true;
	}
}
//------------------------------------------------------
void getNewGrid(vector<sf::Text>& threeby3, vector<sf::Text>& ChemStrings) {
	threeby3.clear();
	for (unsigned int i = 0; i < 9; ++i) {
		threeby3.push_back(ChemStrings[mt_rand() % ChemStrings.size()]);
		threeby3[i].setOrigin(50, 50);
		threeby3[i].setPosition(250 + 100 * (i % 3), 250 + 100 * (i / 3));
	}
}
//------------------------------------------------------
int main() {
	sf::Font font;
	font.loadFromFile("28 Days Later.ttf");
	// Make a window that is 800 by 800 pixels
	// And has the title "Chem Strings"
	sf::RenderWindow window(sf::VideoMode(800, 800), "Chem Strings");

	// Create a vector of Chem Strings
	vector<sf::Text> ChemStrings(118);
	// and load/format them into vector of sf::Text objects
	setup(ChemStrings, font);

	// SLow this puppy down! (1 frame per second)
	window.setFramerateLimit(10);

	// Create a random 3x3 array of chem symbols and check if it's good
	vector<sf::Text> threeby3; //will contain 9 elements
	for (unsigned int i = 0; i < 9; ++i) {
		threeby3.push_back(ChemStrings[mt_rand() % ChemStrings.size()]);

		threeby3[i].setOrigin(50, 50);
		threeby3[i].setPosition(250 + 100 * (i % 3), 250 + 100 * (i / 3));

	}
	cout << "Size of words:" << threeby3.size() << '\n';
	if (isGood(threeby3)) cout << "Good one!\n";
	else cout << "Not good...try again.\n";
	// This "while" loop goes round and round- perhaps forever
	while (window.isOpen())
	{
		// The next 6 lines of code detect if the window is closed
		// And then shuts down the program
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				// Someone closed the window- bye
				window.close();
		}

		// Clear everything from the last run of the while loop
		window.clear();

		// Draw the 3x3 grid
		//window.draw(message);
		for (sf::Text t : threeby3) {
			t.rotate(10);
			window.draw(t);
		}
		if (!isGood(threeby3)) {
			getNewGrid(threeby3, ChemStrings);
		}
		// Show everything we just drew

		window.display();
	} // while

	return 0;
}