//
// Created by sebastian on 30.07.26.
//

#include "GameScene.h"

#include "AudioLayer.h"
#include "gameLayer/GameLayer.h"
#include "core/inputsOutputs/inputs/Mouse.h"
#include "ecs/standardComponents/MeshComponent.h"
#include "ecs/standardComponents/TransformComponent.h"
#include "layer/DebugLayer.h"
#include "renderer/primitives/SphereFactory.h"

GameScene::GameScene(engine::InputContextStack& inputStack, engine::Keyboard& keyboard, engine::Mouse& mouse) : Scene(inputStack, keyboard, mouse), m_camera(std::make_unique<engine::Camera>()) {
    m_pointLight = std::make_shared<engine::PointLight>(glm::vec3(.0f, 10.f, -5.0f), glm::vec3(1.0f, 1.0f, 1.0f));
    const std::vector<unsigned int> players = {0,1,2,3};
    m_gameMode = std::make_shared<ludo::LudoGameMode>(m_eventBus, players);
}

void GameScene::onEnter() {


    m_camera->setPosition({0.0f, 0.0f, 5.0f});
    m_camera->setYaw(0.0f);
    m_camera->setPitch(0.0f);

    const auto gameboardModel = assetManager->getModel("gameboard");
    const auto gameboardEntity = entityRegistry.createEntity();

    const auto transform = engine::TransformComponent({0,0,0}, {0,0,0}, {1,1,1});
    const auto mesh = engine::MeshComponent(gameboardModel);
    entityRegistry.addComponent<engine::TransformComponent>(gameboardEntity, transform);
    entityRegistry.addComponent<engine::MeshComponent>(gameboardEntity, mesh);

    std::unordered_map<std::string, std::shared_ptr<engine::Model>> pieceModels;
    pieceModels["Red"] = assetManager->getModel("pawn_red");
    pieceModels["Blue"] = assetManager->getModel("pawn_blue");
    pieceModels["Green"] = assetManager->getModel("pawn_green");
    pieceModels["Yellow"] = assetManager->getModel("pawn_yellow");

    std::unordered_map<std::string, std::shared_ptr<engine::audio::SoundBuffer>> pieceSounds;
    pieceSounds["step"] = assetManager->getSound("pawn_step");

    addLayer(std::make_unique<ludo::AudioLayer>(*assetManager, m_keyboard, m_mouse, m_gameMode, entityRegistry, m_eventBus, getAnimationEventBus()));
    addLayer(std::make_unique<engine::DebugLayer>(getAnimationEventBus(), *m_camera, entityRegistry, m_keyboard, m_mouse, engine::SphereFactory::createUVSphere(1.0f)));
    addLayer(std::make_unique<GameLayer>(getAnimationEventBus(), m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, m_eventBus, *assetManager));
}

void GameScene::onExit() {
    Scene::onExit();
}

std::vector<engine::AssetRequest> GameScene::getRequiredAssets() const {
    std::vector<engine::AssetRequest> requests;
    requests.emplace_back(engine::ModelRequest{"pawn_red", "assets/models/chess_red.obj", engine::ModelSplitPolicy::Combined});
    requests.emplace_back(engine::ModelRequest{"pawn_blue", "assets/models/chess_blue.obj", engine::ModelSplitPolicy::Combined});
    requests.emplace_back(engine::ModelRequest{"pawn_green", "assets/models/chess_green.obj", engine::ModelSplitPolicy::Combined});
    requests.emplace_back(engine::ModelRequest{"pawn_yellow", "assets/models/chess_yellow.obj", engine::ModelSplitPolicy::Combined});
    requests.emplace_back(engine::ModelRequest{ "gameboard", "assets/models/GameBoard.obj", engine::ModelSplitPolicy::Combined});
    requests.emplace_back(engine::SoundRequest{ "rolling_dice", "assets/sounds/rolling_dice.wav"});
    requests.emplace_back(engine::SoundRequest{ "pawn_step", "assets/sounds/pawn_step.wav"});
    return requests;
}