package eu.oschl.gui.actions;

import eu.oschl.gui.Output;
import eu.oschl.textadventure.Game;
import javafx.scene.paint.Color;

public class UseItem implements Action {
    private final Game game;

    public UseItem(Game game) {
        this.game = game;
    }

    @Override
    public String getId() {
        return "useitem";
    }

    @Override
    public String getName() {
        return "Use item";
    }

    @Override
    public String getDescription() {
        return "use an item";
    }

    @Override
    public void execute(String[] args) {
        if (args.length == 0) {
            Output.print("What item?", Color.RED);
            return;
        }

        var item = this.game.getInventory().getItems().stream()
                .filter(inventoryItem -> inventoryItem.getName().equalsIgnoreCase(String.join(" ", args)))
                .findFirst()
                .orElse(null);

        if (item == null) {
            Output.print("That item does not exist.", Color.RED);
            return;
        }

        var result = item.use();

        if (result) {
            Output.print(item.getUseText(), Color.MAGENTA);
        } else {
            Output.print("It's impossible to use ", Color.RED);
            Output.print(item.getName(), Color.MAGENTA);
            Output.print(" here.", Color.RED);
        }
    }
}
