package rpgEngine.Resources;

import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Dictionary;
import java.util.Hashtable;


public class FontPool {
	private static FontPool instance;
	private Dictionary<Integer,Font> items;
	
	public static final int FONT_BLORK = 0;
	public static final int FONT_MINECRAFT = 1;
	
	
	private FontPool() {
		items = new Hashtable<Integer,Font>();
		loadData();
	}
	
	
	public static FontPool getInstance() {
		if(instance == null) {
			instance = new FontPool();
		}
		
		return instance;
	}
	
	
	public Font getPoolItem(Integer i) {
		Font item = null;
		
		try 
		{
			item = items.get(i);	
		}
		catch(Exception e) 
		{
			return null;	
		}
		
		return item;
	}
	
	
	private void loadData() {
		
		loadFont(FONT_BLORK, "blork.ttf");
		loadFont(FONT_MINECRAFT, "minecraft.ttf");
		
	}

	private void loadFont(int id, String filename) {
		InputStream fileStream = null;
		Font font = null;
		
		//Open File
		try {
			fileStream = new BufferedInputStream(this.getClass().getResourceAsStream("Font/" + filename));
			font = Font.createFont(Font.TRUETYPE_FONT, fileStream);
		}
		catch(Exception e) {
			System.err.println("Error openning file " + filename);
			System.err.println(e.toString());
			return;
		}
		
		items.put(id, font);
		
		//Close File
		try {
			fileStream.close();
		}
		catch(Exception e) {
			System.err.println("Error closing file " + filename);
			System.err.println(e.toString());
			return;
		}
	}

}
