package org.project.entity.players;

import org.project.entity.Entity;

public abstract class Player implements Entity
{
    protected String name;
    protected int level;
    protected int xp;
    protected int xpToNextLevel;

    protected int hp;
    protected int maxHP;
    protected int mp;
    protected int maxMP;

    protected boolean defending;

    public Player(String name, int maxHP, int maxMP)
    {
        this.name = name;
        this.level = 1;
        this.xp = 0;
        this.xpToNextLevel = 100;
        this.maxHP = maxHP;
        this.hp = maxHP;
        this.maxMP = maxMP;
        this.mp = maxMP;
        this.defending = false;
    }

    public abstract void specialAbility(Entity target);

    public boolean checkLevelUp()
    {
        if (xp >= xpToNextLevel)
        {
            level++;
            xp -= xpToNextLevel;
            xpToNextLevel *= 1.5;
            maxHP += 10;
            maxMP += 5;
            hp = maxHP;
            mp = maxMP;

            System.out.println("\u001B[32m[LEVEL UP] Congratulations! You reached Level " + level + ".\u001B[0m");
            return true;
        }
        return false;
    }

    public void addXP(int amount) {xp += amount;}

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

    @Override
    public int getHP() {return hp;}

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

    @Override
    public void setHP(int hp) {this.hp = Math.min(hp, maxHP);}

    @Override
    public int getMP() {return mp;}

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

    @Override
    public void setMP(int mp) {this.mp = Math.min(mp, maxMP);}

    @Override
    public void defend()
    {
        defending = true;
        System.out.println(name + " is defending and will reduce damage next turn.");
    }

    @Override
    public void takeDamage(int damage)
    {
        if (defending)
        {
            damage /= 2;
            System.out.println(name + " defended and reduced damage to " + damage);
            defending = false;
        }
        hp -= damage;
        if (hp < 0) hp = 0;
    }

    @Override
    public void attack(Entity target)
    {
        int damage = 10 + level * 2;
        System.out.println(name + " attacks for " + damage + " damage!");
        target.takeDamage(damage);
    }

    public String getName() {return name;}
}