001package org.intellimate.izou.sdk.frameworks.music.events;
002
003import org.intellimate.izou.events.EventModel;
004import org.intellimate.izou.identification.Identifiable;
005import org.intellimate.izou.identification.Identification;
006import org.intellimate.izou.sdk.Context;
007import org.intellimate.izou.sdk.events.CommonEvents;
008import org.intellimate.izou.sdk.events.Event;
009import org.intellimate.izou.sdk.frameworks.common.resources.SelectorResource;
010import org.intellimate.izou.sdk.frameworks.music.Capabilities;
011import org.intellimate.izou.sdk.frameworks.music.resources.CommandResource;
012
013import java.util.Collections;
014import java.util.Optional;
015
016/**
017 * @author LeanderK
018 * @version 1.0
019 */
020public class PlayerCommand extends Event {
021    @SuppressWarnings("SpellCheckingInspection")
022    public static final String ID = "izou.music.events.playercommand";
023    /**
024     * Creates a new Event Object
025     *
026     * @param source      the source of the Event, most likely a this reference.
027     * @throws IllegalArgumentException if one of the Arguments is null or empty
028     */
029    protected PlayerCommand(Identification source) throws IllegalArgumentException {
030        super(CommonEvents.Type.RESPONSE_TYPE, source, Collections.singletonList(ID));
031    }
032
033    /**
034     * Creates a new Event Object
035     *
036     * @param source      the source of the Event, most likely a this reference.
037     * @param target the target who should start playing
038     * @param capabilities the capabilities of the player
039     * @param command the command
040     * @param context the context to use
041     * @return the optional PlayerCommand
042     * @throws IllegalArgumentException if one of the Arguments is null or empty
043     */
044    public static Optional<PlayerCommand> createPlayerCommand(Identification source, Identification target,
045                                                              String command, Capabilities capabilities,
046                                                              Context context) {
047        try {
048            Optional<CommandResource> commandResource = CommandResource.createCommandResource(source, command,
049                    capabilities, context);
050            if (!commandResource.isPresent()) {
051                context.getLogger().error("unable to obtain commandResource");
052                return Optional.empty();
053            }
054            PlayerCommand playerCommand = new PlayerCommand(source);
055            playerCommand.addResource(new SelectorResource(source, target));
056            playerCommand.addResource(commandResource.get());
057            return Optional.of(playerCommand);
058        } catch (IllegalArgumentException e) {
059            return Optional.empty();
060        }
061    }
062
063    /**
064     * verifies that the Event has the correct id and is correctly addressed
065     * @param eventModel the eventModel
066     * @param player the identifiable
067     * @return true if everything is correctly
068     */
069    public static boolean verify(EventModel eventModel, Identifiable player) {
070        if (!eventModel.containsDescriptor(PlayerCommand.ID))
071            return false;
072        return SelectorResource.isTarget(eventModel, player)
073                .orElse(true);
074    }
075}