001package org.intellimate.izou.sdk.frameworks.music.resources;
002
003import org.intellimate.izou.events.EventModel;
004import org.intellimate.izou.identification.Identification;
005import org.intellimate.izou.sdk.frameworks.music.player.TrackInfo;
006import org.intellimate.izou.sdk.resource.Resource;
007
008import java.util.HashMap;
009import java.util.Optional;
010
011/**
012 * A resource which holds a trackInfo for events, can also be obtained via the resourceManager (retruns the current)
013 * @author LeanderK
014 * @version 1.0
015 */
016public class TrackInfoResource extends Resource<HashMap<String, Object>> {
017    @SuppressWarnings("SpellCheckingInspection")
018    public static final String RESOURCE_ID = "izou.music.resource.trackinfo";
019
020    /**
021     * creates a new Resource.
022     * This method is thread-safe.
023     */
024    public TrackInfoResource() {
025        super(RESOURCE_ID);
026    }
027
028    /**
029     * creates a new Resource.
030     * This method is thread-safe.
031     *
032     * @param provider   the Provider of the Resource
033     */
034    public TrackInfoResource(Identification provider) {
035        super(RESOURCE_ID, provider);
036    }
037
038    /**
039     * creates a new Resource.
040     * This method is thread-safe.
041     *
042     * @param provider   the Provider of the Resource
043     * @param trackInfo  the resource
044     */
045    public TrackInfoResource(Identification provider, TrackInfo trackInfo) {
046        super(RESOURCE_ID, provider, trackInfo.export());
047    }
048
049    /**
050     * creates a new Resource.
051     * This method is thread-safe.
052     *
053     * @param provider   the Provider of the Resource
054     * @param consumer   the ID of the Consumer
055     */
056    public TrackInfoResource(Identification provider, Identification consumer) {
057        super(RESOURCE_ID, provider, consumer);
058    }
059
060    /**
061     * creates a new Resource.
062     * This method is thread-safe.
063     *
064     * @param provider   the Provider of the Resource
065     * @param trackInfo  the resource
066     * @param consumer   the ID of the Consumer
067     */
068    public TrackInfoResource(Identification provider, TrackInfo trackInfo, Identification consumer) {
069        super(RESOURCE_ID, provider, trackInfo.export(), consumer);
070    }
071
072    /**
073     * gets the first TrackInfo if found in the EventModel
074     * @param eventModel the EventModel
075     * @return return the optional TrackInfo
076     */
077    public static Optional<TrackInfo> getTrackInfo(EventModel eventModel) {
078        if (eventModel.getListResourceContainer().containsResourcesFromSource(RESOURCE_ID)) {
079            return eventModel
080                    .getListResourceContainer()
081                    .provideResource(RESOURCE_ID)
082                    .stream()
083                    .findAny()
084                    .flatMap(TrackInfo::importFromResource);
085        } else {
086            return Optional.empty();
087        }
088    }
089}