package com.perm.kate.api;

import java.io.Serializable;
import org.json.JSONException;
import org.json.JSONObject;

public class Video implements Serializable{
    private static final long serialVersionUID = 1L;
    public long vid;
    public long owner_id;
    public String title;
    public String description;
    public long duration;
    public String link1;
    public String image;//130*120
    public String image_big;//320*240
    //public String photo_640;
    public long date;
    public String player;
    //files
    public String external;
    public String mp4_240;
    public String mp4_360;
    public String mp4_480;
    public String mp4_720;
    public String flv_320;
    public String access_key;//used when private video attached to message
    
    
    public static Video parse(JSONObject o) throws NumberFormatException, JSONException{
        Video v = new Video();
        v.vid = o.getLong("id");
        v.owner_id = o.getLong("owner_id");
        v.title = Api.unescape(o.optString("title"));
        v.duration = o.optLong("duration");
        v.description = Api.unescape(o.optString("description"));
        v.image = o.optString("photo_130");
        //video.getUserVideos возвращает видео по-старому - баг в API
        if(!o.has("photo_130") && o.has("thumb"))
            v.image = o.optString("thumb");
        
        v.image_big = o.optString("photo_320");
        //video.getUserVideos возвращает видео по-старому - баг в API
        if(!o.has("photo_320") && o.has("image_medium"))
            v.image_big = o.optString("image_medium");
        v.date = o.optLong("date");
        v.player = o.optString("player");
        
        JSONObject files=o.optJSONObject("files");
        if(files!=null){
            v.external = files.optString("external");
            v.mp4_240 = files.optString("mp4_240");
            v.mp4_360 = files.optString("mp4_360");
            v.mp4_480 = files.optString("mp4_480");
            v.mp4_720 = files.optString("mp4_720");
            v.flv_320 = files.optString("flv_320");
        }
        return v;
    }
    
    public static Video parseForAttachments(JSONObject o) throws NumberFormatException, JSONException{
        Video v = new Video();
        v.vid = o.getLong("id");
        v.owner_id = o.getLong("owner_id");
        v.title = Api.unescape(o.getString("title"));
        v.duration = o.getLong("duration");
        v.description = Api.unescape(o.optString("description"));
        if(o.has("image"))
            v.image = o.optString("image");
        v.image_big = o.optString("image_big");
        if(o.has("thumb"))//video.getUserVideos
            v.image = o.optString("thumb");
        v.date = o.optLong("date");
        v.player = o.optString("player");
        v.access_key = o.optString("access_key");
        return v;
    }
    
    public String getVideoUrl() {
        return getVideoUrl(owner_id, vid);
    }
    
    public static String getVideoUrl(long owner_id, long video_id) {
        String res = null;
        String base_url = "http://vk.com/";
        res = base_url + "video" + owner_id + "_" + video_id;
        //sample http://vkontakte.ru/video4491835_158963813
        //http://79.gt2.vkadre.ru/assets/videos/f6b1af1e4258-24411750.vk.flv
        return res;
    }
}
@@ -2542,40 +2542,8 @@
 );%0D%0A
-        if(o.has(%22image%22))%0D%0A    
     
@@ -2569,21 +2569,25 @@
 String(%22
-image
+photo_130
 %22);%0D%0A   
@@ -2622,111 +2622,17 @@
 ng(%22
-image_big%22);%0D%0A        if(o.has(%22thumb%22))//video.getUserVideos%0D%0A            v.image = o.optString(%22thumb
+photo_320
 %22);%0D

Fix video parse for API 5.0