#include <fstream>
#include <Redacted3D/types/track.h>
#include <Redacted3D/engine/engine.h>
#include <Redacted3D/types/entity/moby.h>
#include <jlog/jlog.hpp>

Track::Track(const std::string &file, Moby* moby) {
    std::ifstream f(file);
    if (!f.is_open()) {
        FATAL("Couldn't load track file: " + file)
        engine->quit();
    }

    std::string line;
    bool success = true;
    while (std::getline(f, line)) {
        std::istringstream stream(line);
        std::string prefix;
        stream >> prefix;

        if (prefix == "Teleport: ")
            stream >> teleportToStart;

        TrackKeyFrame frame;
        if (prefix == "Position: ")
            success = !success,
            stream >> frame.position.x,
            stream >> frame.position.y,
            stream >> frame.position.z;

        if (prefix == "Angle: ")
            success = !success,
            stream >> frame.angle.x,
            stream >> frame.angle.y,
            stream >> frame.angle.z;

        if (success) {track.push_back(frame); continue;}

        WARNING("There is not the same number of Positions and Angles in the track: " + file);
    }
    parent = moby;
}
