package org.project.entity.players;

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

public abstract class Player implements Entity, CombatActions {
    protected String name;
    protected Weapon weapon;
    protected Armor armor;
    protected int hp;
    protected int mp;
    protected int maxHP;
    protected int maxMP;
    protected int xp;
    protected int level;
    protected boolean isDefending;

    public Player(String name, int hp, int mp, Weapon weapon, Armor armor) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
        this.maxHP = hp;
        this.maxMP = mp;
        this.weapon = weapon;
        this.armor = armor;
        this.level = 1;
        this.xp = 0;
        this.isDefending = false;
    }

    @Override
    public void lightAttack(Entity target) {
        int damage= 5 + (level*2);
        target.takeDamage(damage);
        System.out.println("\u001B[31m" + name + " use Light attack! (0 mana) " + damage + " damage!\u001B[0m");
    }

    @Override
    public void heavyAttack(Entity target){
        if(mp >= 8){
            mp -= 8;
            int damage = weapon.getDamage() + 10;
            target.takeDamage(damage);
            System.out.println("\u001B[31m" + name + " use heavy attack! (-8 mana) " + damage + " damage!\u001B[0m");
        }else{
            System.out.println("\u001B[33m Not enough mana! use light attack instead.\u001B[0m" );
            lightAttack(target);
        }
    }

    @Override
    public void defend() {
        isDefending = true;
        System.out.println("\u001B[34m" + this.name + " is defending.next attack reduced by half!\u001B[0m");
    }

    @Override
    public void attack(Entity target){
        lightAttack(target);
    }

    @Override
    public void takeDamage(int damage) {
        if(isDefending){
            damage = damage/2;
            isDefending = false;
            System.out.println("\u001B[34m" + name + " Defense reduced damage!\u001B[0m");
        }

        int armorDefense =(armor != null && !armor.isBroke()) ? armor.getDefense(): 0;
        int finalDamage = damage - armorDefense;
        if(finalDamage < 0)   finalDamage=0;

        hp -= finalDamage;
        if (hp < 0) hp = 0;
        System.out.println("\u001B[31m" + name + " took " + finalDamage + " damage!\u001B[0m");

        //if(armor != null){
            //armorDefense.reductDurability();
        //}
    }

    @Override
    public void heal() {
        if(mp>=12){
            mp -= 12;
            int healAmount = 20;
            hp += healAmount;
            if (hp > maxHP) hp = maxHP;
            System.out.println("\u001B[32m" + name + " head" + healAmount + "HP (-12 mana)\u001B[0m");
        }else{
            System.out.println("\u001B[33m Not enough mana to heal!\u001B[0m");
        }
    }

    @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;
    }

    public void gainXp(int amount){
        xp += amount;
        System.out.println("\u001B[33m" + name + "gained" + amount + "XP \u001B[0m");
        if(xp >= 100){
            level ++;
            xp = 0;
            maxHP += 10;
            maxMP += 5;
            hp = maxHP;
            mp = maxMP;
            System.out.println("\u001B[33m Level up" + level + "\u001B[0m");
        }
    }

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

    public String getName() {
        return name;
    }

    public int getHp() {
        return hp;
    }

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

    public int getMp() {
        return mp;
    }

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

    public Weapon getWeapon() {
        return weapon;
    }

    public int getLevel(){
        return level;
    }

}
