package org.project.entity.players;

import org.project.entity.Entity;
import org.project.entity.enemies.Enemy;
import org.project.item.weapons.Weapon;

public abstract class Player implements Entity {
    protected String name;
    Weapon weapon;
    protected int hp;
    protected int maxHP;
    protected int mp;
    protected int maxMP;
    protected boolean defending = false;
    private boolean stunned = false;
    protected int keys;
    protected boolean alive = true;

    protected final int defendMP = 20;
    protected int specialAbilityMP;

    public static final String RESET = "\u001B[0m";
    public static final String RED_TEXT = "\u001B[31m";


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

    public void lightAttack(Entity target){
        if(target.isDefending()){ // check if enemy is defending or not
            System.out.printf("\t%s is Defending. You missed your attack. \uD83D\uDEE1\uFE0F \n", target.getName());
            return;
        }
    }

    public void heavyAttack(Entity target){ // Uses weapons
        if(target.isDefending()){ // check if enemy is defending or not
            System.out.printf("\t%s is Defending. You missed your attack. \uD83D\uDEE1\uFE0F\n", target.getName());
            return;
        }
        System.out.printf("\t%s used Heavy Attack \uD83D\uDCA5! (%d Mana)\n", name, weapon.getManaCost());
        target.takeDamage(weapon.getDamage());
        mp -= weapon.getManaCost();
    }

    @Override
    public void defend(boolean value) {
        if(value){
            System.out.printf("\t%s used Defend, Enemy cannot attack in his next turn \uD83D\uDEE1\uFE0F! (%d Mana)\n", name, 20);
            mp -= 20;
            defending = true;
            return;
        }
        defending = false;
    }

    @Override
    public void takeDamage(int damage) {
        hp -= damage;
        System.out.printf("\t%s took %d damage \uD83D\uDCA5\n", name, damage);
    }

    @Override
    public void heal(int health) {
        System.out.printf("\t%s used Heal ❤\uFE0F! (%d Mana)\n", name, 20);
        hp += health;
        if (hp > maxHP) {
            hp = maxHP;
        }
        mp -= 20;
    }

    @Override
    public void stun(boolean value) {
        stunned = value;
    }


    public void fillMana() {
        mp = maxMP;
        hp = maxHP;
    }

    public void addKey(Enemy target){
        keys++;
        target.dropKey();
        System.out.printf("\t%s gained %s's key \uD83D\uDDDD\uFE0F! gained keys: %d\n\n", getName(), target.getName(), keys);
    }

    public void addXP(int xp){
        System.out.printf("\n\t%d XP added.\n", xp);
        maxMP += xp;
        maxHP += xp;
    }


    // Getter Methods
    public String getName() {
        return name;
    }

    public int getHp() {
        return hp;
    }

    public int getMaxHP() {
        return maxHP;
    }

    public int getMp() {
        return mp;
    }

    public int getMaxMP() {
        return maxMP;
    }

    public Weapon getWeapon() {
        return weapon;
    }

    @Override
    public boolean isDefending() {
        return defending;
    }

    public int getKeys() {
        return keys;
    }

    public int getDefendMP() {
        return defendMP;
    }

    public int getSpecialAbilityMP() {
        return specialAbilityMP;
    }

    @Override
    public boolean isAlive() {
        if(hp <= 0){
            System.out.println(RED_TEXT + "\t=============== Game Over ===============" + RESET);
            alive = false;
        }
        return alive;
    }
}
