package org.project.entity.enemies;

import org.project.entity.Entity;
import org.project.entity.players.Knight;
import org.project.entity.players.Player;
import org.project.item.weapons.Weapon;


public abstract class Enemy  implements Entity {
    Weapon weapon;
    private int hp;
    private int maxHP;
    private int mp;
    private int maxMP;
    private boolean defending = false;
    private double defendRate;
    private double attackMultiplier = 1;
    private boolean hasKey = false;
    private static boolean keyFound = false;

    public Enemy(int hp, int mp, Weapon weapon) {
        this.hp = hp;
        this.maxHP = hp;
        this.mp = mp;
        this.maxMP = mp;
        this.weapon = weapon;
        if (!keyFound) {
            hasKey = Math.random() < .2;
        }
        if (hasKey) {
            keyFound = true;
        }
    }
    //implement this in Main
    private boolean checkTargetAbility(Player target) {
        if (target instanceof Knight) {
            boolean check = target.getUsingSpecialAbility();
            return check;
        }
        return false;
    }

    @Override
    public  void attack(Entity target) {
        if (getMP() >= weapon.getManaCost()) {
            if (checkTargetAbility((Player) target)) {
                System.out.println("target is using special ability");
                return;
            }
            double damage = getAttackMultiplier() * weapon.getDamage();
            if (target.isDefending()) {
                target.takeDamage((int) (damage * (1 - target.getDefendRate())));
                target.setDefending(false);
            } else {
                target.takeDamage((int) damage);
            }
            setMP(getMP() - weapon.getManaCost());
        }
        else System.out.println("not enough mana");
    }

    @Override
    public void defend(){
        setDefending(true);
    };

    @Override
    public void heal(int health) {
        setHP(getHP() + health);
    }

    @Override
    public void takeDamage(int damage) {
        setHP(getHP() - damage);
    }

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

    @Override
    public void setHP(int hp) {
        this.hp = hp;
    }

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

    @Override
    public void setMaxHP(int maxHP) {
        this.maxHP = maxHP;
    }

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

    @Override
    public void setMP(int mp) {
        this.mp = mp;
    }

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

    @Override
    public void setMaxMP(int maxMP) {
        this.maxMP = maxMP;
    }

    @Override
    public boolean isDefending() {
        return defending;
    }

    @Override
    public void setDefending(boolean defending) {
        this.defending = defending;
    }

    @Override
    public double getDefendRate() {
        return defendRate;
    }

    @Override
    public void setDefendRate(double defendRate) {
        this.defendRate = defendRate;
    }

    @Override
    public Weapon getWeapon() {
        return weapon;
    }

    @Override
    public double getAttackMultiplier() {
        return attackMultiplier;
    }

    @Override
    public void setAttackMultiplier(double multiplier) {
        attackMultiplier = multiplier;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName();
    }

    public boolean getHasKey() {
        return hasKey;
    }
}

