//
// Created by sebastian on 06.08.26.
//

#ifndef COLORRACE_LUDOGAMECLIENT_H
#define COLORRACE_LUDOGAMECLIENT_H
#include <iostream>

#include "LudoNetworkEvents.h"
#include "../../../external/NetworkCore/src/client/ApplicationClient.h"
#include "core/events/EventBus.h"
#include "spdlog/spdlog.h"


class LudoGameClient : public engine::net::ApplicationClient{
public:
    explicit LudoGameClient(const engine::net::ClientConfig &config, std::shared_ptr<engine::EventBus<NetworkEvents>> networkEventBus)
        : ApplicationClient(config), m_networkEventBus(std::move(networkEventBus)) {
    }

protected:
    void onConnected() override {
        spdlog::info("LudoGameClient successfully connected to server");
        send({
     {"type", "auth"},
     {"payload", {
         {"token", getConfig().token}
     }}
    });
    }

    void onDisconnected() override {
        std::cout << "Verbindung verloren, versuche Reconnect...\n";
    }

    void onMessage(nlohmann::json message) override {
        if (message.value("type", "") == "auth_ack") {
            NetworkEvents connectResult = ConnectResult(true, "");
            m_networkEventBus->publish(connectResult);
        }
        std::cout << "Empfangen: " << message.dump() << "\n";
    }

private:
    std::shared_ptr<engine::EventBus<NetworkEvents>> m_networkEventBus;
};


#endif //COLORRACE_LUDOGAMECLIENT_H
