#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <curl/curl.h>

namespace AuthlyGrej {
    class api {
    public:
        std::string name;
        std::string ownerid;
        std::string version;
        std::string sessionid;
        bool initialized = false;

        api(std::string name, std::string ownerid, std::string version) {
            this->name = name;
            this->ownerid = ownerid;
            this->version = version;
        }

        void init() {
            std::string post_data = "type=init&ver=" + version + "&name=" + name + "&ownerid=" + ownerid;
            std::string response = req(post_data);

            if (response == "KeyAuth_Invalid") {
                std::cout << "Application not found" << std::endl;
                exit(1);
            }

            // Parse JSON simplified response
            if (response.find("\"success\":true") != std::string::npos) {
                size_t sess_pos = response.find("\"sessionid\":\"");
                if (sess_pos != std::string::npos) {
                    size_t start = sess_pos + 13;
                    size_t end = response.find("\"", start);
                    sessionid = response.substr(start, end - start);
                    initialized = true;
                }
            }
        }

        bool login(std::string username, std::string password) {
            if (!initialized) {
                std::cout << "Please run init() first" << std::endl;
                return false;
            }

            std::string post_data = "type=login&username=" + username + "&pass=" + password + "&hwid=cpp_hwid&sessionid=" + sessionid + "&name=" + name + "&ownerid=" + ownerid;
            std::string response = req(post_data);

            return (response.find("\"success\":true") != std::string::npos);
        }

        bool reg(std::string username, std::string password, std::string key) {
            if (!initialized) {
                std::cout << "Please run init() first" << std::endl;
                return false;
            }

            std::string post_data = "type=register&username=" + username + "&pass=" + password + "&key=" + key + "&hwid=cpp_hwid&sessionid=" + sessionid + "&name=" + name + "&ownerid=" + ownerid;
            std::string response = req(post_data);

            return (response.find("\"success\":true") != std::string::npos);
        }

    private:
        static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
            ((std::string*)userp)->append((char*)contents, size * nmemb);
            return size * nmemb;
        }

        std::string req(std::string post_data) {
            CURL* curl = curl_easy_init();
            std::string readBuffer;

            if (curl) {
                curl_easy_setopt(curl, CURLOPT_URL, "https://grejlabs.shop/api/1.3/");
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str());
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
                
                CURLcode res = curl_easy_perform(curl);
                curl_easy_cleanup(curl);
            }

            return readBuffer;
        }
    };
}

#ifndef SDK_LIBRARY_ONLY
int main() {
    std::cout << "=========================================" << std::endl;
    std::cout << "       Authly Grej C++ Client Example      " << std::endl;
    std::cout << "=========================================" << std::endl;

    // Replace with your credentials from the dashboard
    AuthlyGrej::api AuthApp(
        "YOUR_APP_NAME",
        "YOUR_OWNER_ID",
        "1.0"
    );

    std::cout << "[*] Initializing connection to Authly Grej server..." << std::endl;
    AuthApp.init();
    std::cout << "[+] Initialization completed successfully!\n" << std::endl;

    std::cout << "1. Login" << std::endl;
    std::cout << "2. Register" << std::endl;
    std::cout << "Select an option: ";
    int choice;
    std::cin >> choice;

    if (choice == 1) {
        std::string username, password;
        std::cout << "Enter username: ";
        std::cin >> username;
        std::cout << "Enter password: ";
        std::cin >> password;

        std::cout << "[*] Logging in as " << username << "..." << std::endl;
        if (AuthApp.login(username, password)) {
            std::cout << "[+] Login successful!" << std::endl;
        } else {
            std::cout << "[-] Login failed!" << std::endl;
        }
    } else if (choice == 2) {
        std::string username, password, key;
        std::cout << "Enter username: ";
        std::cin >> username;
        std::cout << "Enter password: ";
        std::cin >> password;
        std::cout << "Enter license key: ";
        std::cin >> key;

        std::cout << "[*] Registering user " << username << "..." << std::endl;
        if (AuthApp.reg(username, password, key)) {
            std::cout << "[+] Registration successful! You can now log in." << std::endl;
        } else {
            std::cout << "[-] Registration failed!" << std::endl;
        }
    } else {
        std::cout << "[-] Invalid option selected." << std::endl;
    }

    return 0;
}
#endif
