package rpgEngine.Logic.Game;

import rpgEngine.Data.Abstract.Item;
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.Resources.SkillPool;

public class PlayerItemUse {
	private Inventory inventory;
	private InventoryItem invItem;
	
	public PlayerItemUse(Inventory inv, InventoryItem i) {
		invItem = i;
		inventory = inv;
	}
	
	
	public void useItem(HumanPlayer player) {
		Item item = null;
		Boolean decreaseItem = true;
		
		item = invItem.getItem();
		
		if(item.getType() == Item.TYPE_HEALING) {
			if((player.getHP() != 0) && (player.getHP() != player.getMaxHP())) {				
				player.setHP(player.getHP() + item.getStatChanges().getHP());
				
				if(player.getHP() > player.getMaxHP()) {
					player.setHP(player.getMaxHP());
				}
			}
			else {
				decreaseItem = false;
			}
		}
		else if(item.getType() == Item.TYPE_REVIVE) {
			if(player.getHP() == 0) {
				player.setHP(player.getHP() + item.getStatChanges().getHP());
				
				if(player.getHP() > player.getMaxHP()) {
					player.setHP(player.getMaxHP());
				}
			}
			else {
				decreaseItem = false;
			}
		}
		else if(item.getType() == Item.TYPE_MP_HEALING) {
			if((player.getMP() != player.getMaxMP())) {				
				player.setMP(player.getMP() + item.getStatChanges().getMP());
				
				if(player.getMP() > player.getMaxMP()) {
					player.setMP(player.getMaxMP());
				}
			}
			else {
				decreaseItem = false;
			}
		}
		else if(item.getType() == Item.TYPE_TOME) {
			Skill skill = SkillPool.getInstance().getPoolItem(item.getSkillID());
			
			if(player.getSkills().contains(skill) == false) {
				player.getSkills().add(skill);
			}
			else {
				decreaseItem = false;
			}
		}
		
		//Drop or Decrease Quantity
		if(decreaseItem) {
			if(invItem.getQuantity() == 1) {
				dropItem();
			}
			else {
				invItem.setQuantity(invItem.getQuantity()-1);
			}
		}
	}
	
	
	public void dropItem() {
		inventory.getItems().remove(invItem);
	}
	
	
}
