#include <algorithm>
#include <string.h>
#include <cstdio>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <curl/curl.h>
/*
g++ -v -x c -E -
 -lmysqlcppconn
https://stackoverflow.com/questions/34057798/how-does-one-set-up-the-visual-studio-code-compiler-debugger-to-gcc
https://docs.microsoft.com/en-us/cpp/linux/deploy-run-and-debug-your-linux-project?view=vs-2017
*/

using namespace std;
string data;
string mapnames[10];
string EventContent[5];
string pattern[10];
int SelectMysqlEventCount(sql::Connection *con)
{
	sql::Statement *stmt;
	sql::ResultSet *res;
	int EventCount = 0;
	try {
		stmt = con->createStatement();
		res = stmt->executeQuery("SELECT * FROM `EventCount`");
		if (res->next()) {
			cout << res->getInt(1) << endl;
			EventCount = res->getInt(1);
		}
	}
	catch (sql::SQLException &e) {
		cout << "# ERR: SQLException in " << __FILE__;
		cout << "# ERR: " << e.what();
		cout << " (MySQL error code: " << e.getErrorCode();
		cout << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
	delete stmt;
	delete res;
	//cout << endl;
	return EventCount;
}

size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{
	for (std::size_t c = 0; c < size*nmemb; c++)
	{
		data.push_back(buf[c]);
	}
	return size * nmemb; //tell curl how many bytes we handled
}


string printreturn(std::string::size_type urlstringend, std::string const &s)
{
	string strreturn("");
	if (urlstringend == std::string::npos) {
		strreturn = "not found sdbhabd2342d";
	}
	else 
	{
		strreturn = s.substr(0, urlstringend);
	}
	return strreturn;
}

void removeSubstrs(string& s, string& p) {
	string::size_type n = p.length();
	for (string::size_type i = s.find(p);
		i != string::npos;
		i = s.find(p))
		s.erase(i, n);
}

void ReadForumSubSectionEvent()
{
	int index;
	CURL* curl;
	curl_global_init(CURL_GLOBAL_ALL);
	curl = curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_URL, "https://unloze.com/forums/events.76/");
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
	//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
	curl_easy_perform(curl);
	//check by taking the first from the top of the page, thats always the newest in order
	//the needed context is always close, instead of taking the full size we only take the needed portion distance
	std::string::size_type urlstringend;
	urlstringend = data.find("preview\">Event #");
	std::string sdeli = ("data-previewUrl=\"");
	string strf = printreturn(urlstringend, data);
	size_t pos = 0;
	if ((pos = strf.find(sdeli)) != std::string::npos) {
		strf.erase(0, pos + sdeli.length());
	}
	data.clear();
	strf.insert(0, "https://unloze.com/");
	//https://unloze.com/threads/event-54n74-15-b4ck.1324 is returned here for example
	std::cout << "strf.c_str(): " << strf.c_str() << "\n";
	curl_easy_setopt(curl, CURLOPT_URL, strf.c_str());
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
	curl_easy_perform(curl);
	curl_easy_cleanup(curl);
	curl_global_cleanup();
	//assumes only ze event threads contain "CS:S Zombie Escape"
	//mapnames collects maps specified to event
	urlstringend = data.find("CS:S Zombie Escape");
	strf = printreturn(urlstringend, data);
	if (strf.find("not found sdbhabd2342d") == std::string::npos)
	{
		urlstringend = data.find("Prize :");
		strf = printreturn(urlstringend, data);
		sdeli = ("<a href=\"http://fastdl.unloze.com/css_ze/maps/");
		pos = 0;
		while ((pos = strf.find(sdeli)) != std::string::npos && index < 10)
		{
			strf.erase(0, pos + sdeli.length());
			mapnames[index] = strf.substr(0, strf.find(".bsp.bz2"));
			std::cout << "mapnames[index]: " << mapnames[index] << "\n";
			index++;
		}
		//EventContent 0 = title
		urlstringend = data.find("| UNLOZE</title>");
		strf = printreturn(urlstringend, data);
		sdeli = ("<title>");
		pos = 0;
		if ((pos = strf.find(sdeli)) != std::string::npos)
		{
			strf.erase(0, pos + sdeli.length());
			EventContent[0] = strf;
		}
		//EventContent 1 = rewards
		urlstringend = data.find("<div class=\"messageTextEndMarker\">&nbsp;</div>");
		strf = printreturn(urlstringend, data);
		sdeli = ("Prize :");
		pos = 0;
		index = 0;
		if ((pos = strf.find(sdeli)) != std::string::npos)
		{
			strf.erase(0, pos + sdeli.length());
			size_t index1 = 0;
			pattern[0] = "</span>";
			pattern[1] = "<br/>";
			pattern[2] = "<b>";
			pattern[3] = "</b>";
			string teststr = strf;
			while (!!pattern[index].size())
			{
				removeSubstrs(teststr, pattern[index]);
				index++;
			}
			//std::cout << "teststr: " << "\n" << teststr << "\n";
			strf = teststr;
			EventContent[1] = strf;
		}
		//EventContent 2 = leader
		urlstringend = data.find("Prize :");
		strf = printreturn(urlstringend, data);
		sdeli = ("Leader :</span>");
		pos = 0;
		if ((pos = strf.find(sdeli)) != std::string::npos)
		{
			strf.erase(0, pos + sdeli.length());
			//std::cout << "reached eventcontent2 strf: " << strf << "\n";
			EventContent[2] = strf.substr(0, strf.find("<"));
		}
		//EventContent 3 = date
		urlstringend = data.find("\">Time :");
		strf = printreturn(urlstringend, data);
		sdeli = ("Date :</span>");
		pos = 0;
		if ((pos = strf.find(sdeli)) != std::string::npos)
		{
			strf.erase(0, pos + sdeli.length());
			EventContent[3] = strf.substr(0, strf.find("<"));
		}
		//EventContent[4] = hours
		urlstringend = data.find("Leader :");
		strf = printreturn(urlstringend, data);
		sdeli = ("Time :</span>");
		pos = 0;
		if ((pos = strf.find(sdeli)) != std::string::npos)
		{
			strf.erase(0, pos + sdeli.length());
			strf = strf.substr(0, strf.find("pm GMT+1"));
		}
		sdeli = ("//");
		pos = 0;
		if ((pos = strf.find(sdeli)) != std::string::npos)
		{
			strf.erase(0, pos + sdeli.length());
			EventContent[4] = strf;
		}
	}
	else
	{
		std::cout << "Not found" << "\n" << "data value: " << data << "\n";
	}

}

