package org.project.entity.players;

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

public abstract class Player extends Entity implements CombatActions {

    protected Inventory inventory;

    protected int level;
    protected int xp;
    protected int xpToNextLevel;

    protected int baseDamage;

    public Player(String name,
                  int hp,
                  int mana,
                  Weapon weapon) {

        super(name, hp, mana, weapon);

        this.inventory = new Inventory();

        this.level = 1;
        this.xp = 0;
        this.xpToNextLevel = 100;

        this.baseDamage = 10;
    }

    public Inventory getInventory() {
        return inventory;
    }

    public int getLevel() {
        return level;
    }

    public int getXp() {
        return xp;
    }

    public int getBaseDamage() {
        return baseDamage;
    }

    public void gainXP(int amount) {

        xp += amount;

        System.out.println(name + " gained " + amount + " XP!");

        while (xp >= xpToNextLevel) {
            levelUp();
        }
    }

    private void levelUp() {

        level++;

        xp -= xpToNextLevel;

        xpToNextLevel += 50;

        maxHp += 20;
        hp = maxHp;

        maxMana += 10;
        mana = maxMana;

        baseDamage += 5;

        System.out.println("\n=== LEVEL UP ===");
        System.out.println(name + " reached level " + level);
        System.out.println("HP increased!");
        System.out.println("Mana increased!");
        System.out.println("Damage increased!");
    }

    @Override
    public void lightAttack(Entity target) {

        System.out.println(
                this.name +
                        " uses a light attack on " +
                        target.getName()
        );

        if (weapon != null) {

            weapon.use(target);

        } else {

            target.takeDamage(baseDamage);

            System.out.println("Basic attack dealt " +
                    baseDamage +
                    " damage.");
        }

        target.resetDefending();
    }

    @Override
    public void defend() {

        isDefending = true;

        System.out.println(name + " is defending!");
    }
    public int getKeyCount() {

        int count = 0;

        for (var item : inventory.getItems()) {

            if (item.getName().equals("Castle Key")) {
                count++;
            }
        }

        return count;
    }


    @Override
    public abstract void heavyAttack(Entity target);

    @Override
    public abstract void heal();

    @Override
    public abstract void specialAbility(Entity target);
}
