from core.tuning.skill import SkillTuning
from siege import game


class Stoneskin(object):
    NAME = "Stoneskin"
    ICON = SkillTuning.STONESKIN.ICON
    STONESKIN_AMOUNT = 100

    def __init__(self, owner, level, duration):
        self.ownerId = owner.id
        self.life = self.STONESKIN_AMOUNT * (1.0 + (level - 1) * 0.2)
        game.events['apply_damage'].listen(self.handleApplyDamage)

    def onRemove(self, owner):
        game.events['apply_damage'].remove(self.handleApplyDamage)

    def handleApplyDamage(self, results, target, origin, damage, isCritical, knockback):
        if self.ownerId == target.id:
            protection = min(self.life, results.damage)
            self.life -= protection
            results.damage -= protection
            # TODO: Consider showing animation on top of target
            if not self.life:
                target.effects.remove(Stoneskin.NAME)

    def isPositive(self):
        return True

    @staticmethod
    def register():
        game.effects.register(Stoneskin.NAME, Stoneskin)
