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.events.CommonEvents;
007import org.intellimate.izou.sdk.events.Event;
008import org.intellimate.izou.sdk.frameworks.common.resources.SelectorResource;
009
010import java.util.ArrayList;
011import java.util.Arrays;
012import java.util.Optional;
013
014/**
015 * use this event to stop the music from playing
016 * @author LeanderK
017 * @version 1.0
018 */
019public class StopMusic extends Event {
020    public static final String ID = "izou.music.events.stop";
021    /**
022     * Creates a new Event Object
023     *
024     * @param source      the source of the Event, most likely a this reference.
025     * @throws IllegalArgumentException if one of the Arguments is null or empty
026     */
027    protected StopMusic(Identification source)
028            throws IllegalArgumentException {
029        super(CommonEvents.Type.RESPONSE_TYPE, source, new ArrayList<>(Arrays.asList(ID,
030                CommonEvents.Descriptors.STOP_DESCRIPTOR, CommonEvents.Descriptors.NOT_INTERRUPT)));
031    }
032
033    /**
034     * creates a new StopRequest
035     * @param source the caller
036     * @param target the target who should start playing
037     * @return the optional StopMusicRequest
038     */
039    public static Optional<StopMusic> createStopMusic(Identification source, Identification target) {
040        if (target == null || target.equals(source))
041            return Optional.empty();
042        try {
043            StopMusic stopRequest = new StopMusic(source);
044            stopRequest.addResource(new SelectorResource(source, target));
045            return Optional.of(stopRequest);
046        } catch (IllegalArgumentException e) {
047            return Optional.empty();
048        }
049    }
050
051    /**
052     * verifies that the StopMusicRequest is correct and checks whether the you are meant to react to it
053     * @param eventModel the EventModel to check against
054     * @param player the identifiable
055     * @return true if verified, false if not
056     */
057    public static boolean verify(EventModel eventModel, Identifiable player) {
058        if (!eventModel.containsDescriptor(StopMusic.ID))
059            return false;
060        return SelectorResource.isTarget(eventModel, player)
061                .orElse(true);
062    }
063}