package rpgEngine.Resources;

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

import rpgEngine.Data.Abstract.Item;
import rpgEngine.Data.Abstract.Enemy.Enemy;
import rpgEngine.Data.Raw.Graphics.EnemySet;

public class EnemyPool {
	private static EnemyPool instance;
	private Dictionary<Integer,Enemy> items;
	
	private EnemyPool() {
		items = new Hashtable<Integer,Enemy>();
		loadData();
	}
	
	
	public static EnemyPool getInstance() {
		if(instance == null) {
			instance = new EnemyPool();
		}
		
		return instance;
	}
	
	
	public Enemy getPoolItem(Integer i) {
		Enemy item = null;
		
		try 
		{
			item = items.get(i);	
		}
		catch(Exception e) 
		{
			return null;	
		}
		
		return item;
	}
	
	
	private void loadData() {	
		String filename = "enemy.cfg";
		String[] columns = null;
		InputStream fileStream = null;
		BufferedReader reader = null;
		String line = "";
		
		Enemy enemy = null;
		Integer ID = -1;
		Integer HP = 0;
		Integer MP = 0;
		Integer Atk = 0;
		Integer Def = 0;
		Integer Mag = 0;
		Integer Bar = 0;
		Integer enemySetID = 0;
		EnemySet enemySet = null;
		Integer experience = 0;
		Integer gold = 0;
		Integer itemID = null;
		Item item = null;
		Double itemChance = 0.0;
		Double attackChance = 0.0;
		Double magicChance = 0.0;
		
		
		//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) {
					ID = Integer.valueOf(columns[0]);
					
					HP = Integer.valueOf(columns[1]);
					MP = Integer.valueOf(columns[2]);
					Atk = Integer.valueOf(columns[3]);
					Def = Integer.valueOf(columns[4]);
					Mag = Integer.valueOf(columns[5]);
					Bar = Integer.valueOf(columns[6]);
					
					enemySetID = Integer.valueOf(columns[7]);
					enemySet = EnemySetPool.getInstance().getPoolItem(enemySetID);

					experience = Integer.valueOf(columns[8]);
					gold = Integer.valueOf(columns[9]);
					
					itemID = Integer.valueOf(columns[10]);
					if(itemID == -1) {
						item = null;
					}
					else {
						item = ItemPool.getInstance().getPoolItem(itemID);
					}
					
					itemChance = Double.valueOf(columns[11]);
					attackChance = Double.valueOf(columns[12]);
					magicChance = Double.valueOf(columns[13]);
					
					enemy = new Enemy (ID,HP,MP,Atk,Def,Mag,Bar,enemySet,experience,gold,item,itemChance,attackChance,magicChance);
					items.put(ID, enemy);
				}
			}
		}
		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;
		}
	}
}
