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

#include "utils/load_file.hpp"
#include "utils/ptr.hpp"
#include "utils/debug.hpp"

#include "constants.hpp"
#include "paddle_script.hpp"
#include "wall_script.hpp"

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


using namespace frog;


void init_game(frog::engine& eng)
{
    using namespace frog;
    using std::pair, std::string;

    // auto meshes = std::vector<pair<string, gl::mesh>>{};
    // meshes.push_back({ "sphere", gl::shape::sphere(10, 20, 1) });
    // meshes.push_back({ "ground", gl::shape::polygon_2D(20, 1) });
    // meshes.push_back({ "paddle", gl::shape::paddle(30, 4, 1, 0.5, 15) });

    using entry = pair<string, gl::mesh>;

    auto meshes = std::array
    {
        entry{ "sphere", gl::shape::sphere(10, 20, Ball::Size) },
        entry{ "ground", gl::shape::polygon_2D(40, 1) },
        entry{ "paddle", gl::shape::paddle(
                            Sides,
                            Paddle::Radius,
                            Paddle::Thickness,
                            Paddle::Height,
                            Paddle::AngularSize) },
        entry{ "wall", gl::shape::paddle(
                            Sides,
                            2,
                            0.5,
                            Wall::Height,
                            Wall::AngularSize) },
    };

    auto textures = std::vector<pair<string, string>>
    {
        { "ground", "game_assets/ground.png" },
    };

    auto scenes = std::array
    {
        "main",
    };

    for (auto& [tag, shape] : meshes)
    {
        eng.meshes.add(std::move(tag), std::move(shape));
    }

    for (auto& [tag, tex] : textures)
    {
        eng.textures.add(std::move(tag), gl::texture{ tex });
    }

    for (auto& name : scenes)
    {
        eng.scenes->add(std::move(name), mk_ptr<scene>());
    }
}


void add_objects(frog::engine& eng)
{
    object_builder builder;

    //
    // Add ground
    //
    builder.add_mesh("ground");
    builder.add_texture("ground");
    builder.set_scale({ GroundSize });
    builder.build_in(eng);

    //
    // Add ball
    //
    builder.reset();
    builder.add_mesh("sphere");
    builder.set_material(gl::material
        {
            geo::vec4{ 0.05, 0.05, 0.05, 1 },
            geo::vec4{ 0.6, 0.6, 0.6, 1 },
            geo::vec4{ 1, 1, 1, 4 },
        });
    builder.build_in(eng);

    //
    // Add paddles
    //
    gl::material mat =
    {
        geo::vec4(0.1, 0.1, 0.2, 1),
        geo::vec4(0.2, 0.4, 0.9, 1),
        geo::vec4(0.95, 0.95, 0.95, 200),
    };
    for (int i = 0; i < Paddle::Count; i++)
    {
        float angle = 360 / Paddle::Count * i + 90 - Paddle::AngularSize / 2;
        builder.reset();
        builder.add_mesh("paddle");
        builder.set_tag("paddle");
        builder.set_material(mat);
        builder.emplace_script<paddle_script>(angle);
        builder.build_in(eng);
    }

    //
    // Add walls
    //
    auto wall_mats = std::array
    {
        gl::material
        {
            geo::vec4{ 0.12, 0.16, 0.08, 1 },
            geo::vec4{ 252, 166, 86, 1 } / 255.f,
            geo::vec4{ 1, 1, 1, 40 },
        },
        gl::material
        {
            geo::vec4{ 0.12, 0.16, 0.08, 1 },
            geo::vec4{ 255, 255, 171, 1 } / 255.f,
            geo::vec4{ 1, 1, 1, 40 },
        },
    };
    for (int i = 0; i < Wall::Levels; i++)
    {
        int mat_idx = 0;
        for (int j = 0; j < Wall::NumberPerLevel; j++)
        {
            builder.reset();
            builder.add_mesh("wall");
            builder.set_tag("wall");
            builder.set_material(wall_mats[(i + mat_idx++) % wall_mats.size()]);
            builder.emplace_script<wall_script>(Wall::AngularSize * j,
                                                Wall::Height * i);
            builder.build_in(eng);
        }
    }
}


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

    try
    {
        auto global = frog::mk_ptr<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);
        add_objects(engine);

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