//To be compiled using VS 2013
#pragma warning(disable: 4996)
#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <curl/curl.h>
#undef max
using namespace std;

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
	size_t written = fwrite(ptr, size, nmemb, stream);
	return written;
}

int checkVersion()
{
	const char* appdataPathc = getenv("appdata");
	string appdataPathz = appdataPathc;
	string appdataPath = appdataPathz + "\\LSharpDownloader\\version.txt";
	string appdataPathRemote = appdataPathz + "\\LSharpDownloader\\versiontmp.txt";
	string clientVersions;
	string remoteVersions;

	fstream versionFile;
	fstream remoteVersionFile;

	ofstream remoteVersionFileC(appdataPathRemote);
	remoteVersionFileC.close();

	int clientVersion;
	int remoteVersion;

	versionFile.open(appdataPath, ios::in | ios::out);
	if (versionFile.good())
	{
		getline(versionFile, clientVersions);
		clientVersion = atoi(clientVersions.c_str());
		versionFile.close();

		CURL *curl;
		FILE *fp;
		CURLcode res;
		char *url = "http://hehehemlsharp.cba.pl/version.txt";
		const char *outfilename = appdataPathRemote.c_str();
		curl = curl_easy_init();
		if (curl) {
			fp = fopen(outfilename, "wb");
			curl_easy_setopt(curl, CURLOPT_URL, url);
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
			res = curl_easy_perform(curl);
			curl_easy_cleanup(curl);
			fclose(fp);
		}

		remoteVersionFile.open(appdataPathRemote, ios::in | ios::out);
		if (remoteVersionFile.good())
		{
			getline(remoteVersionFile, remoteVersions);
			remoteVersion = atoi(remoteVersions.c_str());
			remoteVersionFile.close();
			if (remoteVersion > clientVersion)
			{
				remove(appdataPathRemote.c_str());
				return 1;
			}
			else
			{
				remove(appdataPathRemote.c_str());
				return 0;
			}
		}
		else
		{
			MessageBox(NULL, L"Błąd podczas pobierania wersji z pliku na serwerze: version.txt!", NULL, MB_OK | MB_ICONSTOP);
			abort();
		}
	}
	else
	{
		return 2;
	}
}

void readmeInit()
{
	fstream readmeFileCreate;

	const char* appdataPathc = getenv("appdata");
	string appdataPathz = appdataPathc;
	string appdataPath = appdataPathz + "\\LSharpDownloader\\readme.txt";

	const char* readmeFileValue = "Ten plik zostal wygenerowany podczas uruchomienia L# Downloadera.\nProsze nie modyfikowac plikow znajdujacych sie wewnatrz folderu LSharpDownloader.";
	readmeFileCreate.open(appdataPath, ios::in | ios::out | ios::trunc);
	if (readmeFileCreate.good())
	{
		readmeFileCreate.write(readmeFileValue, 147);
	}
	else 
	{
		MessageBox(NULL, L"Błąd podczas tworzenia pliku readme.txt!", NULL, MB_OK | MB_ICONSTOP);
		abort();
	}
}

int main()
{
	system("if not exist %appdata%\\LSharpDownloader mkdir %appdata%\\LSharpDownloader");
	readmeInit();

	const char* appdataPathc = getenv("appdata");
	string appdataPathz = appdataPathc;
	string appdataPath = appdataPathz + "\\LSharpDownloader\\leaguesharp.rar";
	string appdataPath2 = appdataPathz + "\\LSharpDownloader\\version.txt";

	int version = checkVersion();
	if (version == 1)
	{
		cout << "L# Downloader. Pobieranie najnowszej wersji..." << endl;

		CURL *curl;
		FILE *fp;
		CURLcode res;
		char *url = "http://hehehemlsharp.cba.pl/leaguesharp.rar";
		const char *outfilename = appdataPath.c_str();
		curl = curl_easy_init();
		if (curl) {
			fp = fopen(outfilename, "wb");
			curl_easy_setopt(curl, CURLOPT_URL, url);
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
			res = curl_easy_perform(curl);
			curl_easy_cleanup(curl);
			fclose(fp);
		}

		CURL *curl2;
		FILE *fp2;
		CURLcode res2;
		char *url2 = "http://hehehemlsharp.cba.pl/version.txt";
		const char *outfilename2 = appdataPath2.c_str();
		curl2 = curl_easy_init();
		if (curl2) {
			fp2 = fopen(outfilename2, "wb");
			curl_easy_setopt(curl2, CURLOPT_URL, url2);
			curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, write_data);
			curl_easy_setopt(curl2, CURLOPT_WRITEDATA, fp2);
			res2 = curl_easy_perform(curl2);
			curl_easy_cleanup(curl2);
			fclose(fp2);
		}
		
		cout << "Pobrano najnowsza wersje. Nacisnij dowolny klawisz, by zamknac program.\nL# Downloader, napisany przez mistake010 dla HeheheM'a." << endl;
		system("explorer %appdata%\\LSharpDownloader");
		return 0;
	}
	else if (version == 0)
	{
		cout << "Nie ma obecnie nowszej wersji na serwerze. Nacisnij dowolny klawisz, by zamknac program.\nL# Downloader, napisany przez mistake010 dla HeheheM'a.";
	}
	else if (version == 2)
	{
		ofstream versionFileCreate(appdataPath2);
		versionFileCreate.close();
		system("cls");
		main();
	}
	else
	{
		cout << "Nieznany blad." << endl;
	}
	_getch();
	return 0;
}