package org.project.entity.players;

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

// TODO: UPDATE IMPLEMENTATION
public abstract class Player implements Entity{
    protected String name;
    Weapon weapon;
    private int hp;
    private int maxHP;
    private int mp;
    private int maxMP;
    private boolean canPlayThisTurn = true;
    private boolean isDefendingThisTurn = false;
    private int healManaCost;
    private int defendManaCost;
    private int keysCollected;
    private int xp = 0;

    public Player(String name, int hp, int mp, Weapon weapon,
                  int healManaCost, int defendManaCost) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
        this.maxHP = hp;
        this.maxMP = mp;
        this.weapon = weapon;
        this.keysCollected = 0;
        this.healManaCost = healManaCost;
        this.defendManaCost = defendManaCost;
    }

    public void collectKey() {
        keysCollected += 1;
    }

    @Override
    public void defend() {
       isDefendingThisTurn = true;
    }


    @Override
    public void takeDamage(int damage) {
        if (!isDefendingThisTurn) {
            hp -= damage;
            if (hp < 0) {
                hp = 0;
            }
        } else {
            isDefendingThisTurn = false;
        }
    }

    @Override
    public void heal(int health) {
        if (hp < maxHP) {
            hp += health;
            if (hp > maxHP) {
                hp = maxHP;
            }
            this.reduceMana(this.getHealManaCost());
        }
    }

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

    @Override
    public void reduceMana(int mana) {
        mp -= mana;
        if (mp < 0) {
            mp = 0;
        }
    }

    @Override
    public void skipNextTurn() {
        this.canPlayThisTurn = false;
    }
    @Override
    public void skipTurn() {
        this.canPlayThisTurn = true;
    }
    public String getName() {
        return name;
    }

    public int getHp() {
        return hp;
    }

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

    public void setMaxHP(int maxHP) {
        this.maxHP = maxHP;
    }

    public int getMp() {
        return mp;
    }

    @Override
    public int getMaxMP() {
        return maxMP;
    }
    public void setMaxMP(int maxMP) {
        this.maxMP = maxMP;
    }
    public Weapon getWeapon() {
        return weapon;
    }

    public int getKeysCollected() {
        return keysCollected;
    }

    @Override
    public String getType() {
        return "Player";
    }

    public int getHealManaCost() {
        return healManaCost;
    }

    public int getXp() {
        return xp;
    }

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

    public void buyHp(int amount) {
        if (amount <= this.getXp()) {
            this.setXp(this.getXp() - amount);
            this.setMaxHP(this.getMaxHP() + amount);
        }
    }
    public void buyMp(int amount) {
        if (amount <= this.getXp()) {
            this.setXp(this.getXp() - amount);
            this.setMaxMP(this.getMaxMP() + amount);
        }
    }

    public int getDefendManaCost() {
        return defendManaCost;
    }

}
