#include <iostream>
#include <nlohmann/json.hpp>
#include <chrono>
#include <thread>
#include "ClientUIModuleGraphical.cpp"

using json = nlohmann::json;
using namespace warfactory;

int main() {
    std::cout << "🎮 ClientUI Graphical Module Test" << std::endl;
    std::cout << "==================================" << std::endl;
    std::cout << "Opening OpenGL window with ImGui interface..." << std::endl;

    ClientUIModuleGraphical clientUI;

    // Initialize graphics module
    clientUI.initialize({}, nullptr, nullptr);

    std::cout << "✅ Graphics initialized - Window should be open!" << std::endl;
    std::cout << "🎯 Instructions:" << std::endl;
    std::cout << "   - Click buttons to simulate server requests" << std::endl;
    std::cout << "   - Open/close windows to test UI state" << std::endl;
    std::cout << "   - Close window to exit" << std::endl;
    std::cout << "   - Hot-reload: Edit code → make → module reloads!" << std::endl;

    // Main rendering loop
    int frame_count = 0;
    while (!clientUI.shouldClose()) {
        frame_count++;

        // Process the UI module
        clientUI.process({});

        // Simulate server responses occasionally
        if (frame_count == 180) { // 3 seconds at 60fps
            json server_response = {
                {"type", "server_response"},
                {"source", "economy"},
                {"data", {
                    {"steel_plate", 5.5},
                    {"iron_ore", 1.2},
                    {"copper_wire", 2.1}
                }}
            };
            clientUI.process(server_response);
        }

        if (frame_count == 360) { // 6 seconds
            json server_response = {
                {"type", "server_response"},
                {"source", "macroentity"},
                {"data", {
                    {"SteelCorp", {{"cash", 2600000}, {"strategy", "aggressive"}}},
                    {"IronWorks", {{"cash", 1900000}, {"strategy", "conservative"}}}
                }}
            };
            clientUI.process(server_response);
        }

        // Limit to 60 FPS (16.67ms per frame)
        std::this_thread::sleep_for(std::chrono::milliseconds(16));
    }

    clientUI.shutdown();

    std::cout << "\n🎉 Graphics test completed!" << std::endl;
    std::cout << "Total frames rendered: " << frame_count << std::endl;

    return 0;
}