// Lab 3: Madlibs
// DUE: 2026-04-07 (April 7th) 11:59pm
// rename to "<username>_lab3.cpp" (e.g., mine would be "jwdittrich_lab3.cpp")
// and submit to:
// james.dittrich+YSU@gmail.com

#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;

int main(){
	/*
	Example madlibs story:

	Hey diddle diddle,
	The cat and the fiddle,
	The cow jumped over the moon;
	The little dog laughed
	To see such sport,
	And the dish ran away with the spoon.

	Don't use this one... Write your own!
	For the purpose of better understanding:
	1. Enforce correct capitalization of user input (everything should be lowercase unless warranted).
		 The cctype library's islower(), isupper(), tolower(), and toupper() are helpful (see examples below).
	   Replace the first word in a sentence to test this.
	2. Prompt for at least one Proper Noun such as a place name, and ensure that subsequent words after
		 spaces (word boundaries) are capitalized (see examples below). 
	*/
	
	// building the story with replacement tokens through concatenation:
	string story = "Hey _VERB1_ _VERB1_,\nThe _NOUN1_ and the _NOUN2_,\n";
	story += "The cow _VERB2_ over the _NOUN3_;\nThe _ADJ1_ _NOUN4_ _VERB3_\n";
	story += "To see such a sport,\nAnd the _NOUN5_ _VERB4_ away with the _NOUN6_.\n";

	// cout << story << endl;

	const int wordListSize = 11;
	string words[11];
	string hints[11] = {
		"silly nonsense word (verb)", // [0]
		"animal (noun)", // [1]
		"thing (noun)", // [2]
		"(verb - past tense)", // [3]
		"place (noun)", // [4]
		"(adjective)" , // [5]
		"animal (noun)", // [6]
		"(verb - past tense)", // [7]
		"thing (noun)", // [8]
		"(verb - past tense)", // [9]
		"thing (noun)"  // [10] 
	};
	string targets[11] = {
		"_VERB1_", // [0]
		"_NOUN1_", // [1]
		"_NOUN2_", // [2]
		"_VERB2_", // [3]
		"_NOUN3_", // [4]
		"_ADJ1_" , // [5]
		"_NOUN4_", // [6]
		"_VERB3_", // [7]
		"_NOUN5_", // [8]
		"_VERB4_", // [9]
		"_NOUN6_"  // [10] 
	};
	bool done = false;
	int counter = 0;
	
	// MADLIBS PSEUDOCODE

	// TODO: replay loop (the game state)
	do{
		// TODO: declare story string
		// TODO: word input loop
		do{
			// TODO: prompt for user input
			counter++;
		}while(counter < wordListSize);
		// TODO: fix capitalization for beginning of a sentence or proper nouns
		// TODO: replace placeholders
		// TODO: print output
		// TODO: prompt to replay(y/n) - consider using toupper() or tolower() for the response
		// https://cplusplus.com/reference/cctype/tolower/
		// https://cplusplus.com/reference/cctype/toupper/
		// https://cplusplus.com/reference/cctype/isupper/
		// https://cplusplus.com/reference/cctype/islower/
			done = true;
	}while(not done);

	// REPLACEMENT EXAMPLE:
	cout << "\n### REPLACEMENT EXAMPLE ###\n";
	words[0] = "dribble";
	story.replace(story.find(targets[0]),targets[0].length(),words[0]);
	story.replace(story.find(targets[0]),targets[0].length(),words[0]);
	cout << story << endl;


	// CAPITALIZATION EXAMPLE
	cout << "\n### CAPITALIZATION EXAMPLE ###\n";
	cout << "\n### CRUDE VERSION ###\n";
	string test = "the empire state building";
	cout << test << endl;
	test[0] = toupper(test[0]); // fixed indexes are allowed
	cout << test << endl;
	size_t found = test.find(' '); // find word boundaries and capitalize the next letter.
	if (found != string::npos){ // can the substring be found?
		cout << "Space found at: " << found << '\n';
		test[found+1] = toupper(test[found+1]);
	}
	cout << test << endl;
	found = test.find(' ',found+1); // 2nd parameter: pick up where you left off.
	if (found != string::npos){
		cout << "second space found at: " << found << '\n';
		test[found+1] = toupper(test[found+1]);
	}
	cout << test << endl; // <- this is not complete, and you are repeating yourself!
	
	// Don't repeat yourself!
	// Strategy: just loop if another space is found.
	// This can be turned into a function.
	cout << "\n### BETTER VERSION ###\n";
	test = "the empire state building";
	cout << test << endl;
	test[0] = toupper(test[0]); // the first character gets capitalized.
	cout << test << endl;
	bool noMoreSpaces = false;
	found = -1;
	do{
		found = test.find(' ', found+1);
		if (found != string::npos){
			cout << "Space found at: " << found << '\n';
			cout << "Next character: " << test[found+1] << endl;
			test[found+1] = toupper(test[found+1]);
			cout << test << endl;
		}else{
			noMoreSpaces = true;
		}
	}while(not noMoreSpaces);
	cout << "Final string: " << test << endl;
	// you can turn the above into a function, such as:
	// string capitalizeProper(string phrase)

	
	// REFERENCE EXAMPLES:
	// See
	// https://cplusplus.com/reference/string/string/
	// And specifically
	// https://cplusplus.com/reference/string/string/getline/
	// https://cplusplus.com/reference/string/string/length/
	// https://cplusplus.com/reference/string/string/find/
	// https://cplusplus.com/reference/string/string/replace/
	
	// string str ("There are two needles in this haystack with needles.");
	// string str2 ("needle");

	// // different member versions of find in the same order as above:
	// size_t found = str.find(str2);
	// if (found!=string::npos)
	//   cout << "first 'needle' found at: " << found << '\n';

	// found=str.find("needles are small",found+1,6);
	// if (found!=string::npos)
	//   cout << "second 'needle' found at: " << found << '\n';

	// found=str.find("haystack");
	// if (found!=string::npos)
	//   cout << "'haystack' also found at: " << found << '\n';

	// found=str.find('.');
	// if (found!=string::npos)
	//   cout << "Period found at: " << found << '\n';

	// // let's replace the first needle:
	// str.replace(str.find(str2),str2.length(),"preposition");
	// cout << str << '\n';
	// string base="this is a test string.";
	// str2="n example";
	// string str3="sample phrase";
	// string str4="useful.";

	// Behind the scenes string.replace() example without find() (ugly!):
	// str = base;                     // "this is a test string."
	// cout << str << endl;
	// str.replace(9,5,str2);          // "this is an example string." (1)
	// cout << str << endl;
	// str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
	// cout << str << endl;
	// str.replace(8,10,"just a");     // "this is just a phrase."     (3)
	// cout << str << endl;
	// str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
	// cout << str << endl;
	// str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)
	// cout << str << endl;
	return 0;
}