package org.project.entity.players;

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

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 int currentXp = 0;
    private int level = 1;
    private int xpToNextLevel = 100;

    protected static final int costLight = 0;
    protected static final int costHeavy = 20;
    protected static final int costDefend = 15;
    protected static final int costHeal = 30;
    protected static final int costSpecial = 50;
    private boolean isDefending = false;

    public static final String BLUE = "\u001B[34m";
    public static final String RED = "\033[31m";
    public static final String YELLOW = "\033[33m";
    protected static final String PURPLE = "\033[35m";
    public static final String RESET = "\u001B[0m";

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

    @Override
    public void takeDamage(int damage) {
        if (isDefending)
        {
            damage = (int) (damage * 0.1);
            isDefending = false;
        }
        int actualDamage = Math.max(0, damage - (armor != null ? armor.getDefense() : 0));
        hp -= actualDamage;
        if (this.hp < 0)
        {
            this.hp = 0;
        }
        System.out.println("💥 " + name + " took " + RED+damage+RESET + " damage!");
        System.out.println("❤️ " + name + " HP remaining: " + hp + "/" + maxHP);
    }

    @Override
    public void fillMana(int mana) {
        mp += mana;
        if (mp > maxMP) {
            mp = maxMP;
        }
    }

    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 void setMp(int mp) {
        this.mp = mp;
    }

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

    public int getCurrentXp() {
        return currentXp;
    }

    public void setCurrentXp(int currentXp) {
        this.currentXp = currentXp;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getXpToNextLevel() {
        return xpToNextLevel;
    }

    public void setXpToNextLevel(int xpToNextLevel) {
        this.xpToNextLevel = xpToNextLevel;
    }

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

    public abstract int getBaseDamage();


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

    public boolean isDefending() {
        return isDefending;
    }

    @Override
    public abstract void defend();
    @Override
    public abstract void heal(int health);
    public abstract void lightAttack(Entity target);
    public abstract void specialAbility(Entity target);
    public abstract void heavyAttack(Entity target);

    public void addXp(int amount)
    {
        this.setCurrentXp(getCurrentXp()+amount);
        System.out.println("✨ You earned "+ amount + " XP!");
        while (this.getCurrentXp() >= this.getXpToNextLevel())
        {
            levelUp();
        }
    }

    public void levelUp()
    {
        this.setCurrentXp(this.getCurrentXp()-this.getXpToNextLevel());
        this.setLevel(this.getLevel()+1);

        int hpBonus = 10 + (this.getLevel() * 5);
        int mpBonus = 5 + (this.getLevel() * 2);

        this.maxHP += hpBonus;
        this.maxMP += mpBonus;

        this.hp = this.maxHP;
        this.mp = this.maxMP;

        this.setXpToNextLevel((int) (this.getXpToNextLevel()*1.5));

        System.out.println("🎉 LEVEL UP! You are now Level " + this.getLevel() + "!");
        System.out.println("❤️ Max HP increased to " + this.maxHP);
        System.out.println("💎 Max MP increased to " + this.maxMP);
    }

}
