package org.project.item.armors;

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

/**
 * Base class for every piece of armor. Armor lowers the damage the wearer takes
 * through its {@code defense} value, and slowly wears out through
 * {@code durability}: once durability hits zero the armor breaks and stops
 * protecting until it is repaired at the merchant.
 */
public abstract class Armor implements Item {

    private final String name;
    private final int price;

    private int defense;
    private final int maxDefense;
    private int durability;
    private final int maxDurability;

    private boolean isBroke;

    protected Armor(String name, int defense, int durability, int price) {
        this.name = name;
        this.defense = defense;
        this.maxDefense = defense;
        this.durability = durability;
        this.maxDurability = durability;
        this.price = price;
    }

    /** Equipping armor has no direct effect on a target, so this is a no-op. */
    @Override
    public void use(Entity target) {
        // Armor is worn, not "used" on someone. Nothing to do here.
    }

    /** Every hit chips away one point of durability; may break the armor. */
    public void absorbHit() {
        if (isBroke) {
            return;
        }
        durability--;
        checkBreak();
    }

    public void checkBreak() {
        if (durability <= 0) {
            isBroke = true;
            defense = 0;
        }
    }

    /** Bonus: merchants can repair worn/broken armor back to full. */
    public void repair() {
        isBroke = false;
        defense = maxDefense;
        durability = maxDurability;
    }

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

    @Override
    public int getPrice() {
        return price;
    }

    public int getDefense() {
        return defense;
    }

    public int getDurability() {
        return durability;
    }

    public boolean isBroke() {
        return isBroke;
    }
}
