#include <core/director.h>

namespace extra2d {

Director& Director::inst() {
    static Director instance;
    return instance;
}

bool Director::init() {
    if (inited_) return true;

    inited_ = true;
    return true;
}

void Director::shutdown() {
    if (!inited_) return;

    SVC_MGR.shutdownAll();
    SCHED.unscheduleAll();

    inited_ = false;
}

void Director::mainLoop(float dt) {
    if (paused_) return;

    dt_ = dt;
    totalTime_ += dt;
    frameCount_++;

    SVC_MGR.updateAll(dt);

    fixedAccumulator_ += dt;
    while (fixedAccumulator_ >= fixedDt_) {
        SVC_MGR.fixedUpdateAll(fixedDt_);
        fixedAccumulator_ -= fixedDt_;
    }

    SCHED.update(dt);

    SVC_MGR.lateUpdateAll(dt);
}

void Director::mainLoopParallel(float dt) {
    if (paused_) return;

    dt_ = dt;
    totalTime_ += dt;
    frameCount_++;

    SVC_MGR.updateAll(dt);

    fixedAccumulator_ += dt;
    while (fixedAccumulator_ >= fixedDt_) {
        SVC_MGR.fixedUpdateAll(fixedDt_);
        fixedAccumulator_ -= fixedDt_;
    }

    SCHED.updateParallel(dt);

    SVC_MGR.lateUpdateAll(dt);
}

void Director::pause() {
    paused_ = true;
}

void Director::resume() {
    paused_ = false;
}

void Director::setTimeScale(float scale) {
    SCHED.setTimeScale(scale);
}

} // namespace extra2d
