package rpgEngine.Resources;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Dictionary;
import java.util.Hashtable;


public class GameConfigPool {
	private static GameConfigPool instance;
	private Dictionary<Integer,Integer> items;
	
	public static Integer ID_STARTING_MAP_ID = 0;
	public static Integer ID_STARTING_MAP_X = 1;
	public static Integer ID_STARTING_MAP_Y = 2;
	public static Integer ID_STARTING_HERO_ID = 3;
	public static Integer ID_STARTING_GOLD = 4;
	public static Integer ID_BATTLE_MUSIC = 5;
	public static Integer ID_BOSS_BATTLE_MUSIC = 6;
	
	
	private GameConfigPool() {
		items = new Hashtable<Integer,Integer>();
		loadData();
	}
	
	
	public static GameConfigPool getInstance() {
		if(instance == null) {
			instance = new GameConfigPool();
		}
		
		return instance;
	}
	
	
	public Integer getPoolItem(Integer i) {
		Integer item = null;
		
		try 
		{
			item = items.get(i);	
		}
		catch(Exception e) 
		{
			return null;	
		}
		
		return item;
	}
	
	
	private void loadData() {	
		String filename = "game.cfg";
		String[] columns = null;
		InputStream fileStream = null;
		BufferedReader reader = null;
		String line = "";
		Integer key = null;
		
		//Open File
		try {
			fileStream = this.getClass().getResourceAsStream("Config/" + filename);
			reader = new BufferedReader(new InputStreamReader(fileStream));
		}
		catch(Exception e) {
			System.err.println("Error openning file" + filename);
			System.err.println(e.toString());
			return;
		}
		
		//Read File
		try {
			while(reader.ready()) {
				line = reader.readLine();
				
				columns = line.split(",");
				
				if(columns[0].equals("ID") == false) {
					key = Integer.parseInt(columns[2]);
					
					items.put(Integer.parseInt(columns[0]),key);
				}
			}
		}
		catch(Exception e) {
			System.err.println("Error reading file" + filename);
			System.err.println(e.toString());
			return;
		}
		
		//Close File
		try {
			reader.close();
			fileStream.close();
		}
		catch(Exception e) {
			System.err.println("Error closing file" + filename);
			System.err.println(e.toString());
			return;
		}
	}
}
