#ifndef LOADINGTHREAD_H
#define LOADINGTHREAD_H

#include <string>
#include <deque>
#include <thread>

#include "resourcepack.h"

class LoadingThread
{
    static LoadingThread* singleton;
    static std::thread* thread;

    std::deque<ResourceInterface*> m_resourceQueue;
    std::unordered_map<std::string, ResourcePack*> m_packs;
    std::unordered_map<std::string, int> m_references;
    ResourceInterface* m_currentTask;
    bool m_running;
    float m_taskProgress;
    int m_nbTaskProcessed;

    LoadingThread();
    void processTasks();

public:
    static LoadingThread *init();
    static void destroy();
    static void run();

    static LoadingThread* get() { return singleton; }

    float getTotalProgress() { return float(m_nbTaskProcessed)/float(m_resourceQueue.size() + m_nbTaskProcessed); }
    void resetTotalProgress() { m_nbTaskProcessed = 0; }
    float getCurrentTaskProgress() { return m_taskProgress; }
    const std::string& getCurrentTaskName();

    void loadResourcePack(const std::string& name);

    // please don't call this if the queue isn't empty
    void freeResourcePack(const std::string& name);
};

#endif // LOADINGTHREAD_H
