#ifndef MANAGEPAGE_H
#define MANAGEPAGE_H

#pragma once

#include "accountManager.h"
#include "parameterManager.h" // for isNumber

#ifdef _WIN32
#include "./../include/curl/curl.h"
#include <windows.h>

//----------------------?????????????????
#define noSLLCheck

#else
#include "curl/curl.h"
#endif

#include <sstream>
#include <unistd.h> // sleep
#include <algorithm>
#include <ctime> // std::time, localtime ...
#include <mutex>



#define USER_AGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"


struct Reply {
    Reply() {}
    Reply(std::string value_both) : html(value_both), url(value_both) {}
    Reply(std::string html, std::string url) : html(html), url(url) {}

    std::string html, url;
};

struct checkNameRply {
    std::string correct_name, url_name;
    PAGE pageInUse;
    enum STATUS { SUCCESS, FAILED, NOTHING_FOUND, MULTIPLE_OPTIONS, NO_WORKING_PAGE }
          status;


    checkNameRply(const std::string &correct_name, const std::string &url_name, const PAGE &p, const checkNameRply::STATUS &s )
        : correct_name(correct_name), url_name(url_name), pageInUse(p), status(s) {}
};


class PageManager
{
public:
    PageManager(std::string sock5ProxyOnlyAddress = "127.0.0.1:9150");
    ~PageManager();

    void setProxy(std::string ip, int port);
    void setDebugMode(bool status);

    void get_terminal_size(int& width);

    Reply getServerRequest(std::string protocol, std::string Url, bool useCookies = false, std::string data = "", bool generateCookieFile = false, bool UrlAfterRedirectOnlyNeeded = false, std::string cookieFilePath = "", int rek_num = 0);
    int downLoadToFile(std::string filePath, std::string url, int rek_num = 0);

    int login(PAGE page, Account account, std::string cookieFilePath);
    Reply getUrlAfterRedirect(std::string p, std::string Url, std::string cookieFilePath);
    checkNameRply checkName(std::vector<PAGE> &pages, std::string Name, bool useFirstPage, std::vector<std::string> &outPutFilePaths /*for replacement*/, std::string &checkFilePath  );
    std::string getLinks(std::string HTML);
    std::string chooseHosterLink(std::string HosterList, std::string Hoster_with_Highst_Priority_at_First, std::string languages_with_highst_priority_at_first, bool withWarnMsg);
    std::string getLinkAfterHosterBasedOperation(std::string url);

    int counterContains(std::string text, std::string substring_with_prozent_i_for_number, int starte_mit_dieser_Zahl = 1);
    std::string grep(std::string text, std::string substring, bool IgnoreCaseSensetifity = false);
    static std::string upper_string(const std::string& str);
    size_t getDate();
    std::string getExePath();

    int compareVersions(std::string Version1, std::string Version2);

    int writeToFile(std::vector<std::string> paths, std::string text);

    static std::string getCurlVersion();


    static std::mutex torAnmeldeNamenMutex;
    static std::string torAnmeldeNamen;
    static bool cloudflare_protec;

    static std::string generateRandomString(int length) {
        static const char alphanum[] =
            "0123456789"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklmnopqrstuvwxyz";

        std::string randomString;
        for (int i = 0; i < length; ++i) {
            randomString.push_back(alphanum[std::rand() % (sizeof(alphanum) - 1)]);
        }

        return randomString;
    }

    static void generateNewTorAnmeldeNamen(std::string alterName) {
        torAnmeldeNamenMutex.lock();

        if(torAnmeldeNamen == alterName) {
            torAnmeldeNamen = generateRandomString(10);
            std::cout << " -> New Proxy Login Name: " << torAnmeldeNamen << std::flush;
            usleep(700000);
        }
        torAnmeldeNamenMutex.unlock();
    }

    std::string sock5Proxy;
private:
    bool debugMode = false;
};



#endif // MANAGEPAGE_H
