package org.project.item.armors;

import org.project.entity.Entity;
import org.project.entity.players.Player;
import org.project.item.Item;
import org.project.item.weapons.Weapon;

import java.util.List;

public abstract class Armor implements Item {
    private int defense;
    private int durability;
    private int maxDurability;

    private static int upgradeCost = 2;
    private int cost;
    private int x;

    private boolean isBroken;

    public Armor(int defense, int initialDrb, int cost, int x) {
        this.defense = defense;
        this.durability = initialDrb;
        this.maxDurability = initialDrb;
        this.cost = cost;
        this.x = x;
    }

    @Override
    public boolean buy(Player player) {
        List<Armor> A = player.getArmorsList();
        for (Armor a : A) {
            if (a.getClass() == this.getClass()) {
                return false;
            }
        }
        boolean res = player.addItem(this, cost);
        return res;
    }

    public boolean upgrade(Player player) {
        boolean res = player.pay(upgradeCost);
        if (res) {
            upgradeCost *= x;
            maxDurability += 1;
            defense += x;
        }
        return res;
    }

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

    public boolean repair(Player player) {
        boolean res = player.pay(maxDurability - durability);
        if (res) {
            isBroken = false;
            durability = maxDurability;
            return res;
        }
        return res;
    }

    @Override
    public int getCost() { return cost; }

    public int getDefense() {
        return defense;
    }

    public int getMaxDurability() {
        return maxDurability;
    }

    public int getDurability() { return durability; }

    public void decreaseDurability() { durability --; }

    public boolean isBroke() {
        return isBroken;
    }
}
