#include <iostream>
#include <vector>
#include <regex>
#include <fstream>
#include <curl/curl.h>
#include <ctime>
#include <cstring>

struct MemoryStruct {
    char *memory;
    size_t size;
};

size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;

    char *ptr = (char *)realloc(mem->memory, mem->size + realsize + 1);
    if (ptr == NULL) {
        std::cout << "Not enough memory to write response" << std::endl;
        return 0;
    }

    mem->memory = ptr;
    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;

    return realsize;
}
    std::vector<std::pair<std::string, std::string>> getLastPlayedTitle(const std::string& sender, const std::string& url, const std::string& title) {
    std::vector<std::pair<std::string, std::string>> result;

    std::string postData = "playlistSearch_keyword=" + title + "&submit=suchen";

    struct MemoryStruct chunk;
    chunk.memory = (char *)malloc(1);
    chunk.size = 0;

    CURL *curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.length());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

        CURLcode res = curl_easy_perform(curl);

        if (res != CURLE_OK) {
            std::cout << "Failed to fetch data from URL: " << curl_easy_strerror(res) << std::endl;
            curl_easy_cleanup(curl);
            return result;
        }

        std::string html_data = chunk.memory;

        std::regex pattern("<th scope=\"row\" class=\"entry datetime\">\\s*(.*?)\\s*<br>\\s*(.*?)\\s*</th>");
        std::smatch matches;
        std::vector<std::pair<std::string, std::string>> tempResult;

        auto words_begin = std::sregex_iterator(html_data.begin(), html_data.end(), pattern);
        auto words_end = std::sregex_iterator();

        for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
            std::smatch match = *i;
            std::string dateStr = match[1].str();
            std::string timeStr = match[2].str();

            std::tm tm{};
            strptime(dateStr.c_str(), "%d.%m.%Y", &tm);
            char dateFormatted[100];
            strftime(dateFormatted, sizeof(dateFormatted), "%d.%m.%Y", &tm);

            std::tm tm2{};
            strptime(timeStr.c_str(), "%H.%M Uhr", &tm2);
            char timeFormatted[100];
            strftime(timeFormatted, sizeof(timeFormatted), "%H.%M Uhr", &tm2);

            tempResult.emplace_back(std::string(dateFormatted), std::string(timeFormatted));
        }

        // Reverse the order of tempResult
        std::reverse(tempResult.begin(), tempResult.end());

        result = tempResult;

        free(chunk.memory);
        curl_easy_cleanup(curl);
    }

    return result;
}

int main() {
    std::string sender1live = "1live";
    std::string senderWDR2 = "WDR2";
    std::string senderWDR3 = "WDR3";
    std::string senderWDR4 = "WDR4";
    std::string senderWDR5 = "WDR5";
    std::string title = "Last Christmas";
    std::string url1live = "https://www1.wdr.de/radio/1live/musik/playlist/index.jsp";
    std::string urlWDR2 = "https://www1.wdr.de/radio/wdr2/musik/playlist/index.jsp";
    std::string urlWDR3 = "https://www1.wdr.de/radio/wdr3/titelsuche-wdrdrei-104.jsp";
    std::string urlWDR4 = "https://www1.wdr.de/radio/wdr4/titelsuche-wdrvier-102.jsp";
    std::string urlWDR5 = "https://www1.wdr.de/radio/wdr5/musik/titelsuche-wdrfuenf-104.jsp";


    std::vector<std::pair<std::string, std::string>> result1live = getLastPlayedTitle(sender1live, url1live, title);
    std::vector<std::pair<std::string, std::string>> resultWDR2 = getLastPlayedTitle(senderWDR2, urlWDR2, title);
    std::vector<std::pair<std::string, std::string>> resultWDR3 = getLastPlayedTitle(senderWDR3, urlWDR3, title);
    std::vector<std::pair<std::string, std::string>> resultWDR4 = getLastPlayedTitle(senderWDR4, urlWDR4, title);
    std::vector<std::pair<std::string, std::string>> resultWDR5 = getLastPlayedTitle(senderWDR5, urlWDR5, title);

    std::cout << "\t   Wham's Last Christmas lief zuletzt\n";

    std::cout << "\n\t\t\t auf " << sender1live << "\n";
    for (const auto& pair : result1live) {
        std::cout << "\t\tam " << pair.first << " um " << pair.second << std::endl;
    }

    std::cout << "\n\t\t\t auf " << senderWDR2 << "\n";
    for (const auto& pair : resultWDR2) {
        std::cout << "\t\tam " << pair.first << " um " << pair.second << std::endl;
    }

    std::cout << "\n\t\t\t auf " << senderWDR3 << "\n";
    for (const auto& pair : resultWDR3) {
        std::cout << "\t\tam " << pair.first << " um " << pair.second << std::endl;
    }

    std::cout << "\n\t\t\t auf " << senderWDR4 << "\n";
    for (const auto& pair : resultWDR4) {
        std::cout << "\t\tam " << pair.first << " um " << pair.second << std::endl;
    }

    std::cout << "\n\t\t\t auf " << senderWDR5 << "\n";
    for (const auto& pair : resultWDR5) {
        std::cout << "\t\tam " << pair.first << " um " << pair.second << std::endl;
    }
    std::cout << std::endl;
    
return 0;

}