package rpgEngine.Logic.Game;

import java.util.List;
import rpgEngine.Data.Abstract.Enemy.Enemy;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Data.Abstract.Party.Skill;

public class PlayerAttackSkillUse {
	private HumanPlayer player;
	private Skill skill;
	
	public PlayerAttackSkillUse(HumanPlayer p, Skill s) {
		player = p;
		skill = s;
	}
	
	
	public String useSkillOnAllEnemies(List<Enemy> enemyParty) {
		String resultMessage = "";
		Enemy enemy = null;
		Double randomMultiplier = 1.0;
		Double skillMultiplier = 1.0;
		Integer damagePoints = 0;
		Integer averageDefense = 0;
		Integer totalDefense = 0;
		Integer counter = 0;
		
		//Random Multiplier
		randomMultiplier = 0.8 + (Math.random() * (1.2 - 0.8));
		
		//Skill Multiplier
		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 Damage
		damagePoints = (int)((double)skill.getBaseDamage() * randomMultiplier * skillMultiplier);
		
		//Defense Consideration
		averageDefense = 0;
		totalDefense = 0;
		counter = 0;
		for(Integer i = 0; i < enemyParty.size(); i++) {
			enemy = enemyParty.get(i);
			
			if(enemy.getHP() > 0) {
				if(skill.getBasedOnStat() == Skill.STAT_ATTACK) {
					totalDefense = totalDefense + enemy.getDefense();
				}
				else {
					totalDefense = totalDefense + enemy.getBarrier();
				}
				
				counter++;
			}
		}
		averageDefense = totalDefense / counter;

		damagePoints = damagePoints - (averageDefense / 2);
		
		
		if(damagePoints <= 0) {
			damagePoints = 1;
		}
		
		for(Integer i = 0; i < enemyParty.size(); i++) {
			enemy = enemyParty.get(i);
			
			enemy.setHP(enemy.getHP() - damagePoints);
			
			if(enemy.getHP() < 0) {
				enemy.setHP(0);
			}
		}
		
		player.setMP(player.getMP() - skill.getMpCost());
		
		resultMessage = skill.getName() + " caused " + damagePoints + " damage to all enemies";

		return resultMessage;
	}
	
	
	public String useSkillOnEnemy(Enemy enemy) {
		String resultMessage = "";
		Double randomMultiplier = 1.0;
		Double skillMultiplier = 1.0;
		Integer damagePoints = 0;
		
		//Random Multiplier
		randomMultiplier = 0.8 + (Math.random() * (1.2 - 0.8));
		
		//Skill Multiplier
		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 Damage
		damagePoints = (int)((double)skill.getBaseDamage() * randomMultiplier * skillMultiplier);
		
		//Defense Consideration
		if(skill.getBasedOnStat() == Skill.STAT_ATTACK) {
			damagePoints = damagePoints - (enemy.getDefense() / 2);
		}
		else {
			damagePoints = damagePoints - (enemy.getBarrier() / 2);
		}
		
		if(damagePoints <= 0) {
			damagePoints = 1;
		}
		
		enemy.setHP(enemy.getHP() - damagePoints);
		
		if(enemy.getHP() < 0) {
			enemy.setHP(0);
		}
		
		player.setMP(player.getMP() - skill.getMpCost());
		
		resultMessage = skill.getName() + " caused " + damagePoints + " damage";
		
		return resultMessage;
	}
	
	
}
