package rpgEngine.UI;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Item;
import rpgEngine.Data.Abstract.Enemy.Enemy;
import rpgEngine.Data.Abstract.Enemy.EnemyParty;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Data.Abstract.Party.Inventory;
import rpgEngine.Data.Abstract.Party.InventoryItem;
import rpgEngine.Data.Abstract.Party.Skill;
import rpgEngine.Logic.Camera.BattleCamera;
import rpgEngine.Logic.Camera.GenericCamera;
import rpgEngine.Logic.Game.EnemyFightUse;
import rpgEngine.Logic.Game.PlayerAttackSkillUse;
import rpgEngine.Logic.Game.PlayerExperienceGiver;
import rpgEngine.Logic.Game.PlayerFightUse;
import rpgEngine.Logic.Game.PlayerHealingSkillUse;
import rpgEngine.Logic.Game.PlayerItemUse;


public class BattlePanel extends GenericPanel {
	private static final long serialVersionUID = 6538729434640140830L;
	private GenericCamera camera;
	private Integer screenMode;
	private Integer cursorPosition;
	private Integer sourceChoiceCursorPosition;
	private Integer playerChoiceCursorPosition;
	private Integer enemyChoiceCursorPosition;
	private Integer currentPlayer;
	private Integer currentEnemy;
	private Integer battleStep;
	private String dialogText;
	
	
	public static final Integer BATTLE_PLAYER = 0;
	public static final Integer BATTLE_ENEMY = 1;
	public static final Integer BATTLE_VICTORY = 2;
	public static final Integer BATTLE_FINISHED = 3;
	
	public static final Integer SCREEN_NORMAL = 0;
	public static final Integer SCREEN_CHOOSE_SKILL = 1;
	public static final Integer SCREEN_CHOOSE_ITEM = 2;
	public static final Integer SCREEN_CHOOSE_ENEMY_FOR_FIGHT = 3;
	public static final Integer SCREEN_CHOOSE_PLAYER_FOR_ITEM = 4;
	public static final Integer SCREEN_DIALOG = 5;
	public static final Integer SCREEN_CHOOSE_PLAYER_FOR_SKILL = 6;
	public static final Integer SCREEN_CHOOSE_ENEMY_FOR_SKILL = 7;
	
	public static final Integer CURSOR_FIGHT = 0;
	public static final Integer CURSOR_SKILL = 1;
	public static final Integer CURSOR_ITEM = 2;
	public static final Integer CURSOR_DEFEND = 3;
	public static final Integer CURSOR_RUN = 4;
	
	
	
	public BattlePanel(Integer id, MainFrame p) {
		super(id,p);
		
		camera = new BattleCamera(this);
		
		dialogText = "";
		cursorPosition = CURSOR_FIGHT;
		enemyChoiceCursorPosition = 0;
		playerChoiceCursorPosition = 0;
		screenMode = SCREEN_NORMAL;
		sourceChoiceCursorPosition = 0;
		currentPlayer = 0;
		currentEnemy = 0;
		battleStep = BATTLE_PLAYER;
	}
	
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		Game game = this.getParentMainFrame().getGame();
		
