package ch.ethz.geco.bass.audio.gson;

import ch.ethz.geco.bass.audio.util.AudioTrackMetaData;
import com.google.gson.*;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * Serializes an AudioTrack to json.
 */
public class AudioTrackSerializer implements JsonSerializer<AudioTrack> {
    @Override
    public JsonElement serialize(AudioTrack src, Type typeOfSrc, JsonSerializationContext context) {
        AudioTrackInfo info = src.getInfo();

        // Get meta data
        AudioTrackMetaData metaData = (AudioTrackMetaData) src.getUserData();

        // Parse votes to json array
        JsonArray votes = new JsonArray();
        for (Map.Entry<String, Byte> entry : metaData.getVotes().entrySet()) {
            JsonObject vote = new JsonObject();
            vote.addProperty(entry.getKey(), entry.getValue());
            votes.add(vote);
        }

        JsonObject jsonTrack = new JsonObject();
        jsonTrack.addProperty("id", metaData.getTrackID());
        jsonTrack.addProperty("uri", info.uri);
        jsonTrack.addProperty("userID", metaData.getUserID());
        jsonTrack.addProperty("title", info.title);
        jsonTrack.add("votes", votes);
        jsonTrack.addProperty("length", info.length);
        jsonTrack.addProperty("position", src.getPosition());

        return jsonTrack;
    }
}

@@ -1283,16 +1283,17 @@
 dd(%22vote
+r
 s%22, vote
@@ -1296,16 +1296,81 @@
 votes);%0A
+        jsonTrack.addProperty(%22votes%22, metaData.getVoteCount());%0A
         

Send vote count to interface