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 HeroLearningPool {
	private static HeroLearningPool instance;
	private Dictionary<Integer,Dictionary<Integer,Integer>> items;
	
	private HeroLearningPool() {
		items = new Hashtable<Integer,Dictionary<Integer,Integer>>();
		loadData();
	}
	
	
	public static HeroLearningPool getInstance() {
		if(instance == null) {
			instance = new HeroLearningPool();
		}
		
		return instance;
	}
	
	
	public Dictionary<Integer,Integer> getPoolItem(Integer i) {
		Dictionary<Integer,Integer> item = null;
		
		try 
		{
			item = items.get(i);	
		}
		catch(Exception e) 
		{
			return null;	
		}
		
		return item;
	}
	
	
	private void loadData() {	
		String filename = "herosSkillLearning.cfg";
		String[] columns = null;
		InputStream fileStream = null;
		BufferedReader reader = null;
		String line = "";
		Dictionary<Integer,Integer> item = null;
		
		Integer heroID = -1;
		Integer level = -1;
		Integer skillID = -1;
		
		
		//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("HeroID") == false) {
					
					heroID = Integer.parseInt(columns[0]);
					level = Integer.parseInt(columns[1]);
					skillID = Integer.parseInt(columns[2]);
					
					if(items.get(heroID) == null) {
						item = new Hashtable<Integer,Integer>();
						
						items.put(heroID,item);
					}
					
					item = items.get(heroID);
					
					item.put(level, skillID);
				}
			}
		}
		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;
		}
	}
}
