package eu.oschl.gui;

import eu.oschl.gui.actions.*;
import eu.oschl.gui.exceptions.InvalidActionId;
import eu.oschl.textadventure.Game;

import java.util.ArrayList;
import java.util.List;

public class ActionProcessor extends Observable {
    private final ArrayList<Action> actions;

    public ActionProcessor(ArrayList<Action> actions) {
        this.actions = actions;
    }

    public ArrayList<Action> getActions() {
        return actions;
    }

    /**
     * Factory method to create a ActionProcessor with a predefined set of Actions.
     *
     * @param game the game instance to be used by the actions
     * @return a new ActionProcessor instance with initialized actions
     */
    public static ActionProcessor create(Game game) {
        ArrayList<Action> actions = new ArrayList<>(List.of(
            new Explore(game),
            new Slay(game),
            new PressButton(game),
            new TakeItem(game),
            new Enter(game),
            new UseItem(game)
        ));

        return new ActionProcessor(actions);
    }

    /**
     * Executes the action of the provided ID and arguments.
     *
     * @param id the action ID
     * @throws InvalidActionId if the action ID is not found or is invalid
     */
    public void executeAction(String id, String[] args) throws InvalidActionId {
        Output.clear();

        for (Action action : actions) {
            if (action.getId().equals(id)) {
                action.execute(args);

                sendEvent();

                return;
            }
        }

        throw new InvalidActionId("Invalid action ID");
    }
}
