package org.project.item.weapons;

import org.project.entity.Entity;
import org.project.item.Item;

public abstract class Weapon implements Item
{
    private final int damage;
    private final int manaCost;
    private final double attackSpeed;
    private final double critChance;
    private final int durability;
    private int currentDurability;

    public Weapon(int damage, int manaCost, double attackSpeed, double critChance, int durability)
    {
        this.damage = damage;
        this.manaCost = manaCost;
        this.attackSpeed = attackSpeed;
        this.critChance = critChance;
        this.durability = durability;
        this.currentDurability = durability;
    }

    @Override
    public void use(Entity target)
    {
        if (currentDurability <= 0)
        {
            System.out.println(getName() + " is broken and cannot be used.");
            return;
        }

        if (target == null)
        {
            System.out.println("No target to attack.");
            return;
        }

        int finalDamage = damage;
        if (Math.random() < critChance)
        {
            finalDamage *= 2;
            System.out.println("Critical hit!");
        }

        target.takeDamage(finalDamage);
        currentDurability--;

        System.out.println(getName() + " used on " + (target.getClass().getSimpleName()) + ", damage: " + finalDamage + ", durability left: " + currentDurability);
        if (currentDurability == 0)
            System.out.println(getName() + " is now broken!");
    }

    public void repair()
    {
        currentDurability = durability;
        System.out.println(getName() + " repaired to full durability!");
    }

    public int getDamage() {return damage;}

    public int getManaCost() {return manaCost;}

    public double getAttackSpeed() {return attackSpeed;}

    public double getCritChance() {return critChance;}

    public int getDurability() {return durability;}

    public int getCurrentDurability() {return currentDurability;}

    public abstract String getName();

    public abstract String getDescription();

    public void uniqueAbility(Entity... targets) {}
}
