package org.project.entity.players;

import org.project.entity.Entity;

public abstract class Player implements Entity, ICombatActions {
    protected String name;
    protected int hp;
    protected int maxHP;
    protected int mp;
    protected int maxMP;
    protected int baseDamage;
    protected boolean isDefending;
    protected boolean stunned;
    protected int level = 1;
    protected int xp = 0;

    public Player(String name, int maxHP, int maxMP, int baseDamage) {
        this.name = name;
        this.maxHP = maxHP;
        this.hp = maxHP;
        this.maxMP = maxMP;
        this.mp = maxMP;
        this.baseDamage = baseDamage;
        this.isDefending = false;
        this.stunned = false;
    }

    @Override
    public void takeDamage(int damage) {
        if (isDefending) {
            damage /= 2;
            isDefending = false;
        }
        hp -= damage;
        if (hp < 0) hp = 0;
    }

    @Override
    public void heal(int health) {
        hp += health;
        if (hp > maxHP) hp = maxHP;
    }

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

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

    @Override
    public void setStunned(boolean stunned) {
        this.stunned = stunned;
    }

    @Override
    public boolean isStunned() {
        return stunned;
    }

    public void gainXP(int amount) {
        xp += amount;
        if (xp >= level * 100) {
            levelUp();
        }
    }

    protected void levelUp() {
        level++;
        maxHP += 20;
        maxMP += 10;
        baseDamage += 5;
        hp = maxHP;
        mp = maxMP;
    }

    @Override
    public void lightAttack(Entity target) {
        target.takeDamage(baseDamage);
    }

    @Override
    public void heavyAttack(Entity target) {
        int cost = 8;
        if (mp >= cost) {
            mp -= cost;
            target.takeDamage(baseDamage * 2);
        } else {
            lightAttack(target);
        }
    }

    @Override
    public void defendAction() {
        int cost = 6;
        if (mp >= cost) {
            mp -= cost;
            isDefending = true;
        }
    }

    @Override
    public void healAction() {
        int cost = 12;
        if (mp >= cost) {
            mp -= cost;
            heal(maxHP / 3);
        }
    }

    @Override
    public String getName() {
        return name;
    }

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

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

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

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