package org.project.entity.players;

import org.project.entity.Entity;
import org.project.entity.actions.ICombatActions;
import org.project.utilities.Colors;

import java.util.*;

public abstract class Player implements Entity, ICombatActions
{
    protected String name;

    protected int maxHealth;
    protected int health;

    protected int maxMana;
    protected int mana;

    protected int damage;

    protected int level = 1;
    protected int xp = 0;

    protected boolean defending = false;
    protected boolean invisible = false;
    protected boolean criticalNext = false;

    protected Set<String> keys = new HashSet<>();

    public Player(String name, int maxHealth, int maxMana, int damage)
    {
        this.name = name;
        this.maxHealth = maxHealth;
        this.health = maxHealth;

        this.maxMana = maxMana;
        this.mana = maxMana;

        this.damage = damage;
    }

    public abstract String getClassName();

    public void takeDamage(int amount)
    {
        if (invisible)
        {
            System.out.println(Colors.PURPLE + "Attack dodged due to invisibility!" + Colors.RESET);
            invisible = false;
            return;
        }

        if (defending)
        {
            amount /= 2;
            defending = false;
        }

        health -= amount;

        if (health < 0)
            health = 0;

        System.out.println(Colors.RED + name + " took " + amount + " damage!" + Colors.RESET);
    }

    public void restore()
    {
        health = maxHealth;
        mana = maxMana;
    }

    public void gainXP(int amount)
    {
        xp += amount;

        System.out.println(Colors.YELLOW + "You gained " + amount + " XP!" + Colors.RESET);

        if (xp >= level * 50)
            levelUp();
    }

    public void levelUp()
    {
        level++;

        maxHealth += 10;
        maxMana += 10;
        damage += 3;

        health = maxHealth;
        mana = maxMana;

        System.out.println(Colors.GREEN + "LEVEL UP! You are now level " + level + Colors.RESET);
    }

    public boolean hasAllKeys()
    {
        return keys.contains("Goblin") && keys.contains("Skeleton") && keys.contains("Vampire");
    }

    public void addKey(String key)
    {
        keys.add(key);
        System.out.println(Colors.YELLOW + "You obtained the " + key + " Key!" + Colors.RESET);
    }

    public boolean hasKey(String key)
    {
        return keys.contains(key);
    }

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

    @Override
    public int getHealth()
    {
        return health;
    }

    @Override
    public int getMaxHealth()
    {
        return maxHealth;
    }

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

    public int getMana()
    {
        return mana;
    }

    public int getMaxMana()
    {
        return maxMana;
    }
}
