001package org.intellimate.izou.sdk.frameworks.music.player;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.identification.IdentificationManager;
005import org.intellimate.izou.sdk.events.Event;
006import org.intellimate.izou.sdk.frameworks.common.resources.SelectorResource;
007import org.intellimate.izou.sdk.frameworks.music.events.PlayerError;
008import org.intellimate.izou.sdk.frameworks.music.events.PlayerUpdate;
009import org.intellimate.izou.sdk.frameworks.music.resources.*;
010import org.intellimate.izou.sdk.frameworks.permanentSoundOutput.events.StartEvent;
011import org.intellimate.izou.sdk.frameworks.permanentSoundOutput.output.PermanentSoundHelper;
012
013import java.util.Optional;
014
015/**
016 * here lies some utility-methods for the music-player
017 * @author LeanderK
018 * @version 1.0
019 */
020public interface MusicHelper extends PermanentSoundHelper {
021    /**
022     * fires an StartEvent
023     * @param isUsingJava true if using java, false if not (and for example a C-library)
024     */
025    @Override
026    default void startedSound(boolean isUsingJava) {
027        getContext().getLogger().warn("creating start music event without Information");
028        startedSound(null, null, null, null, isUsingJava);
029    }
030
031    /**
032     * fires an StartEvent
033     *
034     * @param playlist the playlist or null
035     * @param progress the progress or null
036     * @param trackInfo the trackInfo or null
037     * @param volume the volume or null
038     * @param isUsingJava true if using java, false if not (and for example a C-library)
039     */
040    default void startedSound(Playlist playlist, Progress progress, TrackInfo trackInfo, Volume volume, boolean isUsingJava) {
041        Optional<Event> startEvent = IdentificationManager.getInstance().getIdentification(this)
042                .flatMap(PlayerUpdate::createPlayerUpdate)
043                .map(event -> event.addDescriptor(StartEvent.ID))
044                .map(event -> isUsingJava ? event : event.addDescriptor(StartEvent.IS_USING_NON_JAVA_OUTPUT));
045        Optional<Identification> id = IdentificationManager.getInstance().getIdentification(this);
046        if (!startEvent.isPresent() || !id.isPresent()) {
047            getContext().getLogger().error("unable to fire PlayerUpdate");
048        } else {
049            startEvent.get().addDescriptor(StartEvent.ID);
050            if (playlist != null) {
051                startEvent.get().addResource(new PlaylistResource(id.get(), playlist));
052            }
053            if (progress != null) {
054                startEvent.get().addResource(new ProgressResource(id.get(), progress));
055            }
056            if (trackInfo != null) {
057                startEvent.get().addResource(new TrackInfoResource(id.get(), trackInfo));
058            }
059            if (volume != null) {
060                startEvent.get().addResource(new VolumeResource(id.get(), volume));
061            }
062            getContext().getEvents().distributor().fireEventConcurrently(startEvent.get());
063        }
064    }
065
066    /**
067     * fires an Music-Player-Error
068     * @param message the message
069     */
070    default void playerError(String message) {
071        Optional<PlayerError> playerError = IdentificationManager.getInstance().getIdentification(this)
072                .flatMap(id -> PlayerError.createMusicPlayerError(id, message));
073        if (!playerError.isPresent()) {
074            getContext().getLogger().error("unable to fire PlayerError");
075        } else {
076            getContext().getEvents().distributor().fireEventConcurrently(playerError.get());
077        }
078    }
079
080    /**
081     * fires an Music-Player-Error
082     * @param message the message
083     * @param target the one who caused the error
084     */
085    default void playerError(String message, Identification target) {
086        Optional<PlayerError> playerError = IdentificationManager.getInstance().getIdentification(this)
087                .flatMap(id -> PlayerError.createMusicPlayerError(id, message));
088        Optional<Identification> id = IdentificationManager.getInstance().getIdentification(this);
089        if (!playerError.isPresent() || !id.isPresent()) {
090            getContext().getLogger().error("unable to fire PlayerError");
091        } else {
092            playerError.get().addResource(new SelectorResource(id.get(), target));
093            getContext().getEvents().distributor().fireEventConcurrently(playerError.get());
094        }
095    }
096
097    /**
098     * fires an update event which notifies that parameters have changed
099     * @param playlist the optional playlist
100     * @param progress the optional progress
101     * @param trackInfo the optional trackInfo
102     * @param volume the optional volume
103     */
104    default void updatePlayInfo(Playlist playlist, Progress progress, TrackInfo trackInfo, Volume volume) {
105        Optional<PlayerUpdate> updateEvent = IdentificationManager.getInstance().getIdentification(this)
106                .flatMap(PlayerUpdate::createPlayerUpdate);
107        Optional<Identification> id = IdentificationManager.getInstance().getIdentification(this);
108        if (!updateEvent.isPresent() || !id.isPresent()) {
109            getContext().getLogger().error("unable to fire PlayerUpdate");
110        } else {
111            if (playlist != null) {
112                updateEvent.get().addResource(new PlaylistResource(id.get(), playlist));
113            }
114            if (progress != null) {
115                updateEvent.get().addResource(new ProgressResource(id.get(), progress));
116            }
117            if (trackInfo != null) {
118                updateEvent.get().addResource(new TrackInfoResource(id.get(), trackInfo));
119            }
120            if (volume != null) {
121                updateEvent.get().addResource(new VolumeResource(id.get(), volume));
122            }
123            fire(updateEvent.get(), 5);
124        }
125    }
126
127    /**
128     * fires an update event which notifies that parameters have changed
129     * @param playlist the optional playlist
130     */
131    default void updatePlayInfo(Playlist playlist) {
132        updatePlayInfo(playlist, null, null, null);
133    }
134
135    /**
136     * fires an update event which notifies that parameters have changed
137     * @param progress the optional progress
138     */
139    default void updatePlayInfo(Progress progress) {
140        updatePlayInfo(null, progress, null, null);
141    }
142
143    /**
144     * fires an update event which notifies that parameters have changed
145     * @param trackInfo the optional trackInfo
146     */
147    default void updatePlayInfo(TrackInfo trackInfo) {
148        updatePlayInfo(null, null, trackInfo, null);
149    }
150
151    /**
152     * fires an update event which notifies that parameters have changed
153     * @param volume the optional volume
154     */
155    default void updatePlayInfo(Volume volume) {
156        updatePlayInfo(null, null, null, volume);
157    }
158
159    /**
160     * updates the PlaybackState
161     * @param playbackState the playbackState
162     */
163    default void updateStateInfo(PlaybackState playbackState) {
164        if (playbackState == null)
165            return;
166        Optional<PlayerUpdate> updateEvent = IdentificationManager.getInstance().getIdentification(this)
167                .flatMap(PlayerUpdate::createPlayerUpdate);
168        Optional<Identification> id = IdentificationManager.getInstance().getIdentification(this);
169        if (!updateEvent.isPresent() || !id.isPresent()) {
170            getContext().getLogger().error("unable to fire PlayerUpdate");
171        } else {
172            updateEvent.get().addResource(new PlaybackStateResource(id.get(), playbackState));
173            fire(updateEvent.get(), 5);
174        }
175    }
176}