#include "animation.h"
#include "filesystem.h"
#include "model.h"

SkeletonInstance::SkeletonInstance() :
    m_model(nullptr),
    m_animation(INVALID_ANIMATION_HANDLE),
    m_time(0.0f),
    m_looped(false)
{
}

SkeletonInstance::~SkeletonInstance()
{
}

bool SkeletonInstance::IsValid()
{
    return m_model != nullptr;
}

void SkeletonInstance::LoadAnimation(const char* filename)
{
    if (!m_model)
        return;

    //m_model->LoadAnimation(filename);
}

AnimationId_t SkeletonInstance::FindAnimation(const char* name)
{
    if (!m_model)
        return INVALID_ANIMATION_HANDLE;

    return m_model->FindAnimation(name);
}

uint32_t SkeletonInstance::GetNumAnimations()
{
    if (!m_model)
        return 0;

    return m_model->GetNumAnimations();
}

float SkeletonInstance::GetAnimationTime(AnimationId_t id)
{
    if (!m_model)
        return 0.0f;

    const Animation* animation = m_model->GetAnimation(id);

    float frameTime = 1.0f / animation->framerate;
    float totalDuration = animation->numFrames * frameTime;
    return totalDuration;
}

void SkeletonInstance::PlayAnimation(AnimationId_t id, bool looped)
{
    m_time = 0.0f;
    m_animation = id;
    m_looped = looped;
}

void SkeletonInstance::StopAnimation()
{
    m_time = 0.0f;
    m_animation = INVALID_ANIMATION_HANDLE;
    m_looped = -1;
}

AnimationId_t SkeletonInstance::GetCurrentAnimation()
{
    return m_animation;
}

float SkeletonInstance::GetCurrentTime()
{
    return m_time;
}
