package electrosphere.entity.state.life;

import electrosphere.entity.Entity;
import electrosphere.game.data.creature.type.HealthSystem;

public class LifeState {
    
    Entity parent;
    
    boolean isAlive;
    boolean isInvincible;
    int lifeCurrent;
    int lifeMax;
    int iFrameMaxCount;
    int iFrameCurrent;

    public LifeState(Entity parent, HealthSystem system){
        this.parent = parent;
        isAlive = true;
        isInvincible = false;
        lifeMax = system.getMaxHealth();
        lifeCurrent = lifeMax;
        iFrameMaxCount = system.getOnDamageIFrames();
        iFrameCurrent = 0;
    }
    
    public LifeState(Entity parent, boolean isAlive, boolean isInvincible, int lifeCurrent, int lifeMax, int iFrameMaxCount) {
        this.parent = parent;
        this.isAlive = isAlive;
        this.isInvincible = isInvincible;
        this.lifeCurrent = lifeCurrent;
        this.lifeMax = lifeMax;
        this.iFrameMaxCount = iFrameMaxCount;
    }

    public boolean isIsAlive() {
        return isAlive;
    }

    public boolean isIsInvincible() {
        return isInvincible;
    }

    public int getLifeCurrent() {
        return lifeCurrent;
    }

    public int getLifeMax() {
        return lifeMax;
    }

    public void setIsAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }

    public void setIsInvincible(boolean isInvincible) {
        this.isInvincible = isInvincible;
    }

    public void setLifeCurrent(int lifeCurrent) {
        this.lifeCurrent = lifeCurrent;
    }

    public void setLifeMax(int lifeMax) {
        this.lifeMax = lifeMax;
    }

    public int getiFrameMaxCount() {
        return iFrameMaxCount;
    }

    public int getiFrameCurrent() {
        return iFrameCurrent;
    }

    public void setiFrameMaxCount(int iFrameMaxCount) {
        this.iFrameMaxCount = iFrameMaxCount;
    }

    public void setiFrameCurrent(int iFrameCurrent) {
        this.iFrameCurrent = iFrameCurrent;
    }
    
    public void damage(int damage){
        if(!isInvincible){
            lifeCurrent = lifeCurrent - damage;
            isInvincible = true;
            if(lifeCurrent < 0){
                lifeCurrent = 0;
                isAlive = false;
            } else {
                iFrameCurrent = iFrameMaxCount;
            }
        }
    }
    
    public void revive(){
        isAlive = true;
        isInvincible = false;
        lifeCurrent = lifeMax;
    }
    
    public void simulate(){
        if(iFrameCurrent > 0){
            iFrameCurrent--;
            if(iFrameCurrent == 0){
                isInvincible = false;
            }
        }
    }
    
}
