package eu.oschl.gui.actions;

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

public class TakeItem implements Action {
    private final Game game;

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

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

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

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

    @Override
    public void execute(String[] args) {
        if (game.getCurrentRoom().isBlockedByEnemy()) {
            Output.print("The way is blocked. It's impossible to pick up items.", Color.RED);
            return;
        }

        for (var object : this.game.getCurrentRoom().getObjects()) {
            if (object.getName().equalsIgnoreCase(String.join(" ", args))) {
                if (object instanceof PickableObject item) {
                    var result = item.pickUp();

                    if (item instanceof Weapon) {
                        if (result) {
                            Output.print("Weapon ", Color.MAGENTA);
                            Output.print(item.getName(), Color.MAGENTA);
                            Output.print(" obtained.", Color.MAGENTA);
                        } else {
                            Output.print("This weapon is weaker than the current one.", Color.RED);
                        }
                    } else {
                        if (result) {
                            Output.print("Item ", Color.MAGENTA);
                            Output.print(item.getName(), Color.MAGENTA);
                            Output.print(" added to inventory.", Color.MAGENTA);
                        } else {
                            Output.print("Carrying too many items.", Color.MAGENTA);
                        }
                    }

                } else {
                    Output.print("Can't pick up ", Color.RED);
                    Output.print(object.getName(), Color.MAGENTA);
                    Output.print(".", Color.RED);
                }
                return;
            }
        }

        Output.print("That item is not here.", Color.RED);
    }
}
