package rpgEngine.Logic.Game;

import java.util.List;

import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Data.Abstract.Party.Skill;

public class PlayerHealingSkillUse {
	private HumanPlayer player;
	private Skill skill;
	
	public PlayerHealingSkillUse(HumanPlayer p, Skill s) {
		player = p;
		skill = s;
	}
	
	
	public void useSkillOnPlayer(HumanPlayer targetPlayer) {		
		if(player.getMP() >= skill.getMpCost()) {
			
			useSkillOnTarget(targetPlayer);
		
			player.setMP(player.getMP() - skill.getMpCost());
		}
	}
	
	
	public void useSkillOnParty(List<HumanPlayer> party) {
		if(player.getMP() >= skill.getMpCost()) {
		
			for(Integer i = 0; i < party.size(); i++) {
				useSkillOnTarget(party.get(i));
			}
			
			player.setMP(player.getMP() - skill.getMpCost());
		}
	}
	
	
	private void useSkillOnTarget(HumanPlayer p) {
		Double randomMultiplier = 1.0;
		Double skillMultiplier = 1.0;
		Integer healPoints = 0;
		
		//Random Multiplier
		randomMultiplier = 0.8 + (Math.random() * (1.2 - 0.8));
		
		//Skill Multiplier
		if(skill.getBasedOnStat() == Skill.STAT_MAGIC) {
			skillMultiplier = ((double)player.getSkill()) / ((double)player.getAttack());
		}
		else if(skill.getBasedOnStat() == Skill.STAT_ATTACK) {
			skillMultiplier = ((double)player.getAttack()) / ((double)player.getSkill());
		}
		else {
			skillMultiplier = ((double)player.getSkill()) / ((double)player.getAttack());
		}
		
		if(skillMultiplier < 0.8) {
			skillMultiplier = 0.8;
		}
		if(skillMultiplier > 1.2) {
			skillMultiplier = 1.2;
		}
		
		//Determine Healing
		healPoints = (int)((double)skill.getBaseDamage() * randomMultiplier * skillMultiplier);
		
		if(skill.getType() == Skill.TYPE_HEALING) {
			if(p.getHP() > 0) {
				p.setHP(p.getHP() + healPoints);
				
				if(p.getHP() > p.getMaxHP()) {
					p.setHP(p.getMaxHP());
				}
			}
		}
		else if (skill.getType() == Skill.TYPE_REVIVE) {
			if(healPoints <= 0) {
				healPoints = 1;
			}
			
			p.setHP(p.getHP() + healPoints);
			
			if(p.getHP() > p.getMaxHP()) {
				p.setHP(p.getMaxHP());
			}
		}
		
	}
	
	
	public Boolean canUseSkill() {
		
		if(player.getMP() >= skill.getMpCost()) {
			return true;
		}
		else {
			return false;
		}
		
	}
	
}
