#include <iostream>
#include <cassert>
#include <nlohmann/json.hpp>
#include "../src/ClientUIModule.cpp"

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

void testUIInitialization() {
    std::cout << "🧪 Testing UI initialization..." << std::endl;

    ClientUIModule ui;
    ui.initialize({}, nullptr, nullptr);

    auto state = ui.getState();
    assert(state["initialized"] == true);
    assert(state["frame_count"] == 0);

    std::cout << "✅ UI initialization test passed" << std::endl;
}

void testUIEvents() {
    std::cout << "🧪 Testing UI events..." << std::endl;

    ClientUIModule ui;
    ui.initialize({}, nullptr, nullptr);

    // Test button click event
    json button_event = {
        {"type", "ui_event"},
        {"action", "button_click"},
        {"button", "get_economy_prices"}
    };

    ui.process(button_event);

    // State should have changed (frame count increased)
    auto state = ui.getState();
    assert(state["frame_count"] == 1);

    std::cout << "✅ UI events test passed" << std::endl;
}

void testServerResponses() {
    std::cout << "🧪 Testing server responses..." << std::endl;

    ClientUIModule ui;
    ui.initialize({}, nullptr, nullptr);

    // Test economy response
    json economy_response = {
        {"type", "server_response"},
        {"source", "economy"},
        {"data", {{"steel_plate", 5.2}}}
    };

    ui.process(economy_response);

    // Test macroentity response
    json macroentity_response = {
        {"type", "server_response"},
        {"source", "macroentity"},
        {"data", {{"steel_corp", {{"cash", 2500000}}}}}
    };

    ui.process(macroentity_response);

    std::cout << "✅ Server responses test passed" << std::endl;
}

void testStateManagement() {
    std::cout << "🧪 Testing state management..." << std::endl;

    ClientUIModule ui;
    ui.initialize({}, nullptr, nullptr);

    // Process some events to change state
    ui.process({});
    ui.process({});

    // Get state
    auto state1 = ui.getState();
    assert(state1["frame_count"] == 2);

    // Reset and restore state
    ClientUIModule ui2;
    ui2.initialize({}, nullptr, nullptr);
    ui2.setState(state1);

    auto state2 = ui2.getState();
    assert(state2["frame_count"] == 2);

    std::cout << "✅ State management test passed" << std::endl;
}

void testWindowManagement() {
    std::cout << "🧪 Testing window management..." << std::endl;

    ClientUIModule ui;
    ui.initialize({}, nullptr, nullptr);

    // Test window state changes
    json show_window_event = {
        {"type", "ui_event"},
        {"action", "button_click"},
        {"button", "show_economy_window"}
    };

    ui.process(show_window_event);

    auto state = ui.getState();
    assert(state.contains("windows_state"));

    std::cout << "✅ Window management test passed" << std::endl;
}

int main() {
    std::cout << "🎮 Running ClientUI Module Tests" << std::endl;
    std::cout << "=================================" << std::endl;

    try {
        testUIInitialization();
        testUIEvents();
        testServerResponses();
        testStateManagement();
        testWindowManagement();

        std::cout << "\n🎉 All ClientUI tests passed!" << std::endl;
        return 0;
    } catch (const std::exception& e) {
        std::cerr << "❌ Test failed: " << e.what() << std::endl;
        return 1;
    }
}