package rpgEngine.Logic.Game;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Resources.LevelRequirementPool;

public class PlayerExperienceGiver {
	private Game game;
	
	public PlayerExperienceGiver(Game g) {
		game = g;
	}
	
	
	
	public void giveExperienceToTeam(Integer exp) {
		HumanPlayer player = null;
		
		for(Integer i = 0; i < game.getParty().size(); i++) {
			player = game.getParty().get(i);
			
			giveExperienceToPlayer(player, exp);
		}
		
	}
	
	
	public void giveExperienceToPlayer(HumanPlayer p, Integer exp) {
		Integer increase = 0;
		
		p.setExperience(p.getExperience() + exp);
		
		while(p.getExperience() >= p.getExperienceToNextLevel()) {
			p.setLevel(p.getLevel() + 1);
			p.setExperienceToNextLevel(LevelRequirementPool.getInstance().getPoolItem(p.getLevel()));
			
			//HP
			increase = (int) Math.ceil(0.1 * (double) p.getBaseMaxHP());
			p.setMaxHP(p.getBaseMaxHP() + increase);
			p.setHP(p.getMaxHP());
			
			//MP
			increase = (int) Math.ceil(0.1 * (double) p.getBaseMaxMP());
			p.setMaxMP(p.getBaseMaxMP() + increase);
			p.setMP(p.getMaxMP());
			
			//Attack
			increase = (int) Math.ceil(0.1 * (double) p.getBaseAttack());
			p.setAttack(p.getBaseAttack() + increase);

			//Defense
			increase = (int) Math.ceil(0.1 * (double) p.getBaseDefense());
			p.setDefense(p.getBaseDefense() + increase);
			
			//Magic
			increase = (int) Math.ceil(0.1 * (double) p.getBaseSkill());
			p.setSkill(p.getBaseSkill() + increase);
			
			//Barrier
			increase = (int) Math.ceil(0.1 * (double) p.getBaseBarrier());
			p.setBarrier(p.getBaseBarrier() + increase);
		}
	}
	
}
