package org.project.entity.players;

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

import java.util.ArrayList;


public abstract class Player implements Entity {
    protected String name;
    Weapon weapon;
    Armor armor;
    private int hp;
    private int maxHP;
    private int mp;
    private int maxMP;
    private final int defendCost = 6;
    private boolean isDefending = false;
    private final int healCost = 12;
    private boolean isInvisible = false;
    private boolean nextAttackCritical = false;

    public Player(String name, int hp, int mp, Weapon weapon, Armor armor) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;

        this.weapon = weapon;
        this.armor = armor;
    }

    @Override
    public void defend() {
        //checking mana
        if (mp < defendCost) {
            System.out.println("Not enough mana to defend!");
            return;
        }

        //reducing mana
        mp -= defendCost;

        //activating defense mode
        isDefending = true;

        System.out.println("🛡️ " + name + " raises shield, preparing to block next attack!");
    }

    @Override
    public void takeDamage(int damage) {
        int actualDamage = damage;

        //reducing damage if the player is in defend mode
        if (isDefending()) {
            actualDamage = damage / 2;
            System.out.println("🛡️ Defense reduced damage from " + damage + " to " + actualDamage);
            setDefending(false);
        }

        if (armor != null && !armor.isBroke()) {
            int defense = armor.getDefense();
            actualDamage -= defense;
            if (actualDamage < 0) actualDamage = 0;

            armor.reduceDurability(1);
            System.out.println("🛡️ Armor blocked " + defense +
                    " damage! Remaining durability: " + armor.getDurability());
        }

        setHp(getHp() - actualDamage);
        System.out.println("💥 " + getName() + " took " + actualDamage + " damage! HP: " + getHp());

        if (getHp() < 0) {
            setHp(0);
            System.out.println("💀 " + getName() + " has been defeated!");
        }
    }

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

        //reducing mana
        mp -= healCost;

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

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

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

        if (mp == maxMP) {
            System.out.println("Mana is now full! (" + mp + "/" + maxMP + ")");
        } else {
            System.out.println("Mana increased from " + oldMana + " to " + mp);
        }
    }

    public String getName() {
        return name;
    }

    public int getHp() {
        return hp;
    }

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

    public int getMp() {
        return mp;
    }

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

    public Weapon getWeapon() {
        return weapon;
    }

    public Armor getArmor() {
        return armor;
    }

    public boolean isDefending() {
        return isDefending;
    }

    public boolean isInvisible() { return isInvisible; }

    public boolean isNextAttackCritical() { return nextAttackCritical; }

    public void setInvisible(boolean invisible) { this.isInvisible = invisible; }

    public void setNextAttackCritical(boolean critical) { this.nextAttackCritical = critical; }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setMp(int mp) {
        this.mp = mp;
    }

    public void setDefending(boolean defending) {
        isDefending = defending;
    }
}
