package org.project.entity.players;

import org.project.entity.Entity;
import org.project.location.GameDisplay;

public abstract class Player implements Entity {
    protected String name;
    protected boolean isDefending = false;
    private int hp;
    private int maxHP = 100;
    private int mp;
    private int maxMP = 100;
    private int xp;

    public Player(String name, int hp, int mp, int xp) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
        this.xp = xp;
    }

    public void gainXp(int newXp) {
        this.xp += newXp;
        System.out.println(GameDisplay.YELLOW + getName() + " gained " + newXp + " XP 🪙" + GameDisplay.RESET);
    }

    @Override
    public void attack(Entity target) {
        System.out.println(GameDisplay.CYAN + getName() + " attacks! ⚔️" + GameDisplay.RESET);
        target.takeDamage(2);
    }

    public void heavyAttack(Entity target) {
        if (getMp() >= 2) {
            System.out.println(GameDisplay.CYAN + getName() + " uses Heavy Attack! 💣" + GameDisplay.RESET);
            target.takeDamage(4);
            this.mp -= 2;
        } else {
            System.out.println(GameDisplay.RED + getName() + " does not have enough MP for Heavy Attack! 😓" + GameDisplay.RESET);
        }
    }

    @Override
    public void defend() {
        if (getMp() >= 5) {
            this.mp -= 5;
            this.isDefending = true;
            System.out.println(GameDisplay.BLUE + getName() + " defends ! 🛡️" + GameDisplay.RESET);
        } else {
            System.out.println(GameDisplay.RED + getName() + "does not have enough MP to defend! 😓" + GameDisplay.RESET);
        }
    }

    @Override
    public void takeDamage(int damage) {
        if (isDefending) {
            damage = 0;
            this.isDefending = false;
            System.out.println(GameDisplay.GREEN + getName() + " blocks damage!🛡️" + GameDisplay.RESET);
        }
        else System.out.println(GameDisplay.RED + getName() + " takes " + damage + " damage 🩸"+ GameDisplay.RESET);

        this.hp -= damage;
        if (this.hp < 0) this.hp = 0;
    }

    @Override
    public void heal() {
        if (getMp() >= 6) {
            this.hp += 10;
            if (this.hp > maxHP) this.hp = maxHP;
            this.mp -= 6;
            System.out.println(GameDisplay.GREEN + getName() + " heals! 🩹" + GameDisplay.RESET);
        } else {
            System.out.println(GameDisplay.RED + getName() + " cannot heal due to lack of MP! 😓" + GameDisplay.RESET);
        }
    }

    public void fillMana() {
        if (xp >= 10) {
            this.mp = getMaxMP();
            this.xp -= 10;
            System.out.println(GameDisplay.GREEN + getName() + " restored full Mana 🧪!" + GameDisplay.RESET);
        } else {
            System.out.println(GameDisplay.RED + getName() + " does not have enough XP to restore Mana! 😓" + GameDisplay.RESET);
        }
    }

    public void fillHp() {
        if (xp >= 50) {
            this.hp = getMaxHP();
            this.xp -= 50;
            System.out.println(GameDisplay.GREEN + getName() + " restored 20 HP using 50 XP! 💪🤩" + GameDisplay.RESET);
        } else {
            System.out.println(GameDisplay.RED + getName() + " does not have enough XP to restore HP! 😓" + GameDisplay.RESET);
        }
    }

    @Override
    public void specialAbility(Entity target) {
        System.out.println(GameDisplay.MAGENTA +getName() + " uses special ability 😎" + GameDisplay.RESET);
    }

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

    public String getName()  {return name; }

    public int getHp() { return hp; }

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

    public int getXp() { return xp; }

    public void setXp(int newXp) { this.xp = newXp; }

    public int getMaxHP() { return maxHP; }

    public int getMp() { return mp; }

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

    public int getMaxMP() { return maxMP; }
}