#include <SDL3/SDL_messagebox.h>
#include <SDL3/SDL_dialog.h>
#include <SDL3/SDL_version.h>
#include <SDL3/SDL_platform.h>
#include <SDL3_image/SDL_image.h>
#include <SDL3_sound/SDL_sound.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "meow.h"
#include "eventthread.h"
#include "config.h"
#include <stdio.h>
#include <time.h>
#include <fstream>
#include <ruby.h>
#include <zlib.h>
#include <AL/al.h>
#include <boost/version.hpp>
#include <physfs.h>
#include <pixman.h>

#ifdef __LINUX__
	#include <gtk/gtk.h>
	#include "xdg-user-dir-lookup.h"
#endif


SDL_MessageBoxButtonData buttons[] = {
    { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Yes" },
    { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "No" }
};

void crash(const char* reason){
	char msg[1024];
	snprintf(msg, sizeof msg, "Error occured! Error message: %s\n\n Want to create a crash log? You can share the crash log with the developers and help resolve the issue.", reason);
	SDL_MessageBoxData messageboxdata = {
	    .flags = SDL_MESSAGEBOX_ERROR,
	    .window = NULL,
	    .title = "Crashlog",
	    .message = msg,
	    .numbuttons = SDL_arraysize(buttons),
	    .buttons = buttons,
	    .colorScheme = NULL
	};

	
	int buttonid = 0;
	if (SDL_ShowMessageBox(&messageboxdata, &buttonid) == false) {
		printf("[CRASHDUMP] %s\n", reason);
	}

	if(buttonid == 1){
		std::ofstream out;
		const Config conf;
		time_t mtime = time(NULL);
		struct tm *now = localtime(&mtime);
		std::string time = std::to_string(now->tm_hour) + "." + std::to_string(now->tm_min) + "." + std::to_string(now->tm_sec);
		out.open("crash_" + time + ".txt");
		if (out.is_open()){
				out << "[SUNSHINE CRASHDUMP]" << std::endl;
				out << "CONFIG" << std::endl;
				out << "defScreenW: " << conf.defScreenW << std::endl;
				out << "defScreenH: " << conf.defScreenH << std::endl;
				out << "debugMode: " << conf.debugMode << std::endl;
				out << "screenMode: " << conf.screenMode << std::endl;
				out << "fullscreen: " << conf.fullscreen << std::endl;
				out << "fixedAspectRatio: " << conf.fixedAspectRatio << std::endl;
				out << "smoothScaling: " << conf.smoothScaling << std::endl;
				out << "vsync: " << conf.vsync << std::endl;
				out << "EnableSixteenByNine: " << conf.EnableSixteenByNine << std::endl;
				out << "windowTitle: " << conf.windowTitle << std::endl;
				out << "defSfixedFrameratecreenW: " << conf.fixedFramerate << std::endl;
				out << "frameSkip: " << conf.frameSkip << std::endl;
				out << "syncToRefreshrate: " << conf.syncToRefreshrate << std::endl;
				out << "solidFonts: " << conf.solidFonts << std::endl;
				out << "subImageFix: " << conf.subImageFix << std::endl;
				out << "enableBlitting: " << conf.enableBlitting << std::endl;
				out << "maxTextureSize: " << conf.maxTextureSize << std::endl;
				out << "gameFolder: " << conf.gameFolder << std::endl;
				out << "allowSymlinks: " << conf.allowSymlinks << std::endl;
				out << "pathCache: " << conf.pathCache << std::endl;
				out << "iconPath: " << conf.iconPath << std::endl;
				out << "useScriptNames: " << conf.useScriptNames << std::endl;
				out << "customScript: " << conf.customScript << std::endl;
				out << "customDataPath: " << conf.customDataPath << std::endl;
				out << "commonDataPath: " << conf.commonDataPath << std::endl;
				out << "" << std::endl;

				const int sdlcompiled = SDL_VERSION;
				const int sdllinked = SDL_GetVersion();
				out << "SDL(compiled) version: " << SDL_VERSIONNUM_MAJOR(sdlcompiled) << "." << SDL_VERSIONNUM_MINOR(sdlcompiled) << "." << SDL_VERSIONNUM_MICRO(sdlcompiled) << std::endl;
				out << "SDL(linked) version: " << SDL_VERSIONNUM_MAJOR(sdllinked) << "." << SDL_VERSIONNUM_MINOR(sdllinked) << "." << SDL_VERSIONNUM_MICRO(sdllinked) << std::endl;
				out << "SDL_image(compiled) version: " << SDL_IMAGE_MAJOR_VERSION << "." << SDL_IMAGE_MINOR_VERSION << "." << SDL_IMAGE_MICRO_VERSION << std::endl;
				out << "SDL_sound(compiled) version: " << SDL_SOUND_MAJOR_VERSION << "." << SDL_IMAGE_MINOR_VERSION << "." << SDL_IMAGE_MICRO_VERSION << std::endl;
				out << "SDL_TTF(compiled) version: " << SDL_TTF_MAJOR_VERSION << "." << SDL_TTF_MINOR_VERSION << "." << SDL_TTF_MICRO_VERSION << std::endl;

				//out << "Detected OS: " << SDL_GetPlatform() << std::endl;
				
				#ifdef __LINUX__
					out << "GTK(compiled) version: " << GTK_MAJOR_VERSION << "." << GTK_MINOR_VERSION << "." << GTK_MICRO_VERSION << std::endl;
					out << "Desktop enviroment(XDG_CURRENT_DESKTOP): " << getenv("XDG_CURRENT_DESKTOP") << std::endl;
				#endif
				
				out << "ZLib version: " << ZLIB_VERSION << std::endl;
				out << "OpenAL version: " << alGetString(AL_VERSION) << std::endl;
				out << "Boost versino: " << BOOST_VERSION / 100000 << "." << BOOST_VERSION / 100 % 1000 << "." << BOOST_VERSION % 100 << std::endl;
				out << "Pixman version: " << PIXMAN_VERSION_STRING << std::endl;
				
				out.close();
		}else{
			printf("[CRASHLOG] Failed to write crashdump file\n");
		}
	}
}
