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.NPC.NPC;
import rpgEngine.Data.Abstract.NPC.Script;
import rpgEngine.Data.Abstract.NPC.ScriptItem;


public class NPCPool {
	private static NPCPool instance;
	private Dictionary<Integer,NPC> items;
	
	private NPCPool() {
		items = new Hashtable<Integer,NPC>();
		loadData();
	}
	
	
	public static NPCPool getInstance() {
		if(instance == null) {
			instance = new NPCPool();
		}
		
		return instance;
	}
	
	
	public NPC getPoolItem(Integer i) {
		NPC item = null;
		
		try 
		{
			item = items.get(i);	
		}
		catch(Exception e) 
		{
			return null;	
		}
		
		return item;
	}
	
	
	private void loadData() {
		String filename = "npc.cfg";
		String[] columns = null;
		InputStream fileStream = null;
		BufferedReader reader = null;
		String line = "";
		Integer intVal = null;
		String strVal = "";
		NPC npc = null;
		Script script = null;
		ScriptItem scriptItem = null;
		Integer id = 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();
				
				if(line.startsWith("ID:")) {
					id = Integer.parseInt(line.substring(3));
					npc = new NPC();

					script = new Script();
					npc.setScript(script);
					
					items.put(id,npc);
				}
				else if(line.startsWith("STARTING_POSITION:")) {
					intVal = Integer.parseInt(line.substring(18));
					npc.setCharacterPosition(intVal);
				}
				else if(line.startsWith("ONLY_SHOW_IF:")) {
					columns = line.split(":");
					
					npc.setHasOnlyShowCriteria(true);
					npc.setOnlyShowVariable(Integer.valueOf(columns[1]));
					npc.setOnlyShowVariableValue(Boolean.valueOf(columns[2]));
				}
				else if(line.startsWith("CHARSET:")) {
					intVal = Integer.parseInt(line.substring(8));
					npc.setCharacterSet(CharacterSetPool.getInstance().getPoolItem(intVal));
				}
				else if(line.startsWith("TEXT:")) {
					strVal = line.substring(5);
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_TEXT,strVal,"");
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("STOPIF:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_STOP_IF,columns[1],columns[2]);
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("GIVEITEM:")) {
					strVal = line.substring(9);
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_GIVE_ITEM,strVal,"");
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("SETVAR:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_SET_VARIABLE,columns[1],columns[2]);
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("SHOW_SHOP:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_SHOW_SHOP,columns[1],"");
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("GIVEGOLD:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_GIVE_GOLD,columns[1],"");
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("BATTLE:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_BATTLE,columns[1],"");
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("GIVEEXP:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_GIVE_EXPERIENCE,columns[1],"");
					script.getScriptItems().add(scriptItem);
				}
				else if(line.startsWith("ADDCHARWITHEXP:")) {
					columns = line.split(":");
					
					scriptItem = new ScriptItem(ScriptItem.TYPE_GIVE_CHARACTER_WITH_EXP,columns[1],columns[2]);
					script.getScriptItems().add(scriptItem);
				}
			}
		}
		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;
		}
	}
	
	
	
}
