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

#ifndef COLORRACE_GAMELAYER_H
#define COLORRACE_GAMELAYER_H
#include <utility>

#include "core/audio/AudioDevice.h"
#include "core/audio/SoundBuffer.h"
#include "core/inputsOutputs/controller/CameraController.h"
#include "ecs/EntityRegistry.h"
#include "ecs/systems/PickingSystem.h"
#include "layer/Layer.h"
#include "ludo/BoardLayout.h"
#include "ludo/LudoAiController.h"
#include "ludo/PiecePresenter.h"
#include "renderer/MeshRenderer.h"
#include "renderer/RenderQueue.h"
#include "utils/openglWrapper/GLRenderState.h"
#include "ludo/LudoGameMode.h"
#include "ludo/ecs/highlight/HighlightSystem.h"

class GameLayer: public engine::Layer {
public:
    GameLayer(std::shared_ptr<ludo::LudoGameMode> gameMode, engine::EntityRegistry& entityRegistry, std::shared_ptr<engine::Camera> cam, std::shared_ptr<engine::PointLight> light,
        engine::Keyboard& keyboard, engine::Mouse& mouse, std::shared_ptr<engine::Model> boardModel, std::unordered_map<std::string, std::shared_ptr<engine::Model>> pieceModdels)
        : Layer(keyboard, mouse), m_gameMode(std::move(gameMode)),  m_entityRegistry(entityRegistry), m_camera(std::move(cam)), m_renderer(std::make_unique<engine::MeshRenderer>()),
        m_pointLight(std::move(light)), m_boardLayout(std::move(boardModel)), m_piecePresenter(m_entityRegistry, std::move(pieceModdels), m_boardLayout),
        m_pickingSystem(*m_camera), m_highlightSystem(*m_camera) {
        for (auto playerID : m_gameMode->getPlayerOrder()) {
            if (playerID != m_localPlayer) {
                aiControllers.emplace_back(m_gameMode, playerID);
            }
        }
    }

    void onUpdate(float deltaTime) override;
    void onRender() override;
protected:
    void onAttachImpl() override;
    void onDetachImpl() override;
private:
    engine::GLRenderState m_renderState;
    engine::RenderQueue m_renderQueue;
    engine::EntityRegistry& m_entityRegistry;
    std::shared_ptr<engine::PointLight> m_pointLight;
    std::shared_ptr<engine::Camera> m_camera;
    std::unique_ptr<engine::MeshRenderer> m_renderer;
    std::unique_ptr<engine::CameraController> m_cameraController;

    ludo::BoardLayout m_boardLayout;
    ludo::PiecePresenter m_piecePresenter;
    engine::PlayerID m_localPlayer = 0;
    std::shared_ptr<ludo::LudoGameMode> m_gameMode;
    engine::PickingSystem m_pickingSystem;
    ludo::HighlightSystem m_highlightSystem;
    std::string m_lastEventMessage;

    std::vector<ludo::LudoAiController> aiControllers;


    std::unique_ptr<engine::audio::SoundBuffer> m_soundBuffer;


    void renderHud();

};

#endif //COLORRACE_GAMELAYER_H
