#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>

// Function to check if a file exists
bool FileExists(const std::string& filename) {
    std::ifstream file(filename);
    return file.good();
}

// Function to download and install Java 8
void InstallJava() {
    std::cout << "Java 8 or higher is not installed. Downloading Java 8..." << std::endl;
    std::string command = "curl -L -o java_installer.exe https://javadl.oracle.com/webapps/download/AutoDL?BundleId=251654_7ed26d28139143f38c58992680c214a5 && start java_installer.exe";
    std::system(command.c_str());
    std::cout << "Please install Java and restart the program." << std::endl;
    exit(1);
}

// Function to get the latest release URL from GitHub
std::string GetLatestReleaseUrl() {
    std::string command = "curl -s https://api.github.com/repos/MinicraftPlus/minicraft-plus-online/releases/latest";
    std::string response;
    char buffer[128];
    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe) {
        std::cerr << "Failed to run curl command." << std::endl;
        return "";
    }
    while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
        response += buffer;
    }
    pclose(pipe);
    
    std::istringstream stream(response);
    std::string line;
    while (std::getline(stream, line)) {
        if (line.find("browser_download_url") != std::string::npos && line.find(".jar") != std::string::npos) {
            size_t start = line.find("https://");
            size_t end = line.find_last_of('"');
            if (start != std::string::npos && end != std::string::npos) {
                return line.substr(start, end - start);
            }
        }
    }
    return "";
}

// Function to download a file using curl command
bool DownloadFile(const std::string& url, const std::string& filename) {
    std::string command = "curl -L -o " + filename + " " + url;
    int result = std::system(command.c_str());
    return result == 0;
}

int main() {
    const std::string jarFilename = "minicraft.jar";

    if (FileExists(jarFilename)) {
        std::cout << jarFilename << " found." << std::endl;
    } else {
        std::cout << jarFilename << " not found. Downloading the latest version..." << std::endl;
        std::string latestReleaseUrl = GetLatestReleaseUrl();

        if (latestReleaseUrl.empty()) {
            std::cerr << "Failed to get the latest release URL." << std::endl;
            return 1;
        }

        if (DownloadFile(latestReleaseUrl, jarFilename)) {
            std::cout << "Download complete." << std::endl;
        } else {
            std::cerr << "Failed to download the file." << std::endl;
            return 1;
        }
    }

    // Run minicraft.jar
    std::cout << "Running minicraft.jar..." << std::endl;
    int result = std::system(("java -jar " + jarFilename).c_str());

    if (result != 0) {
        std::cerr << "Java is not installed or failed to run minicraft.jar. Installing Java..." << std::endl;
        InstallJava();
        return 1;
    }

    return 0;
}
