package org.give2peer.give2peer;

import android.media.Image;
import android.util.Log;
import android.view.View;

import com.google.android.gms.maps.model.LatLng;

import org.joda.time.DateTime;
import org.json.JSONException;
import org.json.JSONObject;
import org.ocpsoft.prettytime.PrettyTime;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Items are first-class citizens in this app.
 * They are fetched as JSON from the server.
 * They handle their own image downloads through async tasks.
 */
public class Item
{
    protected Integer  id;
    protected String   title;
    protected String   description;
    protected String   location;
    protected Float    latitude;
    protected Float    longitude;
    protected Float    distance;
    protected DateTime updatedAt;

    protected String                thumbnail;
    protected View                  thumbnailView;

    // not sure this is used ?
    protected Image   picture;

    // Pictures to upload, (and maybe downloaded pictures, later on)
    protected List<File> pictures = new ArrayList<>();

    public Item() {}

    public Item(JSONObject json)
    {
        updateWithJSON(json);
    }

    public Item updateWithJSON(JSONObject json)
    {
        try {
            setId(json.getInt("id"));
            setTitle(json.optString("title"));
            setDescription(json.optString("description"));
            setLocation(json.getString("location"));
            setLatitude((float) json.getDouble("latitude"));
            setLongitude((float) json.getDouble("longitude"));
            setDistance((float) json.optDouble("distance", 0));
            setUpdatedAt(new DateTime(json.getString("updated_at")));
            setThumbnail(json.optString("thumbnail"));
        } catch (JSONException e) {
            Log.e("Item", e.getMessage());
            e.printStackTrace();
        }

        return this;
    }

    public String toString() { return getTitle() + ' ' + getHumanDistance(); }

    /**
     * Returns a string describing the distance in human-readable format, like :
     * - 42m
     * - 4,2km
     * - 42km
     *
     * This could be easily unit-tested. It isn't. But it could.
     */
    public String getHumanDistance()
    {
        int meters = Math.round(distance);
        if (meters <= 999) {
            return String.format("%dm", meters);
        } else if (meters > 999 && meters <= 9999) {
            return String.format("%.1fkm", meters/1000.0);
        } else if (meters > 9999) {
            return String.format("%dkm", Math.round(meters / 1000.0));
        }
        return String.format("%dm", meters);
    }

    public String getHumanUpdatedAt()
    {
        return new PrettyTime().format(updatedAt.toDate());
    }

    /**
     * @return a concatenation of the distance and the title, for the thumbnail
     */
    public String getThumbnailTitle()
    {
        String s = getHumanDistance();
        if (title.length() > 0) {
            s = s + "  " + title;
        }
        return s;
    }

    /**
     * A convenience method to generate a `LatLng` object that some third-party applications
     * (notably, Google Maps) use.
     */
    public LatLng getLatLng() { return new LatLng(getLatitude(), getLongitude()); }

    // VANILLA ACCESSORS AND MUTATORS //////////////////////////////////////////////////////////////

    public Integer getId()                           { return id; }

    public void setId(Integer id)                    { this.id = id; }

    public String getTitle()                         { return title; }

    public void setTitle(String title)               { this.title = title; }

    public String getDescription()                   { return description; }

    public void setDescription(String description)   { this.description = description; }

    public String getLocation()                      { return location; }

    public void setLocation(String location)         { this.location = location; }

    public Float getLatitude()                       { return latitude; }

    public void setLatitude(Float latitude)          { this.latitude = latitude; }

    public Float getLongitude()                      { return longitude; }

    public void setLongitude(Float longitude)        { this.longitude = longitude; }

    public Float getDistance()                       { return distance; }

    public void setDistance(Float distance)          { this.distance = distance; }

    public DateTime getUpdatedAt()                   { return updatedAt; }

    public void setUpdatedAt(DateTime updatedAt)     { this.updatedAt = updatedAt; }

    public Image getPicture()                        { return picture; }

    public void setPicture(Image picture)            { this.picture = picture; }

    public boolean hasThumbnail()                    { return ! thumbnail.isEmpty(); }

    public String getThumbnail()                     { return thumbnail; }

    public void setThumbnail(String thumbnail)       { this.thumbnail = thumbnail; }

    public boolean hasThumbnailView()                { return null != thumbnailView; }

    public View getThumbnailView()                   { return thumbnailView; }

    public void setThumbnailView(View thumbnailView) { this.thumbnailView = thumbnailView; }

    public List<File> getPictures()                  { return pictures; }

    public void setPictures(List<File> pictures)     { this.pictures = pictures; }
}

@@ -1204,30 +1204,39 @@
 ct json)
-%0A    %7B%0A       
+ throws JSONException %7B
  updateW
@@ -1249,20 +1249,16 @@
 N(json);
-%0A   
  %7D%0A%0A    
@@ -1303,34 +1303,98 @@
 json
-)%0A    %7B%0A        try %7B%0A    
+all) throws JSONException%0A    %7B%0A        JSONObject json = jsonall.getJSONObject(%22item%22);%0A%0A
     
@@ -1419,28 +1419,24 @@
 Int(%22id%22));%0A
-    
         setT
@@ -1470,28 +1470,24 @@
 ));%0A        
-   
- 
 setDescripti
@@ -1525,28 +1525,24 @@
 ));%0A        
-    
 setLocation(
@@ -1574,28 +1574,24 @@
 ));%0A        
-   
- 
 setLatitude(
@@ -1619,36 +1619,32 @@
 e(%22latitude%22));%0A
-    
         setLongi
@@ -1690,28 +1690,24 @@
 ));%0A        
-   
- 
 setDistance(
@@ -1742,28 +1742,24 @@
 ance%22, 0));%0A
-    
         setU
@@ -1808,28 +1808,24 @@
 ted_at%22)));%0A
-    
         setT
@@ -1866,130 +1866,8 @@
 %22));
-%0A        %7D catch (JSONException e) %7B%0A            Log.e(%22Item%22, e.getMessage());%0A            e.printStackTrace();%0A        %7D
 %0A%0A  

Update Item to the latest server specs.