		if ((e.getKeyCode() == KeyEvent.VK_ENTER) || (e.getKeyCode() == KeyEvent.VK_SPACE)) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			processEscapeKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(battleStep == BATTLE_PLAYER) {
				if(screenMode == SCREEN_NORMAL) {
					if(cursorPosition == CURSOR_FIGHT) {
						if(game.getActiveEnemyParty().isBossBattle()) {
							cursorPosition = CURSOR_DEFEND;
						}
						else {
							cursorPosition = CURSOR_RUN;
						}
					}
					else if(cursorPosition == CURSOR_SKILL) {
						cursorPosition = CURSOR_FIGHT;
					}
					else if(cursorPosition == CURSOR_ITEM) {
						cursorPosition = CURSOR_SKILL;
					}
					else if(cursorPosition == CURSOR_DEFEND) {
						cursorPosition = CURSOR_ITEM;
					}
					else if(cursorPosition == CURSOR_RUN) {
						cursorPosition = CURSOR_DEFEND;
					}
				}
				else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_FIGHT) {
					chooseEnemyMove("LEFT");
				}
				else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_SKILL) {
					chooseEnemyMove("LEFT");
				}
				else if(screenMode == SCREEN_CHOOSE_ITEM) {
					moveItemPosition("LEFT");
				}
				else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_ITEM) {
					choosePlayerMove("LEFT");
				}
				else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_SKILL) {
					choosePlayerMove("LEFT");
				}
				else if(screenMode == SCREEN_CHOOSE_SKILL) {
					moveSkillPosition("LEFT");
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			if(battleStep == BATTLE_PLAYER) {
				if(screenMode == SCREEN_NORMAL) {
					if(cursorPosition == CURSOR_FIGHT) {
						cursorPosition = CURSOR_SKILL;
					}
					else if(cursorPosition == CURSOR_SKILL) {
						cursorPosition = CURSOR_ITEM;
					}
					else if(cursorPosition == CURSOR_ITEM) {
						cursorPosition = CURSOR_DEFEND;
					}
					else if(cursorPosition == CURSOR_DEFEND) {
						if(game.getActiveEnemyParty().isBossBattle()) {
							cursorPosition = CURSOR_FIGHT;
						}
						else {
							cursorPosition = CURSOR_RUN;
						}
					}
					else if(cursorPosition == CURSOR_RUN) {
						cursorPosition = CURSOR_FIGHT;
					}
				}
				else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_FIGHT) {
					chooseEnemyMove("RIGHT");
				}
				else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_SKILL) {
					chooseEnemyMove("RIGHT");
				}
				else if(screenMode == SCREEN_CHOOSE_ITEM) {
					moveItemPosition("RIGHT");
				}
				else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_ITEM) {
					choosePlayerMove("RIGHT");
				}
				else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_SKILL) {
					choosePlayerMove("RIGHT");
				}
				else if(screenMode == SCREEN_CHOOSE_SKILL) {
					moveSkillPosition("RIGHT");
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP){
			if(battleStep == BATTLE_PLAYER) {
				if(screenMode == SCREEN_CHOOSE_ITEM) {
					moveItemPosition("UP");
				}
				else if(screenMode == SCREEN_CHOOSE_SKILL) {
					moveSkillPosition("UP");
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN){
			if(battleStep == BATTLE_PLAYER) {
				if(screenMode == SCREEN_CHOOSE_ITEM) {
					moveItemPosition("DOWN");
				}
				else if(screenMode == SCREEN_CHOOSE_SKILL) {
					moveSkillPosition("DOWN");
				}
			}
		}
	}
	
	
	private void processActionKey() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Skill skill = null;
		
		if(battleStep == BATTLE_PLAYER) {
			if(screenMode == SCREEN_NORMAL) {
				if(cursorPosition == CURSOR_RUN) {
					runAway();
				}
				else if(cursorPosition == CURSOR_ITEM) {
					if(game.getInventory().getItems().size() != 0) {
						sourceChoiceCursorPosition = 0;
						playerChoiceCursorPosition = getPositionOfFirstAlivePlayer();
						enemyChoiceCursorPosition = getPositionOfFirstAliveEnemy();
						screenMode = SCREEN_CHOOSE_ITEM;
					}
				}
				else if(cursorPosition == CURSOR_SKILL) {
					if(game.getParty().get(currentPlayer).getSkills().size() != 0) {
						sourceChoiceCursorPosition = 0;
						playerChoiceCursorPosition = getPositionOfFirstAlivePlayer();
						enemyChoiceCursorPosition = getPositionOfFirstAliveEnemy();
						screenMode = SCREEN_CHOOSE_SKILL;
					}
				}
				else if(cursorPosition == CURSOR_FIGHT) {
					enemyChoiceCursorPosition = getPositionOfFirstAliveEnemy();
					screenMode = SCREEN_CHOOSE_ENEMY_FOR_FIGHT;
				}
				else if(cursorPosition == CURSOR_DEFEND) {
					defend();
				}
			}
			else if(screenMode == SCREEN_DIALOG) {
				proceedFromDialog();
			}
			else if(screenMode == SCREEN_CHOOSE_ITEM) {
				Item item = null;
				
				item = game.getInventory().getItems().get(sourceChoiceCursorPosition).getItem();
				
				if ((item.getType() == Item.TYPE_HEALING) || (item.getType() == Item.TYPE_MP_HEALING) || (item.getType() == Item.TYPE_REVIVE)) {
					playerChoiceCursorPosition = getPositionOfFirstAlivePlayer();
					screenMode = SCREEN_CHOOSE_PLAYER_FOR_ITEM;
				}
			}
			else if(screenMode == SCREEN_CHOOSE_SKILL) {
				player = game.getParty().get(currentPlayer);
				skill = player.getSkills().get(sourceChoiceCursorPosition);
				
				if(player.getMP() >= skill.getMpCost()) {
					if(skill.getType() == Skill.TYPE_ATTACK) {
						if(skill.getTarget() == Skill.TARGET_ALL) {
							useAttackSkillOnAllEnemies();
						}
						else {
							//select enemy to attack
							enemyChoiceCursorPosition = getPositionOfFirstAliveEnemy();
							screenMode = SCREEN_CHOOSE_ENEMY_FOR_SKILL;
						}
					}
					else {
						if(skill.getTarget() == Skill.TARGET_ALL) {						
							useHealingSkillOnParty();
						}
						else {
							//select human to heal
							playerChoiceCursorPosition = getPositionOfFirstAlivePlayer();
							screenMode = SCREEN_CHOOSE_PLAYER_FOR_SKILL;
						}
					}
				}
			}
			else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_FIGHT) {
				fight();
			}
			else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_ITEM) {
				useItemOnPlayer();
			}
			else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_SKILL) {
				player = game.getParty().get(currentPlayer);
				skill = player.getSkills().get(sourceChoiceCursorPosition);
				
				if(player.getMP() >= skill.getMpCost()) {
					useAttackSkillOnEnemy();
				}
			}
			else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_SKILL) {
				player = game.getParty().get(currentPlayer);
				skill = player.getSkills().get(sourceChoiceCursorPosition);
				
				if(player.getMP() >= skill.getMpCost()) {
					useHealingSkillOnPlayer();
				}
			}
		}
		else if(battleStep == BATTLE_ENEMY) {
			if(screenMode == SCREEN_DIALOG) {
				proceedFromDialog();
			}
		}
		else if(battleStep == BATTLE_VICTORY) {
			if(screenMode == SCREEN_DIALOG) {
				proceedFromDialog();
			}
		}
		else if(battleStep == BATTLE_FINISHED) {
			if(screenMode == SCREEN_DIALOG) {
				proceedFromDialog();
			}
		}
	}
	
	
	private void processEscapeKey() {
		if(battleStep == BATTLE_PLAYER) {
			if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_FIGHT) {
				screenMode = SCREEN_NORMAL;
			}
			else if(screenMode == SCREEN_CHOOSE_ITEM) {
				screenMode = SCREEN_NORMAL;
			}
			else if(screenMode == SCREEN_CHOOSE_SKILL) {
				screenMode = SCREEN_NORMAL;
			}
			else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_ITEM) {
				screenMode = SCREEN_NORMAL;
			}
			else if(screenMode == SCREEN_CHOOSE_PLAYER_FOR_SKILL) {
				screenMode = SCREEN_NORMAL;
			}
			else if(screenMode == SCREEN_CHOOSE_ENEMY_FOR_SKILL) {
				screenMode = SCREEN_NORMAL;
			}
		}
	}
	
	
	@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
     
        BufferedImage img = camera.getImage();
        
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
        
        
    }

	
	public Integer getCursorPosition() {
		return cursorPosition;
	}

	public void setCursorPosition(Integer cursorPosition) {
		this.cursorPosition = cursorPosition;
	}

	public Integer getEnemyChoiceCursorPosition() {
		return enemyChoiceCursorPosition;
	}

	public void setEnemyChoiceCursorPosition(Integer choiceCursorPosition) {
		this.enemyChoiceCursorPosition = choiceCursorPosition;
	}

	public Integer getScreenMode() {
		return screenMode;
	}

	public void setScreenMode(Integer screenMode) {
		this.screenMode = screenMode;
	}

	public Integer getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(Integer currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	public Integer getCurrentEnemy() {
		return currentEnemy;
	}

	public void setCurrentEnemy(Integer currentEnemy) {
		this.currentEnemy = currentEnemy;
	}

	public Integer getBattleStep() {
		return battleStep;
	}

	public void setBattleStep(Integer battleStep) {
		this.battleStep = battleStep;
	}
	
	public String getDialogText() {
		return dialogText;
	}

	public void setDialogText(String dialogText) {
		this.dialogText = dialogText;
	}
	
	public Integer getPlayerChoiceCursorPosition() {
		return playerChoiceCursorPosition;
	}

	public void setPlayerChoiceCursorPosition(Integer playerChoiceCursorPosition) {
		this.playerChoiceCursorPosition = playerChoiceCursorPosition;
	}
	
	public Integer getSourceChoiceCursorPosition() {
		return sourceChoiceCursorPosition;
	}
	
	public void setSourceChoiceCursorPosition(Integer sourceChoiceCursorPosition) {
		this.sourceChoiceCursorPosition = sourceChoiceCursorPosition;
	}
	
	
	private void moveSkillPosition(String dir) {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = game.getParty().get(currentPlayer);
		Integer numSkills = player.getSkills().size() - 1;
		
		if(dir.equals("UP")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition - 2;
			
			if(sourceChoiceCursorPosition < 0) {
				sourceChoiceCursorPosition = 0;
			}
		}
		else if(dir.equals("DOWN")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition + 2;
			
			if(sourceChoiceCursorPosition > numSkills) {
				sourceChoiceCursorPosition = numSkills;
			}
		}
		else if(dir.equals("LEFT")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition - 1;
			
			if(sourceChoiceCursorPosition < 0) {
				sourceChoiceCursorPosition = 0;
			}
		}
		else if(dir.equals("RIGHT")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition + 1;
			
			if(sourceChoiceCursorPosition > numSkills) {
				sourceChoiceCursorPosition = numSkills;
			}
		}
	}
	
	
	private void moveItemPosition(String dir) {
		Game game = this.getParentMainFrame().getGame();
		Integer numItems = game.getInventory().getItems().size() - 1; 
		
		if(dir.equals("UP")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition - 2;
			
			if(sourceChoiceCursorPosition < 0) {
				sourceChoiceCursorPosition = 0;
			}
		}
		else if(dir.equals("DOWN")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition + 2;
			
			if(sourceChoiceCursorPosition > numItems) {
				sourceChoiceCursorPosition = numItems;
			}
		}
		else if(dir.equals("LEFT")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition - 1;
			
			if(sourceChoiceCursorPosition < 0) {
				sourceChoiceCursorPosition = 0;
			}
		}
		else if(dir.equals("RIGHT")) {
			sourceChoiceCursorPosition = sourceChoiceCursorPosition + 1;
			
			if(sourceChoiceCursorPosition > numItems) {
				sourceChoiceCursorPosition = numItems;
			}
		}
	}
	
	
	private void runAway() {
		Random random = new Random();
		
		if(random.nextDouble() > 0.5) {
			endBattle();
		}
		else {
			dialogText = "Run Failed";
			screenMode = SCREEN_DIALOG;
			
			nextTurn();
		}
	}
	
	
	private Integer getPositionOfFirstAlivePlayer() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		
		for(Integer i = 0; i < game.getParty().size(); i++) {
			player = game.getParty().get(i);
			
			if(player.getHP() > 0) {
				return i;
			}
		}
		
		return -1;	
	}
	
	
	private Integer getPositionOfFirstAliveEnemy() {
		Game game = this.getParentMainFrame().getGame();
		EnemyParty enemyParty = game.getActiveEnemyParty();
		Enemy enemy = null;
		
		for(Integer i = 0; i < enemyParty.getEnemies().size(); i++) {
			enemy = enemyParty.getEnemies().get(i);
			
			if(enemy.getHP() > 0) {
				return i;
			}
		}
		
		return -1;
	}
	
	
	private void choosePlayerMove(String dir) {
		Game game = this.getParentMainFrame().getGame();
		//HumanPlayer player = null;
		Integer currentPos = playerChoiceCursorPosition;
		Integer newPos = currentPos;
		Integer checkCounter = 0;
		Integer numPlayers = game.getParty().size();
		
		while(checkCounter < numPlayers) {
			if(dir.equals("RIGHT")) {
				currentPos++;
			}
			else {
				currentPos--;
			}
			
			if(currentPos < 0) {
				currentPos = numPlayers - 1;
			}
			else if(currentPos >= numPlayers) {
				currentPos = 0;
			}
		
			//player = game.getParty().get(currentPos);
			
			//if (player.getHP() > 0) {
				newPos = currentPos;
				break;
			//}
			
		}
		
		playerChoiceCursorPosition = newPos;	
	}
	
	
	private void chooseEnemyMove(String dir) {
		Game game = this.getParentMainFrame().getGame();
		EnemyParty enemyParty = game.getActiveEnemyParty();
		Enemy enemy = null;
		Integer currentPos = enemyChoiceCursorPosition;
		Integer newPos = currentPos;
		Integer checkCounter = 0;
		Integer numEnemies = enemyParty.getEnemies().size();
		
		while(checkCounter < numEnemies) {
			if(dir.equals("RIGHT")) {
				currentPos++;
			}
			else {
				currentPos--;
			}
			
			if(currentPos < 0) {
				currentPos = numEnemies - 1;
			}
			else if(currentPos >= numEnemies) {
				currentPos = 0;
			}
		
			enemy = enemyParty.getEnemies().get(currentPos);
			
			if (enemy.getHP() > 0) {
				newPos = currentPos;
				break;
			}
			
		}
		
		enemyChoiceCursorPosition = newPos;
	}
	
	
	private void nextTurn() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Enemy enemy = null;
		
		
		if(battleStep == BATTLE_PLAYER) {
			while(currentPlayer < game.getParty().size()) {
				currentPlayer++;
			
				try {
					player = game.getParty().get(currentPlayer);
				}
				catch(Exception e) {
					player = null;
				}
				
				if(player != null) {
					if(player.getHP() > 0) {
						break;
					}
				}
				else {
					break;
				}
			}
			
			if(currentPlayer >= game.getParty().size()) {
				battleStep = BATTLE_ENEMY;
				currentPlayer = getPositionOfFirstAlivePlayer();
				currentEnemy = getPositionOfFirstAliveEnemy();
				
			}
		}
		else if(battleStep == BATTLE_ENEMY) {
			while(currentEnemy < game.getActiveEnemyParty().getEnemies().size()) {
				currentEnemy++;
			
				try {
					enemy = game.getActiveEnemyParty().getEnemies().get(currentEnemy);
				}
				catch(Exception e) {
					enemy = null;
				}
				
				if(enemy != null) {
					if(enemy.getHP() > 0) {
						break;
					}
				}
				else {
					break;
				}
			}

			if(currentEnemy >= game.getActiveEnemyParty().getEnemies().size()) {
				battleStep = BATTLE_PLAYER;
				currentPlayer = getPositionOfFirstAlivePlayer();
				currentEnemy = getPositionOfFirstAliveEnemy();
			}
		}
	}
	
	
	private void endBattle() {
		cursorPosition = CURSOR_FIGHT;
		enemyChoiceCursorPosition = 0;
		playerChoiceCursorPosition = 0;
		screenMode = SCREEN_NORMAL;
		currentPlayer = 0;
		currentEnemy = 0;
		battleStep = BATTLE_PLAYER;	
		dialogText = "";
		sourceChoiceCursorPosition = 0;
		
		this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);
	}
	
	
	private void enemyTurn() {
		Game game = this.getParentMainFrame().getGame();
		Enemy enemy = null;
		EnemyFightUse efu = null;
		
		enemy = game.getActiveEnemyParty().getEnemies().get(enemyChoiceCursorPosition);
		
		efu = new EnemyFightUse(enemy,game.getParty());
		dialogText = efu.fight();
		
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void useItemOnPlayer() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Inventory inv = null;
		InventoryItem invItem = null;
		Integer targetPlayer = playerChoiceCursorPosition;
		Integer itemIndex = sourceChoiceCursorPosition;
		PlayerItemUse piu = null;
		
		player = game.getParty().get(targetPlayer);
		inv = game.getInventory();
		invItem = inv.getItems().get(itemIndex);
	
		dialogText = invItem.getItem().getName() + " used on " + player.getName();
		
		piu = new PlayerItemUse(inv,invItem);
		piu.useItem(player);
		
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void victory() {
		Game game = this.getParentMainFrame().getGame();
		EnemyParty enemyParty = game.getActiveEnemyParty();
		Enemy enemy = null;
		Integer gold = 0;
		Integer experience = 0;
		Boolean receivedItem = false;
		Item item = null;
		Random random = new Random();
		List<Item> itemsToGive = new ArrayList<Item>();
		
		//determine gold, experience, items
		for(Integer i = 0; i < enemyParty.getEnemies().size(); i++) {
			enemy = enemyParty.getEnemies().get(i);
			
			gold = gold + enemy.getGold();
			experience = experience + enemy.getExperience();
			
			if(enemy.getItem() != null) {
				item = enemy.getItem();
				
				if(random.nextDouble() < enemy.getItemChance()) {
					itemsToGive.add(item);
				}
			}
		}

		//give gold
		game.setGold(game.getGold() + gold);
			
		//give experience
		PlayerExperienceGiver peg = new PlayerExperienceGiver(game);
		peg.giveExperienceToTeam(experience);
		
		//give items
		for(Integer i = 0; i < itemsToGive.size(); i++) {
			item = itemsToGive.get(i);
			
			game.getInventory().addItemToInventory(item);
			
			receivedItem = true;
		}
		
		//show dialog from current victory message
		dialogText = "Got " + experience + " exp & " + gold + " g!";
		
		if(receivedItem) {
			dialogText = dialogText + " Also got some loot!";
		}
		
		battleStep = BATTLE_FINISHED;
	}
	
	
	private void defend() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		
		player = game.getParty().get(currentPlayer);
		
		dialogText = player.getName() + " defended";
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void fight() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Enemy enemy = null;
		PlayerFightUse pfe = null;
		
		player = game.getParty().get(currentPlayer);
		enemy = game.getActiveEnemyParty().getEnemies().get(enemyChoiceCursorPosition);
		
		pfe = new PlayerFightUse(player,enemy);
		dialogText = pfe.fight();
		
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void useAttackSkillOnAllEnemies() {		
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Skill skill = null;
		PlayerAttackSkillUse pasu = null;
		List<Enemy> enemyParty = null;
		
		player = game.getParty().get(currentPlayer);
		enemyParty = game.getActiveEnemyParty().getEnemies();
		skill = player.getSkills().get(sourceChoiceCursorPosition);
		
		pasu = new PlayerAttackSkillUse(player,skill);
		dialogText = pasu.useSkillOnAllEnemies(enemyParty);
		
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void useAttackSkillOnEnemy() {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Skill skill = null;
		Enemy target = null;
		PlayerAttackSkillUse pasu = null;
		
		player = game.getParty().get(currentPlayer);
		target = game.getActiveEnemyParty().getEnemies().get(enemyChoiceCursorPosition);
		skill = player.getSkills().get(sourceChoiceCursorPosition);
		
		pasu = new PlayerAttackSkillUse(player,skill);
		dialogText = pasu.useSkillOnEnemy(target);
		
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void useHealingSkillOnPlayer() {		
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		HumanPlayer target = null;
		Skill skill = null;
		PlayerHealingSkillUse phsu = null;
		
		player = game.getParty().get(currentPlayer);
		target = game.getParty().get(playerChoiceCursorPosition);
		skill = player.getSkills().get(sourceChoiceCursorPosition);
		phsu = new PlayerHealingSkillUse(player,skill);
		
		if(phsu.canUseSkill() == false) {
			screenMode = SCREEN_NORMAL;
			return;
		}
		
		phsu.useSkillOnPlayer(target);
		
		dialogText = "Used " + skill.getName() + " on " + target.getName();
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void useHealingSkillOnParty() {		
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Skill skill = null;
		PlayerHealingSkillUse phsu = null;
		
		player = game.getParty().get(currentPlayer);
		skill = player.getSkills().get(sourceChoiceCursorPosition);
		phsu = new PlayerHealingSkillUse(player,skill);
		
		if(phsu.canUseSkill() == false) {
			screenMode = SCREEN_NORMAL;
			return;
		}
		
		phsu.useSkillOnParty(game.getParty());
		
		dialogText = "Used " + skill.getName() + " on party";
		screenMode = SCREEN_DIALOG;
		nextTurn();
	}
	
	
	private void checkForVictory() {
		Boolean allDead = true;
		Game game = this.getParentMainFrame().getGame();
		EnemyParty enemyParty = game.getActiveEnemyParty();
		Enemy enemy = null;
		
		for(Integer i = 0; i < enemyParty.getEnemies().size(); i++) {
			enemy = enemyParty.getEnemies().get(i);
			
			if(enemy.getHP() > 0) {
				allDead = false;
			}
		}
		
		if(allDead) {
			battleStep = BATTLE_VICTORY;
		}
	}
	
	
	private void checkForLoss() {
		Boolean allDead = true;
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		
		for(Integer i = 0; i < game.getParty().size(); i++) {
			player = game.getParty().get(i);
			
			if(player.getHP() > 0) {
				allDead = false;
			}
		}
		
		if(allDead) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_TITLE);
		}
	}
	
	
	private void proceedFromDialog() {
		
		if ((battleStep != BATTLE_VICTORY) && (battleStep != BATTLE_FINISHED)) {
			checkForLoss();
			
			checkForVictory();
		}
		
		if(battleStep == BATTLE_ENEMY) {
			enemyTurn();
		}
		else if(battleStep == BATTLE_PLAYER) { 
			screenMode = SCREEN_NORMAL;
			cursorPosition = CURSOR_FIGHT;
			enemyChoiceCursorPosition = 0;
		}
		else if(battleStep == BATTLE_VICTORY) {
			victory();
		}
		else if(battleStep == BATTLE_FINISHED) {
			endBattle();
		}
		
	}


	
	


	
}