void MYSQLUpdateEventMaps(sql::Connection *con)
{
	sql::Statement *stmt;
	try { 
		stmt = con->createStatement();
		stmt->executeUpdate("delete from Maps");
		int index;
		while (!!mapnames[index].size())
		{
			stmt->executeUpdate("INSERT INTO `Maps` (`MapNames`) VALUES (\"" + mapnames[index] + "\")");
			index++;
		}
	}
	catch (sql::SQLException &e) {
		cout << "# ERR: SQLException in " << __FILE__;
		cout << "# ERR: " << e.what();
		cout << " (MySQL error code: " << e.getErrorCode();
		cout << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
	delete stmt;
}

void MYSQLUpdateEventContent(sql::Connection *con)
{
	sql::Statement *stmt;
	try {
		stmt = con->createStatement();
		stmt->executeUpdate("UPDATE `EventContent` SET `title`=\"" + EventContent[0] +"\",`Rewards`=\"" + EventContent[1] 
			+ "\",`Leaders`=\"" + EventContent[2] + "\",`datum`=\"" + EventContent[3] + "\",`hours`=\"" + EventContent[4] + "\"");
	}catch (sql::SQLException &e) {
		cout << "# ERR: SQLException in " << __FILE__;
		cout << "# ERR: " << e.what();
		cout << " (MySQL error code: " << e.getErrorCode();
		cout << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
	delete stmt;
}

bool CloseDBConnection(sql::Connection *con)
{
	try
	{
		con->close();
		delete con;
		return true;
	}
	catch (sql::SQLException &e)
	{
		return false;
	}
}

int main()
{
	sql::mysql::MySQL_Driver *driver;
	sql::Connection *con;
	driver = sql::mysql::get_driver_instance();
	con = driver->connect("tcp://151.80.230.149:3306", "eventscss", "sdbhb2h34b2hbhbdfwbfwhber234");
	con->setSchema("eventscss");
	try {
		//Eventcount = SelectMysqlEventCount(con);
		ReadForumSubSectionEvent();
		MYSQLUpdateEventMaps(con);
		MYSQLUpdateEventContent(con);
		CloseDBConnection(con);
	}
	catch (sql::SQLException &e) {
		cout << "# ERR: SQLException in " << __FILE__;
		cout << "# ERR: " << e.what();
		cout << " (MySQL error code: " << e.getErrorCode();
		cout << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
	cout << "EventContent[0]: " << EventContent[0] << endl;
	cout << "EventContent[1]: " << EventContent[1] << endl;
	cout << "EventContent[2]: " << EventContent[2] << endl;
	cout << "EventContent[3]: " << EventContent[3] << endl;
	cout << "EventContent[4]: " << EventContent[4] << endl;
	return 0;
}