package electrosphere.engine.assetmanager;

import electrosphere.audio.AudioBuffer;
import electrosphere.renderer.Model;
import electrosphere.renderer.texture.Texture;
import electrosphere.util.ModelLoader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author amaterasu
 */
public class AssetManager {
    
    ConcurrentHashMap<String,Model> modelsLoadedIntoMemory = new ConcurrentHashMap();
    CopyOnWriteArrayList<String> modelsInQueue = new CopyOnWriteArrayList();
    
    ConcurrentHashMap<String,Texture> texturesLoadedIntoMemory = new ConcurrentHashMap();
    CopyOnWriteArrayList<String> texturesInQueue = new CopyOnWriteArrayList();
    
    ConcurrentHashMap<String,AudioBuffer> audioLoadedIntoMemory = new ConcurrentHashMap();
    CopyOnWriteArrayList<String> audioInQueue = new CopyOnWriteArrayList();
    
    
    
    
    
    
    //
    //General asset manager stuff
    //
    
    public void loadAssetsInQueue(){
        for(String currentPath : modelsInQueue){
            modelsInQueue.remove(currentPath);
            modelsLoadedIntoMemory.put(currentPath, ModelLoader.load_Model_From_File(currentPath));
        }
        for(String currentPath : texturesInQueue){
            texturesInQueue.remove(currentPath);
            texturesLoadedIntoMemory.put(currentPath, new Texture(currentPath));
        }
        for(String currentPath : audioInQueue){
            audioInQueue.remove(currentPath);
            audioLoadedIntoMemory.put(currentPath, new AudioBuffer(currentPath));
        }
    }
    
    
    
    
    
    
    
    
    
    
    //
    //Models
    //
    
    public void addModelPathToQueue(String path){
        if(!modelsInQueue.contains(path) && !modelsLoadedIntoMemory.containsKey(path)){
            modelsInQueue.add(path);
        }
    }
    
    public Model fetchModel(String path){
        Model rVal = null;
        if(modelsLoadedIntoMemory.containsKey(path)){
            rVal = modelsLoadedIntoMemory.get(path);
        }
        return rVal;
    }
    
    /**
    Registers a (presumably generated in code) model with the asset manager
    @returns a random string that represents the model in the asset manager
    */
    public String registerModel(Model m){
        String rVal;
        UUID newUUID = UUID.randomUUID();
        rVal = newUUID.toString();
        modelsLoadedIntoMemory.put(rVal,m);
        return rVal;
    }
    
    public void registerModelToSpecificString(Model m, String s){
        modelsLoadedIntoMemory.put(s,m);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    //
    // Textures
    //
    
    
    public void addTexturePathtoQueue(String path){
        if(!texturesInQueue.contains(path) && !texturesLoadedIntoMemory.containsKey(path)){
            texturesInQueue.add(path);
        }
    }
    
    public Texture fetchTexture(String path){
        Texture rVal = null;
        if(texturesLoadedIntoMemory.containsKey(path)){
            rVal = texturesLoadedIntoMemory.get(path);
        }
        return rVal;
    }
    
    public String registerTexture(Texture t){
        String rVal;
        UUID newUUID = UUID.randomUUID();
        rVal = newUUID.toString();
        texturesLoadedIntoMemory.put(rVal,t);
        return rVal;
    }
    
    
    
    
    
    
    
    
    
    //
    //AUDIO
    //
    
    public void addAudioPathToQueue(String path){
        if(!audioInQueue.contains(path) && !audioLoadedIntoMemory.containsKey(path)){
            audioInQueue.add(path);
        }
    }
    
    public AudioBuffer fetchAudio(String path){
        AudioBuffer rVal = null;
        if(audioLoadedIntoMemory.containsKey(path)){
            rVal = audioLoadedIntoMemory.get(path);
        }
        return rVal;
    }
    
    
    
    
    
    
    
    
    
    
    
}
