package org.project.item;

import org.project.entity.players.Player;
import org.project.item.armors.Armor;
import org.project.item.weapons.Weapon;
import java.util.ArrayList;

public class Inventory {
    private Player player;

    public Inventory(Player player) {
        this.player = player;
    }

    private ArrayList<Weapon> weapons = new ArrayList<>();
    private ArrayList<Armor> armors = new ArrayList<>();
    private ArrayList<Item> miscItems = new ArrayList<>();

    public ArrayList<Weapon> getWeapons() {
        return weapons;
    }

    public ArrayList<Armor> getArmors() {
        return armors;
    }

    public void addWeapon(Weapon weapon) {
        weapons.add(weapon);
    }

    public void addArmor(Armor armor) {
        armors.add(armor);
    }

    public void addMiscItem(Item newItem) {
        for (int i = 0; i < miscItems.size(); i++) {
            Item existingItem = miscItems.get(i);

            if (existingItem.getName().equals(newItem.getName())) {
                existingItem.addQuantity(newItem.getQuantity());
                return;
            }
        }
        miscItems.add(newItem);
    }

    public void showMiscItems() {
        int listNumber = 1;

        for (int i = 0; i < miscItems.size(); i++) {
            Item currentItem = miscItems.get(i);

            if (currentItem.getName().contains("Healing Flask") && currentItem.getQuantity() > 0) {
                System.out.println(listNumber + ". " + currentItem.getName() + " (" + currentItem.getQuantity() + ")");
                listNumber++;
            }
        }

        for (int i = 0; i < miscItems.size(); i++) {
            Item currentItem = miscItems.get(i);

            if (currentItem.getName().contains("Stamina") && currentItem.getQuantity() > 0) {
                System.out.println(listNumber + ". " + currentItem.getName() + " (" + currentItem.getQuantity() + ")");
                listNumber++;
            }
        }
    }

    public Weapon getWeapon(int index) {
        return weapons.get(index);
    }

    public Armor getArmor(int index) {
        return armors.get(index);
    }

    public Item getMiscItemByName(String baseName) {
        for (int i = 0; i < miscItems.size(); i++) {
            Item item = miscItems.get(i);
            if (item.getName().contains(baseName)) {
                return item;
            }
        }
        return null;
    }

    public boolean hasAllKeys() {
        return player.hasGoblinKey() && player.hasSkeletonKey() && player.hasVampireKey();
    }
}
