package org.project.entity.players;

import org.project.Main;
import org.project.entity.Entity;
import org.project.item.weapons.Weapon;
import org.project.item.armors.Armor;

public abstract class Player extends Entity {

    protected Weapon weapon;
    protected Armor armor;
    boolean isDefending = false;

    public Player(String name, int maxHP, int maxMP, Weapon weapon, Armor armor) {
        super(name, maxHP, maxMP);
        this.weapon = weapon;
        this.armor = armor;
    }

    @Override
    public void attack(Entity target) {
        if (weapon == null) {
            System.out.println(name + " has no weapon!");
            return;
        }

        if (mp < weapon.getManaCost()) {
            System.out.println(name + " doesn't have enough mana!");
            return;
        }

        mp -= weapon.getManaCost();
        weapon.use(target);

        System.out.println(name + " attacked " + target.getName() +
                " with " + weapon.getClass().getSimpleName());
    }

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

    @Override
    public void takeDamage(int damage) {
        if (damage <= 0) return;


        if (armor != null) {
            damage -= armor.getDefense();
            System.out.println(name + "'s armor reduced damage by " + armor.getDefense());
        }


        if (isDefending) {
            damage /= 2;
            isDefending = false;
            System.out.println(name + " defended and reduced damage!");
        }


        damage = Math.max(0, damage);

        hp -= damage;
        if (hp < 0) hp = 0;

        System.out.println(name + " took " + damage + " damage. (" + hp + "/" + maxHP + ")");
    }
}
