package Icarus.Graphics.Animation;

import System.Collections.*;
import System.Drawing.Bitmap;
import System.Type;
import System.Reflection.ConstructorInfo;

/**
 * The frameset manager singleton class will handle the framesets in memory. It will load each frameset only once, so that we don't waste memory, and it will sometimes unload the framesets that haven't been used for a while.
 * 
 * @author $Author: asskoala $
 * @version $Revision: 1.1 $ $Date: 2005/12/11 21:07:21 $
 */
public class FramesetManager
{
	/**
	 * Singleton Implementation.
	 */
	private static final FramesetManager mg_Instance = new FramesetManager();

	/**
	 * Maps strings to frameset references.
	 */
	private IDictionary m_Framesets; //TODO: Maybe use soft references here, to provide caching.

	/**
	 * The default frameset provider to use.
	 */
	private IFramesetProvider m_DefaultFramesetProvider;

	/**
	 * Singleton Implementation.
	 */
	private FramesetManager() 
	{
		m_Framesets = new Hashtable();
		m_DefaultFramesetProvider = new PazeraFramesetProvider();
	}

	/**
	 * Singleton Implementation.
	 */
	public static final FramesetManager getInstance() { return mg_Instance; }

	/**
	 * Sets up the new default frameset provider.
	 */
	public void setDefaultFramesetProvider(IFramesetProvider provider) { m_DefaultFramesetProvider = provider; }

	/**
	 * Returns the frameset with the specified ID. If the frameset cannot be loaded, will probably throw an exception.
	 */
	public Frameset getFrameset(String id) 
	{
		if (!m_Framesets.Contains(id)) 
		{
			this.loadFramesetFromId(id, null);
		}

		return (Frameset) m_Framesets.get_Item(id);
	}

	/**
	 * Adds the frameset with the specified id. If a frameset with that id already exists, it is overwritten.
	 */
	public void addFrameset(String id, Frameset frameset) 
	{
		m_Framesets.Add(id, frameset);
	}

	/**
	 * Uses the id passed in as the filename to look for the image in the base image folder.
	 */
	public void loadFramesetFromId(String id, IFramesetProvider provider) 
	{
		this.loadFramesetFromFile(Icarus.Resources.ResourceManager.get_Instance().get_ImagePath() + id, id, provider);
	}

	/**
	 * Loads the frameset with the specified id from the full path passed in using the specified frameset provider.
	 */
	public void loadFramesetFromFile(String filename, String id, IFramesetProvider provider) 
	{
		if (provider == null) 
		{
			provider = m_DefaultFramesetProvider;
		}

		Frameset new_set = new Frameset();
		provider.fillFrameset(new_set, (Bitmap) Bitmap.FromFile(filename));

		this.addFrameset(id, new_set);
	}

	/**
	 * Given an id of an image, loads it.
	 */
	public Bitmap loadImageFromId(String id)
	{
		return (Bitmap) Bitmap.FromFile(Icarus.Resources.ResourceManager.get_Instance().get_ImagePath() + id);
	}
}
