#include <iostream>
#include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>

// mysql must come after cgi stuff for some reason
#include <mysql++.h>

using namespace mysqlpp;
using namespace cgicc;
using namespace std;


// Helper function for form text boxes and lists
string getFormString(Cgicc& cgi, char *element) {
  const_form_iterator name = cgi.getElement(element);
  if(name != (*cgi).end() && ! name->isEmpty())
    return (string)(**name);
  else
    return (string)"";
}

int main(int argc, char* argv[])
{


  try {
	  Cgicc cgi;
    Connection connect("kjv","192.168.0.5",
    "root","84097");

    string name = getFormString(cgi, "name");
	int strsize = name.length();
	// This is needed to prevent someone from DoS attacking the server by just loading the entire Bible all at once.
	if (name == "")  {
		cout << HTTPHTMLHeader();

		cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
		cout << html() << endl;
		cout << head() << title("You Did Not Enter A Query String!!!") << "<meta http-equiv=\"Refresh\" content=\"2; URL=/cgi-bin/index.pl\">" << head() << endl;

	cout << body() << "<center>" << endl;
	cout << h1("You Need To Enter A Search Query First") << endl;
	cout << "</center>" << body() << endl;
	}
	// This is required to prevent people from searching just for at, and, not only, thus preventing a DoS attack as well.
	else if (strsize <= 4) {
		cout << HTTPHTMLHeader();

		cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
		cout << html() << endl;
		cout << head() << title("You Need More Data To Search For!!!") << "<meta http-equiv=\"Refresh\" content=\"2; URL=/cgi-bin/index.pl\">" << head() << endl;

		cout << body() << "<center>" << endl;
		cout << h1("Minimum Query Size of 5 Required, Sorry") << endl;
		cout << "</center>" << body() << endl;
	}
	else {
	string querystr =
      "select book, chapter, verse, passage"
      " from bible where book LIKE \'%"
      + name
			+ "%\' or chapter LIKE \'%"
			+ name
			+"%\' or verse LIKE \'%"
			+ name
			+"%\' or passage LIKE \'%"
			+name
			+"%\'";

//    cout << querystr << "<br>" << endl;

    Query query = connect.query();
    query << querystr;


	cout << HTTPHTMLHeader();

	cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
	cout << html() << endl;
	cout << head() << title("Results") << "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\">" << head() << endl;

	cout << body() << endl;
	cout << h3("Search Results") << endl;
	cout << "<form method=\"GET\" action=\"/cgi-bin/kjv\">";
	cout << "<span>Enter Search: </span><input type=\"text\" name=\"name\" value=\"" << name << "\">";
	cout << "<span><input type=\"submit\"></span>";
	cout << "</form>";
    Result res = query.store();
    Row row;
    Result::iterator iter;
	int count;
	count = 0;
	cout << table().set("width=\"800\" background=\"/images/bg.jpg\" border=\"1\" align=\"center\"") << endl;
    for (iter = res.begin(); iter != res.end(); iter++) {
      row = *iter;
      cout << tr() << endl;
      cout << td((const char *)row[0]) << endl;
      cout << td((const char *)row[1]) << endl;
      cout << td((const char *)row[2]) << endl;
	  cout << td((const char *)row[3]) << endl;
      cout << tr() << endl;
	  count++;
    }
    cout << table() << endl;
	cout << table().set("width=\"800\" background=\"/images/bg.jpg\" border=\"1\" align=\"center\"") << endl;
	cout << tr() << endl;
	cout << "<td><h6>Total Results: "<< count << "</h6></td>" << endl;
	cout << tr() << endl;
	cout << table() << endl;
	}
  }
  catch (BadQuery excep) {
    // Apache will send cerrs to the error log.
    // I also like to write the error to the browser.
    cerr << "MySQL Generated an error: " <<
    excep.error << endl;
    cout << "MySQL Generated an error: " <<
    excep.error << "<p>";
    return -1;
  }

  cout << body() << html() << endl;
  return 0;
}

