package rpgEngine.Logic.Game;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Item;
import rpgEngine.Data.Abstract.Shop;
import rpgEngine.Data.Abstract.Enemy.EnemyParty;
import rpgEngine.Data.Abstract.NPC.ScriptItem;
import rpgEngine.Data.Abstract.NPC.ScriptPlayer;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Resources.EnemyPartyPool;
import rpgEngine.Resources.HeroPool;
import rpgEngine.Resources.ItemPool;
import rpgEngine.Resources.ShopPool;
import rpgEngine.UI.MainFrame;

public class ScriptEventConsumer {
	private Game game;
	private MainFrame mainFrame;
	
	public ScriptEventConsumer(Game g, MainFrame mf) {
		game = g;
		mainFrame = mf;
	}
	
	
	public void consumeScriptItem() {
		Item item = null;
		Integer itemID = -1;
		Integer variable = -1;
		Integer shopID = -1;
		Boolean varValue = false;
		Shop shop = null;
		ScriptItem scriptItem = null;
		ScriptPlayer scriptPlayer = null;
		HumanPlayer player = null;
		EnemyParty enemyParty = null;
		
		if(game == null) {
			return;
		}
		
		scriptPlayer = game.getActiveScript();
		
		while(scriptPlayer.hasNext()) {
			scriptItem = scriptPlayer.next();
			
			if(scriptItem.getType() == ScriptItem.TYPE_TEXT) {
				//do nothing
				
				return;
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_GIVE_ITEM) {
				itemID = Integer.valueOf(scriptItem.getArgument1());
				
				item = ItemPool.getInstance().getPoolItem(itemID);
				
				game.getInventory().addItemToInventory(item);
				
				return;
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_GIVE_GOLD) {
				game.setGold(game.getGold() + Integer.valueOf(scriptItem.getArgument1()));
				
				return;
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_GIVE_EXPERIENCE) {
				PlayerExperienceGiver peg = new PlayerExperienceGiver(game);
				peg.giveExperienceToTeam(Integer.valueOf(scriptItem.getArgument1()));
				
				return;
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_STOP_IF) {
				variable = Integer.valueOf(scriptItem.getArgument1());
				
				if(scriptItem.getArgument2().equals("true")) {
					varValue = true;
				}
				else {
					varValue = false;
				}
				
				if(game.getVariable(variable) == varValue) {
					game.setActiveScript(null);
					return;
				}
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_SET_VARIABLE) {
				variable = Integer.valueOf(scriptItem.getArgument1());
				
				if(scriptItem.getArgument2().equals("true")) {
					varValue = true;
				}
				else {
					varValue = false;
				}
				
				game.setVariable(variable, varValue);
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_SHOW_SHOP) {
				shopID = Integer.valueOf(scriptItem.getArgument1());
				shop = ShopPool.getInstance().getPoolItem(shopID);
				game.setActiveShop(shop);
				
				mainFrame.showScreen(MainFrame.PANEL_SHOP);
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_GIVE_CHARACTER_WITH_EXP) {
				variable = Integer.valueOf(scriptItem.getArgument1());
				
				player = HeroPool.getInstance().getCopyOfItem(variable);
				
				if(game.getParty().size() < 4) {
					game.getParty().add(player);
				}
				
				variable = Integer.valueOf(scriptItem.getArgument2());
				
				PlayerExperienceGiver peg = new PlayerExperienceGiver(game);
				peg.giveExperienceToPlayer(player, variable);
				
				return;
			}
			else if(scriptItem.getType() == ScriptItem.TYPE_BATTLE) {
				variable = Integer.valueOf(scriptItem.getArgument1());
				
				enemyParty = EnemyPartyPool.getInstance().getPoolItem(variable).createCopy();
				
				game.setActiveEnemyParty(enemyParty);
				
				mainFrame.showScreen(MainFrame.PANEL_BATTLE);
			}
		}
		
		game.setActiveScript(null);
	}
	
}
