//
// Created by sebastian on 29.07.26.
//

#ifndef COLORRACE_COLORRACEAPP_H
#define COLORRACE_COLORRACEAPP_H
#include "gameLayer/GameLayer.h"
#include "GameScene.h"
#include "../../external/NetworkCore/src/client/GameClient.h"
#include "core/Application.h"
#include "ecs/EntityRegistry.h"
#include "ecs/standardComponents/MeshComponent.h"
#include "ecs/standardComponents/TransformComponent.h"
#include "layer/SceneManager.h"
#include "mainMenu/MainMenuScene.h"
#include "multiplayer/LudoGameClient.h"
#include "multiplayer/LudoNetworkEvents.h"
#include "renderer/MeshRenderer.h"
#include "renderer/entities/Camera.h"
#include "renderer/shader/StaticShader.h"
#include "utils/openglWrapper/GLRenderState.h"
#include "utils/openglWrapper/openglObjects/Vao.h"
#include "utils/openglWrapper/shader/ShaderProgram.h"


class ColorRaceApp : public engine::Application {
public:
    static engine::EngineConfig makeConfig() {
        engine::EngineConfig config;

        config.windowWidth = 1280;
        config.windowHeight = 720;
        config.windowTitle = "ColorRace";
        config.clearColor = glm::vec4(0.1f, 0.1f, 0.15f, 1.0f);
        config.vsync = true;
        config.debugContext = true;
        return config;
    }
    ColorRaceApp() : Application(makeConfig()), m_networkEventBus(std::make_shared<engine::EventBus<NetworkEvents>>()) {
        getSceneManager().switchTo(std::make_unique<MainMenuScene>(m_applicationEventBus, m_networkEventBus, getInputContextStack(), getKeyboard(), getMouse()));
        m_networkEventBus->subscribe([this](NetworkEvents& event) {
                std::visit([this](auto&& e) {
                    using T = std::decay_t<decltype(e)>;
                    if constexpr (std::is_same_v<T, ConnectRequest>) {
                        engine::net::ClientConfig client_config;
                        client_config.host = e.host;
                        client_config.port = e.port;
                        m_ludoGameClient = std::make_unique<LudoGameClient>(client_config, m_networkEventBus);
                        m_ludoGameClient->onStart();
                    }
                }, event);
            });
    }
protected:
    void onUpdate(float deltaTime) override;
    void onRender() override;
    std::shared_ptr<engine::EventBus<NetworkEvents>> m_networkEventBus;

private:
    std::unique_ptr<LudoGameClient> m_ludoGameClient;
};


#endif //COLORRACE_COLORRACEAPP_H
