/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package macampcorp.macamp.game;

import android.app.Activity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;

import macampcorp.macamp.utils.files.FileManager;
import macampcorp.macamp.utils.files.JSONAssetsManager;

/**
 * Created by ptitcois on 18/08/16.
 * Cette classe gère les differentes ressources
 */
public class ResourceManager implements Serializable{

    protected HashMap<String, Resource> mResources = new HashMap<String, Resource>();
    protected Resource mLatestResource = null;
    protected static ResourceManager sThis = null;

    private ResourceManager()
    {
    }



    public static ResourceManager newInstance()
    {
        return sThis=new ResourceManager();
    }

    public void setInstance(){ sThis=this;}

    public static ResourceManager getInstance()
    {
        if(sThis==null) return newInstance();
        return sThis;
    }

    public void loadResourcesFile(String file, Activity act, Game g)
    {
        JSONObject root = JSONAssetsManager.load(act, file);
        try {
            for(int i = 0; i<root.names().length(); i++){
                    g.registerResource(root.names().getString(i), act);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void addResource(String name, Activity act)
    {
        Resource r = new Resource(name, act);
        mResources.put(name, r);
    }

    public boolean contains(String name){return mResources.containsKey(name);}

    public Resource get(String name)
    {
        return mResources.get(name);
    }

    public int count()
    {
        return mResources.keySet().size();
    }

    public String[] getResourceList()
    {
        Object[] x = mResources.keySet().toArray();
        String[] s = new String[mResources.size()];
        for(int i=0; i<mResources.size(); i++)
            s[i]=(String) x[i];

        return s;
    }


}
