package rpgEngine.Resources;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Dictionary;
import java.util.Hashtable;

import javax.imageio.ImageIO;


public class ImagePool {
	private static ImagePool instance;
	private Dictionary<Integer,BufferedImage> items;
	
	public static final Integer ID_TITLE_BACKGROUND = 0;
	public static final Integer ID_MENU_MAIN_BACKGROUND = 1;
	public static final Integer ID_MENU_STATUS_BACKGROUND = 2;
	public static final Integer ID_MENU_ITEM_BACKGROUND = 3;
	public static final Integer ID_MENU_SKILL_BACKGROUND = 4;
	public static final Integer ID_DIALOG_BACKGROUND = 5;
	public static final Integer ID_DIALOG_LONG_BACKGROUND = 6;
	public static final Integer ID_MENU_EQUIPMENT_BACKGROUND = 7;
	public static final Integer ID_DIALOG_GAME_EVENT = 8;
	public static final Integer ID_SAVE_LOAD_GAME_BACKGROUND = 9;
	public static final Integer ID_SHOP_BACKGROUND = 10;
	public static final Integer ID_BATTLE_BACKGROUND = 11;
	public static final Integer ID_BATTLE_BACKGROUND_WITH_CHOICE = 12;
	public static final Integer ID_BATTLE_DIALOG = 13;
	
	
	private ImagePool() {
		items = new Hashtable<Integer,BufferedImage>();
		loadData();
	}
	
	
	public static ImagePool getInstance() {
		if(instance == null) {
			instance = new ImagePool();
		}
		
		return instance;
	}
	
	
	public BufferedImage getPoolItem(Integer i) {
		BufferedImage item = null;
		
		try 
		{
			item = items.get(i);	
		}
		catch(Exception e) 
		{
			return null;	
		}
		
		return item;
	}
	
	
	private void loadData() {	
		loadImage("titlebackground.png",ID_TITLE_BACKGROUND);
		loadImage("menubackground.png",ID_MENU_MAIN_BACKGROUND);
		loadImage("statusmenubackground.png",ID_MENU_STATUS_BACKGROUND);
		loadImage("itemsmenubackground.png",ID_MENU_ITEM_BACKGROUND);
		loadImage("skillsmenubackground.png",ID_MENU_SKILL_BACKGROUND);
		loadImage("dialog.png",ID_DIALOG_BACKGROUND);
		loadImage("longdialog.png",ID_DIALOG_LONG_BACKGROUND);
		loadImage("equipmenubackground.png",ID_MENU_EQUIPMENT_BACKGROUND);
		loadImage("eventdialog.png",ID_DIALOG_GAME_EVENT);
		loadImage("savegamebackground.png",ID_SAVE_LOAD_GAME_BACKGROUND);
		loadImage("shopbackground.png",ID_SHOP_BACKGROUND);
		loadImage("battle.png",ID_BATTLE_BACKGROUND);
		loadImage("battle-choice.png",ID_BATTLE_BACKGROUND_WITH_CHOICE);
		loadImage("battle-dialog.png",ID_BATTLE_DIALOG);
	}
	
	
	private void loadImage(String filename, Integer ID) {
		BufferedImage img = null;
		InputStream imageStream = null;
		
		try {
			imageStream = this.getClass().getResourceAsStream("Image/" + filename);
			img = ImageIO.read(imageStream);
		}
		catch(Exception e) {
			img = null;
			imageStream = null;
		}
		
		if(img != null) {
			items.put(ID,img);
		}
	}
	
}
