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

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

int main() {
    std::cout << "🎮 ClientUI Simulated Graphics Test" << std::endl;
    std::cout << "====================================" << std::endl;
    std::cout << "Running console-based 'graphical' interface simulation..." << std::endl;

    ClientUIModuleSimulatedGL clientUI;

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

    std::cout << "\n✅ Simulated graphics initialized!" << std::endl;
    std::cout << "🎯 Watch the console for 'rendered' UI frames..." << std::endl;
    std::cout << "⏰ Simulation will run for ~10 seconds (600 frames)\n" << std::endl;

    // Main rendering loop
    int external_frame_count = 0;
    while (clientUI.isRunning() && external_frame_count < 700) {
        external_frame_count++;

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

        // Simulate server responses at specific times
        if (external_frame_count == 200) {
            json server_response = {
                {"type", "server_response"},
                {"source", "economy"},
                {"data", {
                    {"steel_plate", 5.8},
                    {"iron_ore", 1.3},
                    {"copper_wire", 2.2},
                    {"coal", 0.9}
                }}
            };
            clientUI.process(server_response);
            std::cout << "📨 [TEST] Injected economy server response" << std::endl;
        }

        if (external_frame_count == 350) {
            json server_response = {
                {"type", "server_response"},
                {"source", "macroentity"},
                {"data", {
                    {"SteelCorp", {{"cash", 2700000}, {"strategy", "aggressive"}}},
                    {"IronWorks", {{"cash", 1950000}, {"strategy", "conservative"}}},
                    {"NewCorp", {{"cash", 500000}, {"strategy", "startup"}}}
                }}
            };
            clientUI.process(server_response);
            std::cout << "📨 [TEST] Injected macroentity server response" << std::endl;
        }

        // Simulate UI commands
        if (external_frame_count == 150) {
            json ui_command = {
                {"type", "ui_command"},
                {"action", "toggle_economy_window"}
            };
            clientUI.process(ui_command);
            std::cout << "🖱️ [TEST] Simulated user toggling economy window" << std::endl;
        }

        // Simulate realistic frame timing (16ms = ~60fps)
        std::this_thread::sleep_for(std::chrono::milliseconds(16));
    }

    std::cout << "\n📊 Final State Check:" << std::endl;
    auto final_state = clientUI.getState();
    std::cout << "State: " << final_state.dump(2) << std::endl;

    clientUI.shutdown();

    std::cout << "\n🎉 Simulated graphics test completed!" << std::endl;
    std::cout << "Total external frames: " << external_frame_count << std::endl;
    std::cout << "✨ This demonstrates what the real OpenGL version would do!" << std::endl;

    return 0;
}