package uk.breedrapps.vimeoextractor;

import android.support.annotation.NonNull;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * An information extracted Vimeo video containing details about
 * stream quality, stream links, duration and video title
 */
public class VimeoVideo {

    //Video title
    private String title;
    //Video length in seconds
    private long duration;
    //Stream information with key being quality name (e.g. 1080p) and value stream url
    private Map<String, String> streams;

    //Initialise VimeoVideo from JSON
    protected VimeoVideo(@NonNull String json){
        streams = new HashMap<>();
        parseJson(json);
    }

    private void parseJson(String json) {
        try {
            //Turn JSON string to object
            JSONObject requestJson = new JSONObject(json);

            //Access video information
            JSONObject videoInfo = requestJson.getJSONObject("video");
            this.duration = videoInfo.getLong("duration");
            this.title = videoInfo.getString("title");

            //Access video stream information
            JSONArray streamArray = requestJson.getJSONObject("request")
                    .getJSONObject("files")
                    .getJSONArray("progressive");

            //Get info for each stream available
            for (int streamIndex = 0; streamIndex < streamArray.length(); streamIndex++) {
                JSONObject stream = streamArray.getJSONObject(streamIndex);
                String url = stream.getString("url");
                String quality = stream.getString("quality");
                //Store stream information
                this.streams.put(quality, url);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * Video title
     * @return the video title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Video duration in seconds
     * @return the video duration
     */
    public long getDuration() {
        return duration;
    }

    /**
     * Check if given video has stream information
     * @return true if information is present, false otherwise
     */
    public boolean hasStreams(){
        return streams.size() > 0;
    }

    /**
     * Check if video has HD stream available
     * @return true if 1080 or 4096p streams are available, false otherwise
     */
    public boolean isHD(){
        return streams.containsKey("1080p") || streams.containsKey("4096p");
    }

    /**
     * Get stream information in the form of a key-value map.
     * Keys are the quality information of the stream (e.g. 1080p)
     * Values are the corresponding stream URL
     * @return Map of streams for video
     */
    public Map<String, String> getStreams() {
        return streams;
    }

}

@@ -189,16 +189,43 @@
 ashMap;%0A
+import java.util.Iterator;%0A
 import j
@@ -238,16 +238,16 @@
 il.Map;%0A
-
 %0A/**%0A * 
@@ -625,16 +625,126 @@
 streams;
+%0A    //Stream thumbnails with key being quality and value url of image%0A    private Map%3CString, String%3E thumbs;
 %0A%0A    //
@@ -854,24 +854,58 @@
 ashMap%3C%3E();%0A
+        thumbs = new HashMap%3C%3E();%0A
         pars
@@ -1302,24 +1302,369 @@
 (%22title%22);%0A%0A
+            //Get thumbnail information%0A            JSONObject thumbsInfo = videoInfo.getJSONObject(%22thumbs%22);%0A            Iterator%3CString%3E iterator;%0A            for(iterator = thumbsInfo.keys(); iterator.hasNext();) %7B%0A                String key = iterator.next();%0A                this.thumbs.put(key, thumbsInfo.getString(key));%0A            %7D%0A%0A
             
@@ -2298,24 +2298,25 @@
           %7D%0A
+%0A
         %7D ca
@@ -3425,10 +3425,619 @@
 %0A    %7D%0A%0A
+    /**%0A     * Check if video has associated thumbnails%0A     * @return true if thumbnails are present; false otherwise%0A     */%0A    public boolean hasThumbs() %7B%0A        return this.thumbs.size() %3E 0;%0A    %7D%0A%0A    /**%0A     * Get thumbnail information in the form of a key-value map.%0A     * Keys are the quality information of the thumbnail (e.g. base, 640, 1280)%0A     * The default key returned from Vimeo's API is %22base%22%0A     * Values are the corresponding thumbnail image URL%0A     * @return Map of available thumbnails for video%0A     */%0A    public Map%3CString, String%3E getThumbs() %7B%0A        return thumbs;%0A    %7D%0A
 %7D%0A

Add support for accessing thumbnail information - #2