package org.project.entity.enemies;

import org.project.entity.Entity;
import org.project.item.weapons.Weapon;

public abstract class Enemy implements Entity {
    protected String name;
    protected int hp;
    protected int maxHP;
    protected int stamina;
    protected int maxStamina;

    protected int xpReward;
    protected int coinReward;
    protected boolean hasKey;

    protected boolean isDefending = false;
    protected boolean isStunned = false;

    protected Weapon weapon;

    public Enemy(String name, int hp, int stamina, Weapon weapon, double keyChance) {
        this.name = name;
        this.hp = hp;
        this.maxHP = hp;
        this.stamina = stamina;
        this.maxStamina = stamina;
        this.weapon = weapon;
        this.hasKey = Math.random() < keyChance;

    }

    public boolean isDefending() {
        return isDefending;
    }

    public boolean isStunned() {
        return isStunned;
    }

    public void setStunned(boolean status) {
        this.isStunned = status;
    }

    public boolean hasKey() {
        return hasKey;
    }

    @Override
    public void lightAttack(Entity target) {
        target.takeDamage(20, this);
        System.out.println(name + " Performs a Light Attack!");
    }

    @Override
    public void heavyAttack(Entity target) {
        if (spendStamina(weapon.getStaminaCost())) {
            target.takeDamage(weapon.getDamage(), this);
            System.out.println(name + " Performs a Heavy Attack!");
        }
    }

    @Override
    public void defend() {
        if (spendStamina(5)) {
            this.isDefending = true;
            System.out.println(name + " Gets Ready To Block Your Attack...");
        }
    }

    @Override
    public void heal(int health) {
        if (spendStamina(10)) {
            this.hp = Math.min(maxHP, this.hp + health);
            System.out.println(name + " Healed " + health + " HP!");
        }
    }

    public abstract int choice();

    @Override
    public void takeDamage(int damage, Entity attacker) {
        if (this.isDefending) {
            if (!attacker.isDefending()) {
                System.out.println(name + " Defended Your Attack!");
            }
            this.isDefending = false;
            return;
        }
        this.hp = Math.max(0, this.hp - damage);
    }

    @Override
    public void fillHealth(int health) {
        this.hp = Math.min(maxHP, this.hp + health);
    }

    @Override
    public void fillStamina(int stamina) {
        this.stamina = Math.min(maxStamina, this.stamina + stamina);
    }

    @Override
    public boolean spendStamina(int cost) {
        if (this.stamina >= cost) {
            this.stamina -= cost;
            return true;
        }
        System.out.println(name + " Doesn't Have Enough Stamina!");
        return false;
    }

    @Override
    public boolean isAlive() {
        return this.hp > 0;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getHP() {
        return hp;
    }

    @Override
    public int getMaxHP() {
        return maxHP;
    }

    public int getStamina() {
        return stamina;
    }

    @Override
    public int getMaxStamina() {
        return maxStamina;
    }

    public Weapon getWeapon() {
        return weapon;
    }

    public int getXpReward() {
        return xpReward;
    }

    public int getCoinReward() {
        return coinReward;
    }

}
