package org.project.entity.enemies;

import org.project.entity.Entity;
import org.project.item.weapons.Sword;
import org.project.item.weapons.Weapon;
import org.project.location.NarrativeConsole;

public abstract class Enemy implements Entity {
    Weapon weapon;
    private int hp;
    private int mp;
    private boolean isStunned = false;
    private int stunTurns = 0;
    private String name;
    private final int defendCost = 6;
    private boolean isDefending = false;
    private final int healCost = 12;

    public Enemy(String name, int hp, int mp, Weapon weapon) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
        this.weapon = weapon;
    }

    public abstract void attack(Entity target);

    @Override
    public void takeDamage(int damage) {
        hp -= damage;
    }

    public void setStunned(boolean stunned) {
        this.isStunned = stunned;
        if (stunned) {
            this.stunTurns = 1;  //Stunned in one turn
        }
    }

    //Decreasing the stunned turns
    public void decrementStun() {
        if (isStunned) {
            stunTurns--;
            if (stunTurns <= 0) {
                isStunned = false;
            }
        }
    }

    @Override
    public void fillMana(int mana) {
        int oldMana = mp;
        mp += mana;

        mp += mana;
        if (mp > getMaxMP()) {
            mp = getMaxMP();
        }
    }

    @Override
    public void defend() {
        //checking mana

        //reducing mana
        mp -= defendCost;

        //activating defense mode
        isDefending = true;
    }

    public boolean isStunned() {
        return isStunned;
    }

    public int getHp() {
        return hp;
    }

    @Override
    public void heal(int health) {
        //checking mana
        if (mp < healCost) {
            NarrativeConsole.printWarning("❌ Not enough mana to heal!");
            return;
        }

        //reducing mana
        mp -= healCost;

        //increasing health
        hp += health;
        if (hp > getMaxHP()) {
            hp = getMaxHP();
        }
    }

    public int getMp() {
        return mp;
    }

    public Weapon getWeapon() {
        return weapon;
    }

    public String getName(Entity target) {
        if (target instanceof Skeleton) {
            return "Skeleton";
        }
        else if (target instanceof Goblin) {
            return "Goblin";
        }
        else if (target instanceof Vampire) {
            return "Vampire";
        }
        return "Dragon";
    }

    public void setHp(int newHp) {
        hp = newHp;
    }

    public boolean isAlive() {
        return (getHp() > 0);
    }
}
