
#include "core/engine.hpp"
#include "graphics/shape.hpp"

#include "utils/load_file.hpp"

#include <utility>      // move
#include <iostream>     // cout


using namespace frog;


struct move_script : frog::script
{
    void init(game_object& object, engine& engine) override
    {
        object.model().mesh_tag = "test";
        object.model().pos = { 0, 5, 0 };
    }

    void stable_update(game_object& object, engine& engine) override
    {
        object.model().pos.x() += 0.01;
        if (object.model().pos.x() > 5)
            object.destroy();
    }
};



void init_game(frog::engine& eng)
{
    eng.meshes.add("test",
        // gl::shape::cuboid(vec3{ 0.5, 0.25, 1 }));
        gl::shape::sphere(10, 20, 0.85));

    eng.meshes.add("ground", gl::shape::polygon_2D(20, 30.0));

    eng.textures.add("ground", gl::texture{ "engine/assets/font.png" });

    eng.scenes->add("scene1", mk_ptr<frog::scene>());

    {
        auto obj = mk_ptr<frog::game_object>();
        obj->add_script(mk_ptr<move_script>());
        eng.scenes->current().add(std::move(obj));
    }

    {
        auto obj = mk_ptr<frog::game_object>();
        obj->model().mesh_tag = "ground";
        obj->model().texture_tag = "ground";
        obj->model().mat = gl::texture_mat;
        obj->model().pos = { 0, 0, -10 };
        eng.scenes->current().add(std::move(obj));
    }
}

int main(int argc, char** argv)
{
    frog::fs::path path = argc > 0 ? argv[0] : "./unknown";

    try
    {
        auto global = std::make_unique<frog::state>();

        auto set = frog::settings{};
        set.clear_color = { 0.5, 0.6, 0.3 };
        set.window_name = "gaaaaaame";
        set.assign_path(path);

        auto engine = frog::engine{ std::move(set), std::move(global) };

        init_game(engine);

        engine.play();
    }
    catch (std::exception& ex)
    {
        std::cerr << "ERROR: " << ex.what() << std::endl;
    }
    return 0;
}
