package me.hammerle.supersnuvi.entity.components;

import me.hammerle.supersnuvi.entity.Entity;

public class DefaultHealth extends Health
{
    private final double maxHealth;
    private double health;
    
    private int hurtTicks;
    
    private int deathTicks;
    
    public DefaultHealth(Entity ent, Death death, double maxHealth) 
    {
        super(ent, death);
        this.maxHealth = maxHealth;
        this.health = maxHealth;
        this.deathTicks = -1;
    }

    @Override
    public void tick() 
    {
        if(hurtTicks > 0)
        {
            hurtTicks--;
        }
        else if(hurtTicks < 0)
        {
            hurtTicks++;
        }
        
        if(deathTicks != -1)
        {
            deathTicks++;
            if(deathTicks > ent.getAnimator().getDeathTicks())
            {
                death.onDeath(ent);
            }
        }
    }

    @Override
    public boolean isDead() 
    {
        return deathTicks != -1;
    }
    
    @Override
    public boolean wasHurt() 
    {
        return hurtTicks > 0;
    }

    @Override
    public boolean wasHealed() 
    {
        return hurtTicks < 0;
    }

    @Override
    public double getMaxHealth() 
    {
        return maxHealth;
    }

    @Override
    public double getHealth() 
    {
        return health;
    }

    @Override
    public void addHealth(double h) 
    {
        hurtTicks = h < 0 ? 10 : -5;
        health += h;
        if(health > maxHealth)
        {
            health = maxHealth;
        }
        else if(health < 0.0)
        {
            if(deathTicks == -1)
            {
                deathTicks = 0;
                ent.getAnimator().resetFrames();
            }
        }
    }
}
