package org.project.entity.enemies;

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

public abstract class Enemy implements Entity {
    protected String name;
    protected int hp;
    protected int maxHP;
    protected int mp;
    protected int maxMP;
    protected int damage;
    protected Weapon weapon;
    protected boolean stunned;
    protected boolean defending;
    protected boolean resurrected;
    protected int xpReward;

    public Enemy(String name, int maxHP, int maxMP, int damage, Weapon weapon, int xpReward) {
        this.name = name;
        this.maxHP = maxHP;
        this.hp = maxHP;
        this.maxMP = maxMP;
        this.mp = maxMP;
        this.damage = damage;
        this.weapon = weapon;
        this.stunned = false;
        this.defending = false;
        this.resurrected = false;
        this.xpReward = xpReward;
    }

    @Override
    public void attack(Entity target) {
        if (stunned) {
            System.out.println(ConsoleColors.YELLOW + name + " is stunned and cannot attack!" + ConsoleColors.RESET);
            return;
        }

        int total = damage;
        if (weapon != null) {
            total += weapon.getDamage();
        }
        System.out.println(ConsoleColors.RED + name + " attacks!" + ConsoleColors.RESET);
        target.receiveAttack(total, false);
    }

    @Override
    public void receiveAttack(int damage, boolean ignoreDefense) {
        if (defending && !ignoreDefense) {
            damage /= 2;
            defending = false;
        }
        hp -= damage;
        if (hp < 0) {
            hp = 0;
        }
        System.out.println(ConsoleColors.RED + name + " took " + damage + " damage." + ConsoleColors.RESET);
    }

    @Override
    public void defend() {
        defending = true;
        System.out.println(ConsoleColors.CYAN + name + " is defending." + ConsoleColors.RESET);
    }

    @Override
    public void heal(int amount) {
        hp += amount;
        if (hp > maxHP) {
            hp = maxHP;
        }
        System.out.println(ConsoleColors.GREEN + name + " healed for " + amount + " HP." + ConsoleColors.RESET);
    }

    @Override
    public void fillMana(int amount) {
        mp += amount;
        if (mp > maxMP) {
            mp = maxMP;
        }
        System.out.println(ConsoleColors.BLUE + name + " restored " + amount + " Mana." + ConsoleColors.RESET);
    }

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

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

    @Override
    public int getMP() {
        return mp;
    }

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

    @Override
    public int getMaxMP() {
        return maxMP;
    }

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

    public void stun() {
        stunned = true;
        System.out.println(ConsoleColors.YELLOW + name + " is stunned!" + ConsoleColors.RESET);
    }

    public boolean isStunned() {
        return stunned;
    }

    public void nextTurn() {
        if (stunned) {
            stunned = false;
        }
        defending = false;
    }

    public int getXPReward() {
        return xpReward;
    }

    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }

    public Weapon getWeapon() {
        return weapon;
    }
}