001package org.intellimate.izou.sdk.frameworks.music.events;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.sdk.events.CommonEvents;
005import org.intellimate.izou.sdk.events.Event;
006import org.intellimate.izou.sdk.frameworks.music.player.Playlist;
007import org.intellimate.izou.sdk.frameworks.music.player.TrackInfo;
008import org.intellimate.izou.sdk.frameworks.music.player.Volume;
009import org.intellimate.izou.sdk.frameworks.music.resources.PlaylistResource;
010import org.intellimate.izou.sdk.frameworks.music.resources.TrackInfoResource;
011import org.intellimate.izou.sdk.frameworks.music.resources.VolumeResource;
012
013import java.util.Collections;
014import java.util.Optional;
015
016/**
017 * @author LeanderK
018 * @version 1.0
019 */
020public class PlayerUpdate extends Event {
021    @SuppressWarnings("SpellCheckingInspection")
022    public static final String ID = "izou.music.events.playerupdate";
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 PlayerUpdate(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     * @return the optional PlayerUpdate (if no illegal arguments got passed)
038     * @throws IllegalArgumentException if one of the Arguments is null or empty
039     */
040    public static Optional<PlayerUpdate> createPlayerUpdate(Identification source) {
041        try {
042            PlayerUpdate playerUpdate = new PlayerUpdate(source);
043            return Optional.of(playerUpdate);
044        } catch (IllegalArgumentException e) {
045            return Optional.empty();
046        }
047    }
048
049    /**
050     * Creates a new Event Object
051     *
052     * @param source      the source of the Event, most likely a this reference.
053     * @param volume the current volume, not null
054     * @return the optional PlayerUpdate (if no illegal arguments got passed)
055     * @throws IllegalArgumentException if one of the Arguments is null or empty
056     */
057    public static Optional<PlayerUpdate> createPlayerUpdate(Identification source, Volume volume) {
058        if (volume == null)
059            return Optional.empty();
060        try {
061            PlayerUpdate playerUpdate = new PlayerUpdate(source);
062            playerUpdate.addResource(new VolumeResource(source, volume));
063            return Optional.of(playerUpdate);
064        } catch (IllegalArgumentException e) {
065            return Optional.empty();
066        }
067    }
068
069    /**
070     * Creates a new Event Object
071     *
072     * @param source      the source of the Event, most likely a this reference.
073     * @param playlist the current playing tracks
074     * @return the optional PlayerUpdate (if no illegal arguments got passed)
075     * @throws IllegalArgumentException if one of the Arguments is null or empty
076     */
077    public static Optional<PlayerUpdate> createPlayerUpdate(Identification source, Playlist playlist) {
078        if (playlist == null)
079            return Optional.empty();
080        try {
081            PlayerUpdate playerUpdate = new PlayerUpdate(source);
082            playerUpdate.addResource(new PlaylistResource(source, playlist));
083            return Optional.of(playerUpdate);
084        } catch (IllegalArgumentException e) {
085            return Optional.empty();
086        }
087    }
088
089    /**
090     * Creates a new Event Object
091     *
092     * @param source      the source of the Event, most likely a this reference.
093     * @param trackInfo the current song
094     * @return the optional PlayerUpdate (if no illegal arguments got passed)
095     * @throws IllegalArgumentException if one of the Arguments is null or empty
096     */
097    public static Optional<PlayerUpdate> createPlayerUpdate(Identification source, TrackInfo trackInfo) {
098        if (trackInfo == null)
099            return Optional.empty();
100        try {
101            PlayerUpdate playerUpdate = new PlayerUpdate(source);
102            playerUpdate.addResource(new TrackInfoResource(source, trackInfo));
103            return Optional.of(playerUpdate);
104        } catch (IllegalArgumentException e) {
105            return Optional.empty();
106        }
107    }
108}