001package org.intellimate.izou.sdk.frameworks.music.resources;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.sdk.Context;
005import org.intellimate.izou.sdk.frameworks.music.Capabilities;
006import org.intellimate.izou.sdk.resource.Resource;
007
008import java.util.Optional;
009
010/**
011 * A resource containing commands for the player.
012 * @author LeanderK
013 * @version 1.0
014 */
015public class CommandResource extends Resource<String> {
016    public final static String ResourceID = "izou.music.resource.command";
017    public final static String PLAY = "play";
018    public final static String PAUSE = "pause";
019    public final static String STOP = "stop";
020    public final static String SELECT_TRACK = "select";
021    public final static String NEXT = "next";
022    public static final String PREVIOUS = "previous";
023    public static final String JUMP = "jump";
024    @SuppressWarnings("SpellCheckingInspection")
025    public static final String CHANGE_PLAYBACK = "changeplayback";
026    @SuppressWarnings("SpellCheckingInspection")
027    public static final String CHANGE_VOLUME = "changevolume";
028
029    /**
030     * creates a new Resource.
031     *
032     * @param provider   the Provider of the Resource
033     * @param command          the resource
034     * @param capabilities the capabilities of the player
035     */
036    private CommandResource(Identification provider, String command, Capabilities capabilities) {
037        super(ResourceID, provider, command);
038        if (!verifyCommand(command))
039            throw new IllegalArgumentException("IllegalCommand!");
040        if (!verifyCapabilities(command, capabilities))
041            throw new IllegalArgumentException("Player is not able to handle Command");
042    }
043
044    /**
045     * creates a new Resource.
046     *
047     * @param provider   the Provider of the Resource
048     * @param command          the resource
049     * @param capabilities the capabilities of the player
050     * @param context used for logging
051     * @return the commandResource if nothing illegal was passed
052     */
053    public static Optional<CommandResource> createCommandResource(Identification provider, String command, Capabilities capabilities, Context context) {
054        CommandResource commandResource = new CommandResource(provider, command, capabilities);
055        if (!verifyCommand(command)) {
056            context.getLogger().error("IllegalCommand!");
057            return Optional.empty();
058        }
059        if (!verifyCapabilities(command, capabilities)) {
060            context.getLogger().error("Player is not able to handle Command!");
061            return Optional.empty();
062        }
063        return Optional.of(commandResource);
064    }
065
066    /**
067     * verifies that an command is not malformed
068     * @param command the Command
069     * @return false if malformed
070     */
071    public static boolean verifyCommand(String command) {
072        return command.equals(PLAY) ||
073                command.equals(PAUSE) ||
074                command.equals(STOP) ||
075                command.equals(SELECT_TRACK) ||
076                command.equals(NEXT) ||
077                command.equals(PREVIOUS) ||
078                command.equals(CHANGE_PLAYBACK) ||
079                command.equals(CHANGE_VOLUME);
080    }
081
082    /**
083     * verifies that the player is capable of handling the command
084     * @param command the command
085     * @param capabilities the capabilities
086     * @return true if capable, false if not
087     */
088    public static boolean verifyCapabilities(String command, Capabilities capabilities) {
089        switch (command) {
090            case PLAY: return capabilities.hasPlayPauseControl();
091            case PAUSE: return capabilities.hasPlayPauseControl();
092            case SELECT_TRACK: return capabilities.isAbleToSelectTrack();
093            case NEXT: return capabilities.hasNextPrevious();
094            case PREVIOUS: return capabilities.hasNextPrevious();
095            case JUMP: return capabilities.isAbleToJump();
096            case CHANGE_PLAYBACK: return capabilities.isPlaybackChangeable();
097            case CHANGE_VOLUME: return capabilities.canChangeVolume();
098            case STOP: return true;
099        }
100        return false;
101    }
102
103    /**
104     * verifies tha the command is legal and able to be executed
105     * @param command the command
106     * @param capabilities the capabilities
107     * @return true if able, false if not
108     */
109    public static boolean verify(String command, Capabilities capabilities) {
110        return verifyCommand(command) && verifyCapabilities(command, capabilities);
111    }
112}