#include <iostream>
#include <memory>
#include <string>

#include <SceneLoader.hpp>
#include <Window.h>

int main(int argc, char* argv[]) {
    if (argc <= 1) {
        std::cout << "Please specify a scene file" << std::endl;
        return -1;
    }
    if (argc > 2) {
        std::cout << "Please provide only one scene file" << std::endl;
        return -1;
    }
    std::string path = argv[1];
    auto pos = path.find_last_of('.');
    auto pathType = path.substr(pos + 1, path.size());
    if (pathType != "scene") {
        std::cout << "Please specify a scene file \"*.scene\"" << std::endl;
        return -1;
    }

    std::unique_ptr<SceneLoader> scene_loader = std::make_unique<SceneLoader>();
    try {
        scene_loader->loadSceneFile(path);
    } catch (std::runtime_error& e) {
        std::cerr << e.what() << std::endl;
        scene_loader.reset();
        return -1;
    }

    std::unique_ptr<Window> window;
    try {
        window = std::make_unique<Window>(
                std::move(scene_loader->getFirstRenderer()), std::move(scene_loader->getVolume()),
                std::move(scene_loader->getImportanceSDF()), std::move(scene_loader->getCamera()),
                std::move(scene_loader->getTransferFunction()), scene_loader->getLight(), scene_loader->getOptParams(),
                scene_loader->getWidth(), scene_loader->getHeight(), scene_loader->getVolumeProperties());
    } catch (std::runtime_error& e) {
        std::cerr << e.what() << std::endl;
        scene_loader.reset();
        return -1;
    }
    scene_loader.reset();

    // rendering call
    window->render();

    window.reset();
    return 0;
}
