package com.gmail.nossr50.util;

import org.bukkit.entity.Player;

import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.player.UserManager;

public final class HardcoreManager {
    private HardcoreManager() {}

    public static void invokeStatPenalty(Player player) {
        double statLossPercentage = Config.getInstance().getHardcoreDeathStatPenaltyPercentage();

        PlayerProfile playerProfile = UserManager.getPlayer(player).getProfile();
        int totalLevelsLost = 0;

        for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
            if (!skillType.getHardcoreStatLossEnabled()) {
                break;
            }

            int playerSkillLevel = playerProfile.getSkillLevel(skillType);
            int playerSkillXpLevel = playerProfile.getSkillXpLevel(skillType);

            if (playerSkillLevel <= 0) {
                continue;
            }

            double statsLost = playerSkillLevel * (statLossPercentage * 0.01D);
            int levelsLost = (int) statsLost;
            int xpLost = (int) Math.floor(playerSkillXpLevel * (statsLost - levelsLost));

            totalLevelsLost += levelsLost;

            playerProfile.modifySkill(skillType, playerSkillLevel - levelsLost);
            playerProfile.removeXp(skillType, xpLost);
        }

        player.sendMessage(LocaleLoader.getString("Hardcore.DeathStatLoss.PlayerDeath", totalLevelsLost));
    }

    public static void invokeVampirism(Player killer, Player victim) {
        double vampirismStatLeechPercentage = Config.getInstance().getHardcoreVampirismStatLeechPercentage();

        PlayerProfile killerProfile = UserManager.getPlayer(killer).getProfile();
        PlayerProfile victimProfile = UserManager.getPlayer(victim).getProfile();
        int totalLevelsStolen = 0;

        for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
            if (!skillType.getHardcoreVampirismEnabled()) {
                break;
            }

            int killerSkillLevel = killerProfile.getSkillLevel(skillType);
            int victimSkillLevel = victimProfile.getSkillLevel(skillType);

            if (victimSkillLevel <= 0 || victimSkillLevel < killerSkillLevel / 2) {
                continue;
            }

            int victimSkillXpLevel = victimProfile.getSkillXpLevel(skillType);

            double statsStolen = victimSkillLevel * (vampirismStatLeechPercentage * 0.01D);
            int levelsStolen = (int) statsStolen;
            int xpStolen = (int) Math.floor(victimSkillXpLevel * (statsStolen - levelsStolen));

            totalLevelsStolen += levelsStolen;

            killerProfile.modifySkill(skillType, killerSkillLevel + levelsStolen);
            killerProfile.addXp(skillType, xpStolen);

            victimProfile.modifySkill(skillType, victimSkillLevel - levelsStolen);
            victimProfile.removeXp(skillType, xpStolen);
        }

        if (totalLevelsStolen > 0) {
            killer.sendMessage(LocaleLoader.getString("Hardcore.Vampirism.Killer.Success", totalLevelsStolen, victim.getName()));
            victim.sendMessage(LocaleLoader.getString("Hardcore.Vampirism.Victim.Success", killer.getName(), totalLevelsStolen));
        }
        else {
            killer.sendMessage(LocaleLoader.getString("Hardcore.Vampirism.Killer.Failure", victim.getName()));
            victim.sendMessage(LocaleLoader.getString("Hardcore.Vampirism.Victim.Failure", killer.getName()));
        }
    }

    /**
     * Check if Hardcore Stat Loss is enabled for one or more skill types
     *
     * @return true if Stat Loss is enabled for one or more skill types
     */
    public static boolean isStatLossEnabled() {
        boolean enabled = false;

        for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
            if (skillType.getHardcoreStatLossEnabled()) {
                enabled = true;
                break;
            }
        }

        return enabled;
    }

    /**
     * Check if Hardcore Vampirism is enabled for one or more skill types
     *
     * @return true if Vampirism is enabled for one or more skill types
     */
    public static boolean isVampirismEnabled() {
        boolean enabled = false;

        for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
            if (skillType.getHardcoreVampirismEnabled()) {
                enabled = true;
                break;
            }
        }

        return enabled;
    }
}

@@ -100,16 +100,71 @@
 Config;%0A
+import com.gmail.nossr50.datatypes.player.McMMOPlayer;%0A
 import c
@@ -1497,24 +1497,298 @@
 pe, xpLost);
+%0A%0A            if (playerProfile.getSkillXpLevel(skillType) %3C 0) %7B%0A                playerProfile.setSkillXpLevel(skillType, 0);%0A            %7D%0A%0A            if (playerProfile.getSkillLevel(skillType) %3C 0) %7B%0A                playerProfile.modifySkill(skillType, 0);%0A            %7D
 %0A        %7D%0A%0A
@@ -2095,29 +2095,27 @@
     
+McMMO
 Player
-Profile
  killerP
 rofi
@@ -2110,22 +2110,21 @@
  killerP
-rofile
+layer
  = UserM
@@ -2147,16 +2147,68 @@
 (killer)
+;%0A        PlayerProfile killerProfile = killerPlayer
 .getProf
@@ -3161,56 +3161,34 @@
 lerP
-rofile.modifySkill(skillType, killerSkillLevel +
+layer.addLevels(skillType,
  lev
@@ -3222,20 +3222,33 @@
 lerP
-rofile.addXp
+layer.beginUnsharedXpGain
 (ski
@@ -3398,32 +3398,306 @@
 Type, xpStolen);
+%0A%0A            if (victimProfile.getSkillXpLevel(skillType) %3C 0) %7B%0A                victimProfile.setSkillXpLevel(skillType, 0);%0A            %7D%0A%0A            if (victimProfile.getSkillLevel(skillType) %3C 0) %7B%0A                victimProfile.modifySkill(skillType, 0);%0A            %7D
 %0A        %7D%0A%0A    

Add checks to rectify the potential for negative XP